KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > resource > jdbc > BasicManagedConnectionFactory


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: BasicManagedConnectionFactory.java 2116 2005-08-26 22:00:07Z dblevins $
44  */

45
46 package org.openejb.resource.jdbc;
47
48 import javax.resource.spi.ConnectionManager JavaDoc;
49 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
50 import javax.resource.spi.EISSystemException JavaDoc;
51 import javax.resource.spi.ManagedConnection JavaDoc;
52 import javax.resource.spi.ResourceAdapter JavaDoc;
53 import javax.security.auth.Subject JavaDoc;
54 import java.sql.DriverManager JavaDoc;
55 import java.sql.Connection JavaDoc;
56
57 /**
58  * @version $Revision: 2116 $ $Date: 2005-08-26 15:00:07 -0700 (Fri, 26 Aug 2005) $
59  */

60 public class BasicManagedConnectionFactory implements javax.resource.spi.ManagedConnectionFactory JavaDoc, java.io.Serializable JavaDoc {
61     private final String JavaDoc jdbcDriver;
62     private final String JavaDoc jdbcUrl;
63     private final String JavaDoc defaultUserName;
64     private final String JavaDoc defaultPassword;
65     private java.io.PrintWriter JavaDoc logWriter;
66     private final int hashCode;
67     private final JdbcManagedConnectionFactory managedConnectionFactory;
68
69     public BasicManagedConnectionFactory(JdbcManagedConnectionFactory factory, String JavaDoc jdbcDriver, String JavaDoc jdbcUrl, String JavaDoc defaultUserName, String JavaDoc 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 JavaDoc createConnectionFactory() throws javax.resource.ResourceException JavaDoc {
79         throw new javax.resource.NotSupportedException JavaDoc("This connector must be used with an application server connection manager");
80     }
81
82     public Object JavaDoc createConnectionFactory(ConnectionManager JavaDoc cxManager) throws javax.resource.ResourceException JavaDoc {
83         return new JdbcConnectionFactory(managedConnectionFactory, cxManager, jdbcUrl, jdbcDriver, defaultPassword, defaultUserName);
84     }
85
86     public ManagedConnection JavaDoc createManagedConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc connectionRequestInfo) throws javax.resource.ResourceException JavaDoc {
87         try {
88             JdbcConnectionRequestInfo request = (JdbcConnectionRequestInfo) connectionRequestInfo;
89             Connection JavaDoc connection = DriverManager.getConnection(jdbcUrl, request.getUserName(), request.getPassword());
90             return new JdbcManagedConnection(managedConnectionFactory, connection, request);
91         } catch (java.sql.SQLException JavaDoc e) {
92             throw (EISSystemException JavaDoc)new EISSystemException JavaDoc("Could not obtain a physical JDBC connection from the DriverManager").initCause(e);
93         }
94     }
95
96     public boolean equals(Object JavaDoc 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 JavaDoc getLogWriter() {
105         return logWriter;
106     }
107
108     public int hashCode() {
109         return hashCode;
110     }
111
112     public ManagedConnection JavaDoc matchManagedConnections(java.util.Set JavaDoc connectionSet, javax.security.auth.Subject JavaDoc subject, ConnectionRequestInfo JavaDoc connectionInfo) throws javax.resource.ResourceException JavaDoc {
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 JavaDoc out) {
125         logWriter = out;
126     }
127
128     public ResourceAdapter JavaDoc getResourceAdapter() {
129         return null; //TODO: implement this
130
}
131
132     public void setResourceAdapter(ResourceAdapter JavaDoc ra) {
133         //TODO: implement this
134
}
135
136
137 }
138
Popular Tags