1 45 package org.openejb.resource.jdbc; 46 47 import org.openejb.core.EnvProps; 48 import org.openejb.util.Logger; 49 50 import javax.resource.ResourceException ; 51 import javax.resource.spi.ConnectionManager ; 52 import javax.resource.spi.ConnectionRequestInfo ; 53 import javax.resource.spi.ManagedConnection ; 54 import javax.resource.spi.ManagedConnectionFactory ; 55 import javax.resource.spi.ResourceAdapterInternalException ; 56 import javax.security.auth.Subject ; 57 import java.io.PrintWriter ; 58 import java.util.Set ; 59 60 public class JdbcManagedConnectionFactory implements javax.resource.spi.ManagedConnectionFactory , java.io.Serializable { 61 62 protected Logger logger = Logger.getInstance("OpenEJB.connector", "org.openejb.alt.util.resources"); 63 private ManagedConnectionFactory factory; 64 65 public void init(java.util.Properties props) throws javax.resource.spi.ResourceAdapterInternalException { 66 String defaultUserName = props.getProperty(EnvProps.USER_NAME); 67 String defaultPassword = props.getProperty(EnvProps.PASSWORD); 68 String url = props.getProperty(EnvProps.JDBC_URL); 69 String driver = props.getProperty(EnvProps.JDBC_DRIVER); 70 71 loadDriver(driver); 72 73 factory = new BasicManagedConnectionFactory(this, driver, url, defaultUserName, defaultPassword); 74 75 if (driver.equals("org.enhydra.instantdb.jdbc.idbDriver")) { 76 factory = new ManagedConnectionFactoryPathHack(factory); 77 } 78 79 JdbcConnectionRequestInfo info = new JdbcConnectionRequestInfo(defaultUserName, defaultPassword, driver, url); 80 ManagedConnection connection = null; 81 try { 82 connection = factory.createManagedConnection(null, info); 83 } catch (Throwable e) { 84 logger.error("Testing driver failed. " + "[" + url + "] " 85 + "Could not obtain a physical JDBC connection from the DriverManager." 86 + "\nThe error message was:\n" + e.getMessage() + "\nPossible cause:" 87 + "\n\to JDBC driver classes are not available to OpenEJB" 88 + "\n\to Relative paths are not resolved properly"); 89 } finally { 90 try { 91 connection.destroy(); 92 } catch (ResourceException dontCare) { 93 } 94 } 95 } 96 97 private void loadDriver(String driver) throws ResourceAdapterInternalException { 98 try { 99 ClassLoader classLoader = (ClassLoader ) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction () { 100 public Object run() { 101 return Thread.currentThread().getContextClassLoader(); 102 } 103 }); 104 Class.forName(driver, true, classLoader); 105 } catch (ClassNotFoundException cnf) { 106 throw new ResourceAdapterInternalException ("JDBC Driver class \"" + driver + "\" not found by class loader", ErrorCode.JDBC_0002); 107 } 108 } 109 110 public Object createConnectionFactory(ConnectionManager connectionManager) throws ResourceException { 111 return factory.createConnectionFactory(connectionManager); 112 } 113 114 public Object createConnectionFactory() throws ResourceException { 115 return factory.createConnectionFactory(); 116 } 117 118 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException { 119 return factory.createManagedConnection(subject, connectionRequestInfo); 120 } 121 122 public ManagedConnection matchManagedConnections(Set set, Subject subject, ConnectionRequestInfo connectionRequestInfo) throws ResourceException { 123 return factory.matchManagedConnections(set, subject, connectionRequestInfo); 124 } 125 126 public void setLogWriter(PrintWriter printWriter) throws ResourceException { 127 factory.setLogWriter(printWriter); 128 } 129 130 public PrintWriter getLogWriter() throws ResourceException { 131 return factory.getLogWriter(); 132 } 133 134 public int hashCode() { 135 return factory.hashCode(); 136 } 137 138 public boolean equals(Object o) { 139 return factory.equals(o); 140 } 141 } | Popular Tags |