1 26 package org.objectweb.jonas.jdbc; 27 28 29 import java.sql.DriverManager ; 30 import java.sql.SQLException ; 31 32 import javax.resource.ResourceException ; 33 import javax.resource.spi.ConnectionRequestInfo ; 34 import javax.resource.spi.ManagedConnection ; 35 import javax.resource.spi.ResourceAllocationException ; 36 import javax.resource.spi.security.PasswordCredential ; 37 import javax.security.auth.Subject ; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 41 public class DriverManagerMCFImpl 42 extends ManagedConnectionFactoryImpl { 43 44 public ManagedConnection createManagedConnection(Subject subject, 45 ConnectionRequestInfo cxReq) 46 throws ResourceException { 47 48 if (trace.isLoggable(BasicLevel.DEBUG)) { 49 trace.log(BasicLevel.DEBUG,"subject:"+subject+" connectionRequest:"+cxReq); 50 } 51 PasswordCredential pc = Utility.getPasswordCredential(this, subject, cxReq, pw); 52 String clsName = null; 53 DriverWrapper dWrap = null; 54 try 55 { 56 clsName = mcfData.getMCFData(MCFData.DSCLASS); 57 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 58 java.sql.Driver d = (java.sql.Driver ) Class.forName(clsName, true, loader).newInstance(); 59 dWrap = new DriverWrapper(d); 60 DriverManager.registerDriver(dWrap); 61 } 62 catch(ClassNotFoundException cnfe) 63 { 64 throw new ResourceException ("Class Name not found:" + clsName); 65 } 66 catch(Exception ex) 67 { 68 throw new ResourceException ("Error loading driver manager: " + clsName+" "+ex.getMessage()); 69 } 70 71 java.sql.Connection connection = null; 72 try { 73 String val = null; 74 if ((val = mcfData.getMCFData(MCFData.LOGINTIMEOUT)) != null) { 75 if (val.length() > 0) { 76 DriverManager.setLoginTimeout(Integer.parseInt(val)); 77 } 78 } 79 if(cxReq != null) { 80 ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq; 81 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL), 82 cx.getUser(), cx.getPassword()); 83 } else if (pc != null){ 84 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL), 85 pc.getUserName(), new String (pc.getPassword())); 86 } else { 87 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL), 88 mcfData.getMCFData(MCFData.USER), 89 mcfData.getMCFData(MCFData.PASSWORD)); 90 } 91 } 92 catch(SQLException sqle) 93 { 94 sqle.printStackTrace(); 95 throw new ResourceAllocationException ("The connection could not be allocated: " + sqle.getMessage()); 96 } 97 catch(Exception ex) 98 { 99 ex.printStackTrace(); 100 throw new ResourceAllocationException ("Error on allocation: " + ex.getMessage()); 101 } 102 ManagedConnectionImpl mci = new ManagedConnectionImpl(this, pc, connection, null, null, dWrap); 103 if (trace.isLoggable(BasicLevel.DEBUG)) { 104 trace.log(BasicLevel.DEBUG, "Create Mc="+this+" with connection="+connection); 105 } 106 return mci; 107 } 108 109 111 public boolean equals(Object obj) { 112 if (obj instanceof DriverManagerMCFImpl) { 113 return mcfData.equals(((DriverManagerMCFImpl)obj).mcfData); 114 } 115 else { 116 return false; 117 } 118 } 119 120 public String getURL() 122 { 123 return mcfData.getMCFData(MCFData.URL); 124 } 125 126 public void setURL(String val) 127 { 128 mcfData.setMCFData(MCFData.URL, val); 129 } 130 } | Popular Tags |