1 45 46 package org.openejb.resource.jdbc; 47 48 import javax.resource.spi.ConnectionManager ; 49 import javax.resource.spi.ConnectionRequestInfo ; 50 import javax.resource.spi.EISSystemException ; 51 import javax.resource.spi.ManagedConnection ; 52 import javax.resource.spi.ResourceAdapter ; 53 import javax.security.auth.Subject ; 54 import java.sql.DriverManager ; 55 import java.sql.Connection ; 56 57 60 public class BasicManagedConnectionFactory implements javax.resource.spi.ManagedConnectionFactory , java.io.Serializable { 61 private final String jdbcDriver; 62 private final String jdbcUrl; 63 private final String defaultUserName; 64 private final String defaultPassword; 65 private java.io.PrintWriter logWriter; 66 private final int hashCode; 67 private final JdbcManagedConnectionFactory managedConnectionFactory; 68 69 public BasicManagedConnectionFactory(JdbcManagedConnectionFactory factory, String jdbcDriver, String jdbcUrl, String defaultUserName, String defaultPassword) { 70 this.managedConnectionFactory = factory; 71 this.jdbcDriver = jdbcDriver; 72 this.jdbcUrl = jdbcUrl; 73 this.defaultUserName = defaultUserName; 74 this.defaultPassword = defaultPassword; 75 hashCode = jdbcDriver.hashCode() ^ jdbcUrl.hashCode() ^ defaultUserName.hashCode() ^ defaultPassword.hashCode(); 76 } 77 78 public Object createConnectionFactory() throws javax.resource.ResourceException { 79 throw new javax.resource.NotSupportedException ("This connector must be used with an application server connection manager"); 80 } 81 82 public Object createConnectionFactory(ConnectionManager cxManager) throws javax.resource.ResourceException { 83 return new JdbcConnectionFactory(managedConnectionFactory, cxManager, jdbcUrl, jdbcDriver, defaultPassword, defaultUserName); 84 } 85 86 public ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo connectionRequestInfo) throws javax.resource.ResourceException { 87 try { 88 JdbcConnectionRequestInfo request = (JdbcConnectionRequestInfo) connectionRequestInfo; 89 Connection connection = DriverManager.getConnection(jdbcUrl, request.getUserName(), request.getPassword()); 90 return new JdbcManagedConnection(managedConnectionFactory, connection, request); 91 } catch (java.sql.SQLException e) { 92 throw (EISSystemException )new EISSystemException ("Could not obtain a physical JDBC connection from the DriverManager").initCause(e); 93 } 94 } 95 96 public boolean equals(Object object) { 97 if (!(object instanceof BasicManagedConnectionFactory)) { 98 return false; 99 } 100 BasicManagedConnectionFactory that = (BasicManagedConnectionFactory) object; 101 return jdbcDriver.equals(that.jdbcDriver) && jdbcUrl.equals(that.jdbcUrl) && defaultUserName.equals(that.defaultUserName) && defaultPassword.equals(that.defaultPassword); 102 } 103 104 public java.io.PrintWriter getLogWriter() { 105 return logWriter; 106 } 107 108 public int hashCode() { 109 return hashCode; 110 } 111 112 public ManagedConnection matchManagedConnections(java.util.Set connectionSet, javax.security.auth.Subject subject, ConnectionRequestInfo connectionInfo) throws javax.resource.ResourceException { 113 if (!(connectionInfo instanceof JdbcConnectionRequestInfo)) { 114 return null; 115 } 116 117 JdbcManagedConnection[] connections = (JdbcManagedConnection[]) connectionSet.toArray(new JdbcManagedConnection[]{}); 118 int i = 0; 119 for (; i < connections.length && !connections[i].getRequestInfo().equals(connectionInfo); i++) { 120 } 121 return (i < connections.length) ? connections[i] : null; 122 } 123 124 public void setLogWriter(java.io.PrintWriter out) { 125 logWriter = out; 126 } 127 128 public ResourceAdapter getResourceAdapter() { 129 return null; } 131 132 public void setResourceAdapter(ResourceAdapter ra) { 133 } 135 136 137 } 138 | Popular Tags |