1 45 package org.openejb.resource.jdbc; 46 47 import javax.naming.Reference ; 48 import javax.resource.ResourceException ; 49 import javax.resource.spi.ApplicationServerInternalException ; 50 import javax.resource.spi.ConnectionManager ; 51 import javax.resource.spi.ManagedConnectionFactory ; 52 import javax.resource.spi.ResourceAdapterInternalException ; 53 import javax.resource.spi.ResourceAllocationException ; 54 import java.io.PrintWriter ; 55 import java.sql.Connection ; 56 import java.sql.SQLException ; 57 58 69 public class JdbcConnectionFactory implements javax.sql.DataSource , javax.resource.Referenceable , java.io.Serializable { 70 73 private Reference jndiReference; 74 75 private final transient ManagedConnectionFactory managedConnectionFactory; 76 private final transient ConnectionManager connectionManager; 77 private final String jdbcUrl; 78 private final String jdbcDriver; 79 private final String defaultPassword; 80 private final String defaultUserName; 81 private transient PrintWriter logWriter; 82 private int logTimeout = 0; 83 84 public JdbcConnectionFactory(ManagedConnectionFactory managedConnectionFactory, 85 ConnectionManager connectionManager, String jdbcUrl, 86 String jdbcDriver, String defaultPassword, String defaultUserName) throws ResourceException { 87 this.managedConnectionFactory = managedConnectionFactory; 88 this.connectionManager = connectionManager; 89 this.logWriter = managedConnectionFactory.getLogWriter(); 90 this.jdbcUrl = jdbcUrl; 91 this.jdbcDriver = jdbcDriver; 92 this.defaultPassword = defaultPassword; 93 this.defaultUserName = defaultUserName; 94 } 95 96 101 public void setReference(Reference jndiReference) { 102 this.jndiReference = jndiReference; 103 } 104 105 110 public Reference getReference() { 111 return jndiReference; 112 } 113 114 public Connection getConnection() throws SQLException { 115 return getConnection(defaultUserName, defaultPassword); 116 } 117 118 public Connection getConnection(java.lang.String username, java.lang.String password) throws SQLException { 119 return getConnection(new JdbcConnectionRequestInfo(username, password, jdbcDriver, jdbcUrl)); 120 } 121 122 protected Connection getConnection(JdbcConnectionRequestInfo connectionRequestInfo) throws SQLException { 123 try { 126 return (Connection ) connectionManager.allocateConnection(managedConnectionFactory, connectionRequestInfo); 127 } catch (ApplicationServerInternalException e) { 128 throw convertToSQLException(e, "Application error in ContainerManager"); 129 } catch (javax.resource.spi.SecurityException e) { 130 throw convertToSQLException(e, "Authentication error. Invalid credentials"); 131 } catch (ResourceAdapterInternalException e) { 132 throw convertToSQLException(e, "JDBC Connection problem"); 133 } catch (ResourceAllocationException e) { 134 throw convertToSQLException(e, "JDBC Connection could not be obtained"); 135 } catch (ResourceException e) { 136 throw convertToSQLException(e, "JDBC Connection Factory problem"); 137 } 138 } 139 140 private SQLException convertToSQLException(ResourceException e, String error) { 141 Throwable cause = e.getCause(); 142 if (cause instanceof SQLException ) { 143 return (SQLException ) cause; 144 } else { 145 String message = ((cause != null) ? cause.getMessage() : ""); 146 return (SQLException ) new SQLException ("Error code: " + e.getErrorCode() + error + message).initCause(e); 147 } 148 } 149 150 public int getLoginTimeout() { 151 return logTimeout; 152 } 153 154 public java.io.PrintWriter getLogWriter() { 155 return logWriter; 156 } 157 158 public void setLoginTimeout(int seconds) { 159 logTimeout = seconds; 161 } 162 163 public void setLogWriter(java.io.PrintWriter out) { 164 logWriter = out; 165 } 166 } 167 | Popular Tags |