KickJava   Java API By Example, From Geeks To Geeks.

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


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: JdbcConnectionFactory.java 2096 2005-08-23 20:10:37Z dblevins $
44  */

45 package org.openejb.resource.jdbc;
46
47 import javax.naming.Reference JavaDoc;
48 import javax.resource.ResourceException JavaDoc;
49 import javax.resource.spi.ApplicationServerInternalException JavaDoc;
50 import javax.resource.spi.ConnectionManager JavaDoc;
51 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
52 import javax.resource.spi.ResourceAdapterInternalException JavaDoc;
53 import javax.resource.spi.ResourceAllocationException JavaDoc;
54 import java.io.PrintWriter JavaDoc;
55 import java.sql.Connection JavaDoc;
56 import java.sql.SQLException JavaDoc;
57
58 /**
59  * As a connection factory the JdbcConnecitonFactory must implement the
60  * Serializable and Referenceable methods so that it can be store in a
61  * JNDI name space. The reference itself is an application specific object
62  * that can be used to lookup and configure a new ManagedConnectionFactory
63  * the JdbcConnecitonFactory is only a store for this reference, its not
64  * expected to be functional after it has been serialized into a JNDI
65  * namespace.
66  * <p/>
67  * See section 10.5.3 of the J2EE Connector Architecture 1.0 spec.
68  */

69 public class JdbcConnectionFactory implements javax.sql.DataSource JavaDoc, javax.resource.Referenceable JavaDoc, java.io.Serializable JavaDoc {
70     /**
71      * A Reference to this ConnectionFactory in JNDI
72      */

73     private Reference JavaDoc jndiReference;
74     
75     private final transient ManagedConnectionFactory JavaDoc managedConnectionFactory;
76     private final transient ConnectionManager JavaDoc connectionManager;
77     private final String JavaDoc jdbcUrl;
78     private final String JavaDoc jdbcDriver;
79     private final String JavaDoc defaultPassword;
80     private final String JavaDoc defaultUserName;
81     private transient PrintWriter JavaDoc logWriter;
82     private int logTimeout = 0;
83
84     public JdbcConnectionFactory(ManagedConnectionFactory JavaDoc managedConnectionFactory,
85                                  ConnectionManager JavaDoc connectionManager, String JavaDoc jdbcUrl,
86                                  String JavaDoc jdbcDriver, String JavaDoc defaultPassword, String JavaDoc defaultUserName) throws ResourceException JavaDoc {
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     /**
97      * setReference is called by deployment code
98      *
99      * @param jndiReference
100      */

101     public void setReference(Reference JavaDoc jndiReference) {
102         this.jndiReference = jndiReference;
103     }
104
105     /**
106      * getReference is called by JNDI provider during Context.bind
107      *
108      * @return
109      */

110     public Reference JavaDoc getReference() {
111         return jndiReference;
112     }
113
114     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
115         return getConnection(defaultUserName, defaultPassword);
116     }
117
118     public Connection JavaDoc getConnection(java.lang.String JavaDoc username, java.lang.String JavaDoc password) throws SQLException JavaDoc {
119         return getConnection(new JdbcConnectionRequestInfo(username, password, jdbcDriver, jdbcUrl));
120     }
121
122     protected Connection JavaDoc getConnection(JdbcConnectionRequestInfo connectionRequestInfo) throws SQLException JavaDoc {
123         // TODO: Use ManagedConnection.assocoate() method here if the client has already obtained a physical connection.
124
// the previous connection is either shared or invalidated. IT should probably be shared.
125
try {
126             return (Connection JavaDoc) connectionManager.allocateConnection(managedConnectionFactory, connectionRequestInfo);
127         } catch (ApplicationServerInternalException JavaDoc e) {
128             throw convertToSQLException(e, "Application error in ContainerManager");
129         } catch (javax.resource.spi.SecurityException JavaDoc e) {
130             throw convertToSQLException(e, "Authentication error. Invalid credentials");
131         } catch (ResourceAdapterInternalException JavaDoc e) {
132             throw convertToSQLException(e, "JDBC Connection problem");
133         } catch (ResourceAllocationException JavaDoc e) {
134             throw convertToSQLException(e, "JDBC Connection could not be obtained");
135         } catch (ResourceException JavaDoc e) {
136             throw convertToSQLException(e, "JDBC Connection Factory problem");
137         }
138     }
139
140     private SQLException JavaDoc convertToSQLException(ResourceException JavaDoc e, String JavaDoc error) {
141         Throwable JavaDoc cause = e.getCause();
142         if (cause instanceof SQLException JavaDoc) {
143             return (SQLException JavaDoc) cause;
144         } else {
145             String JavaDoc message = ((cause != null) ? cause.getMessage() : "");
146             return (SQLException JavaDoc) new SQLException JavaDoc("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 JavaDoc getLogWriter() {
155         return logWriter;
156     }
157
158     public void setLoginTimeout(int seconds) {
159         //TODO: how should log timeout work?
160
logTimeout = seconds;
161     }
162
163     public void setLogWriter(java.io.PrintWriter JavaDoc out) {
164         logWriter = out;
165     }
166 }
167
Popular Tags