00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 package org.objectweb.jonas.jdbc;
00027
00028
00029 import java.sql.DriverManager;
00030 import java.sql.SQLException;
00031
00032 import javax.resource.ResourceException;
00033 import javax.resource.spi.ConnectionRequestInfo;
00034 import javax.resource.spi.ManagedConnection;
00035 import javax.resource.spi.ResourceAllocationException;
00036 import javax.resource.spi.security.PasswordCredential;
00037 import javax.security.auth.Subject;
00038
00039 import org.objectweb.util.monolog.api.BasicLevel;
00040
00041 public class DriverManagerMCFImpl
00042 extends ManagedConnectionFactoryImpl {
00043
00044 public ManagedConnection createManagedConnection(Subject subject,
00045 ConnectionRequestInfo cxReq)
00046 throws ResourceException {
00047
00048 trace.log(BasicLevel.DEBUG,"subject:"+subject+" connectionRequest:"+cxReq);
00049 PasswordCredential pc = Utility.getPasswordCredential(this, subject, cxReq, pw);
00050 String clsName = null;
00051 DriverWrapper dWrap = null;
00052 try
00053 {
00054 clsName = mcfData.getMCFData(MCFData.DSCLASS);
00055 ClassLoader loader = Thread.currentThread().getContextClassLoader();
00056 java.sql.Driver d = (java.sql.Driver) Class.forName(clsName, true, loader).newInstance();
00057 dWrap = new DriverWrapper(d);
00058 DriverManager.registerDriver(dWrap);
00059 }
00060 catch(ClassNotFoundException cnfe)
00061 {
00062 throw new ResourceException("Class Name not found:" + clsName);
00063 }
00064 catch(Exception ex)
00065 {
00066 throw new ResourceException("Error loading driver manager: " + clsName+" "+ex.getMessage());
00067 }
00068
00069 java.sql.Connection connection = null;
00070 try {
00071 String val = null;
00072 if ((val = mcfData.getMCFData(MCFData.LOGINTIMEOUT)) != null) {
00073 if (val.length() > 0) {
00074 DriverManager.setLoginTimeout(Integer.parseInt(val));
00075 }
00076 }
00077 if(cxReq != null) {
00078 ConnectionRequestInfoImpl cx = (ConnectionRequestInfoImpl) cxReq;
00079 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00080 cx.getUser(), cx.getPassword());
00081 } else if (pc != null){
00082 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00083 pc.getUserName(), new String(pc.getPassword()));
00084 } else {
00085 connection = DriverManager.getConnection(mcfData.getMCFData(MCFData.URL),
00086 mcfData.getMCFData(MCFData.USER),
00087 mcfData.getMCFData(MCFData.PASSWORD));
00088 }
00089 }
00090 catch(SQLException sqle)
00091 {
00092 sqle.printStackTrace();
00093 throw new ResourceAllocationException("The connection could not be allocated: " + sqle.getMessage());
00094 }
00095 catch(Exception ex)
00096 {
00097 ex.printStackTrace();
00098 throw new ResourceAllocationException("Error on allocation: " + ex.getMessage());
00099 }
00100 ManagedConnectionImpl mci = new ManagedConnectionImpl(this, pc, connection, null, null, dWrap);
00101 trace.log(BasicLevel.DEBUG, "Create Mc="+this+" with connection="+connection);
00102 return mci;
00103 }
00104
00105
00106
00107 public boolean equals(Object obj) {
00108 if (obj instanceof DriverManagerMCFImpl) {
00109 return mcfData.equals(((DriverManagerMCFImpl)obj).mcfData);
00110 }
00111 else {
00112 return false;
00113 }
00114 }
00115
00116
00117 public String getURL()
00118 {
00119 return mcfData.getMCFData(MCFData.URL);
00120 }
00121
00122 public void setURL(String val)
00123 {
00124 mcfData.setMCFData(MCFData.URL, val);
00125 }
00126 }