KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > spi > DataSource


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 package com.sun.gjc.spi;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
30 import javax.resource.spi.ConnectionManager JavaDoc;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.naming.Reference JavaDoc;
33 import com.sun.gjc.spi.ConnectionHolder.ConnectionType;
34
35 import com.sun.logging.*;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import com.sun.enterprise.connectors.ConnectionManagerImpl;
39 import com.sun.enterprise.connectors.ConnectorConstants;
40
41 /**
42  * Holds the <code>java.sql.Connection</code> object, which is to be
43  * passed to the application program.
44  *
45  * @version 1.0, 02/07/31
46  * @author Binod P.G
47  */

48 public class DataSource implements javax.sql.DataSource JavaDoc, java.io.Serializable JavaDoc,
49         com.sun.appserv.jdbc.DataSource, javax.resource.Referenceable JavaDoc{
50                    
51     private ManagedConnectionFactory mcf;
52     private ConnectionManager cm;
53     private int loginTimeout;
54     private PrintWriter JavaDoc logWriter;
55     private String JavaDoc description;
56     private Reference JavaDoc reference;
57     
58     private ConnectionType conType_;
59
60     private static Logger JavaDoc _logger;
61     static {
62         _logger = LogDomains.getLogger( LogDomains.RSR_LOGGER );
63     }
64     private boolean debug = false;
65
66     /**
67      * Constructs <code>DataSource</code> object. This is created by the
68      * <code>ManagedConnectionFactory</code> object.
69      *
70      * @param mcf <code>ManagedConnectionFactory</code> object
71      * creating this object.
72      * @param cm <code>ConnectionManager</code> object either associated
73      * with Application server or Resource Adapter.
74      */

75     public DataSource (ManagedConnectionFactory mcf, ConnectionManager cm) {
76         this.mcf = mcf;
77         if (cm == null) {
78             this.cm = (ConnectionManager) new com.sun.gjc.spi.ConnectionManager();
79         } else {
80             this.cm = cm;
81             conType_ = findConnectionType();
82         }
83     }
84     
85     /**
86      * Retrieves the <code> Connection </code> object.
87      *
88      * @return <code> Connection </code> object.
89      * @throws SQLException In case of an error.
90      */

91     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
92         try {
93             ConnectionHolder con = (ConnectionHolder)
94                 cm.allocateConnection( mcf, null );
95             setConnectionType( con );
96
97             return (Connection JavaDoc) con;
98         } catch (ResourceException JavaDoc re) {
99         _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
100             throw new SQLException JavaDoc (re.getMessage());
101         }
102     }
103     
104     /**
105      * Retrieves the <code> Connection </code> object.
106      *
107      * @param user User name for the Connection.
108      * @param pwd Password for the Connection.
109      * @return <code> Connection </code> object.
110      * @throws SQLException In case of an error.
111      */

112     public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc pwd) throws SQLException JavaDoc {
113         try {
114             ConnectionRequestInfo info = new ConnectionRequestInfo (user, pwd);
115             ConnectionHolder con = (ConnectionHolder)
116                 cm.allocateConnection(mcf,info);
117             setConnectionType( con );
118             return (Connection JavaDoc) con;
119         } catch (ResourceException JavaDoc re) {
120         _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
121             throw new SQLException JavaDoc (re.getMessage());
122         }
123     }
124
125     /**
126      * Retrieves the actual SQLConnection from the Connection wrapper
127      * implementation of SunONE application server. If an actual connection is
128      * supplied as argument, then it will be just returned.
129      *
130      * @param con Connection obtained from <code>Datasource.getConnection()</code>
131      * @return <code>java.sql.Connection</code> implementation of the driver.
132      * @throws <code>java.sql.SQLException</code> If connection cannot be obtained.
133      */

134     public Connection JavaDoc getConnection(Connection JavaDoc con) throws SQLException JavaDoc {
135
136         Connection JavaDoc driverCon = con;
137         if (con instanceof com.sun.gjc.spi.ConnectionHolder) {
138            driverCon = ((com.sun.gjc.spi.ConnectionHolder) con).getConnection();
139         }
140
141         return driverCon;
142     }
143    
144     /**
145      * Gets a connection that is not in the scope of any transaction. This
146      * can be used to save performance overhead incurred on enlisting/delisting
147      * each connection got, irrespective of whether its required or not.
148      * Note here that this meethod does not fit in the connector contract
149      * per se.
150      *
151      * @return <code>java.sql.Connection</code>
152      * @throws <code>java.sql.SQLException</code> If connection cannot be obtained
153      */

154     public Connection JavaDoc getNonTxConnection() throws SQLException JavaDoc {
155         try {
156             ConnectionHolder con = (ConnectionHolder)
157                 ((com.sun.enterprise.connectors.ConnectionManagerImpl)
158                 cm).allocateNonTxConnection(mcf, null);
159             setConnectionType( con, true );
160             
161             return (Connection JavaDoc) con;
162         } catch( ResourceException JavaDoc re ) {
163             _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
164             throw new SQLException JavaDoc( re.getMessage() );
165         }
166     }
167
168     /**
169      * Gets a connection that is not in the scope of any transaction. This
170      * can be used to save performance overhead incurred on enlisting/delisting
171      * each connection got, irrespective of whether its required or not.
172      * Note here that this meethod does not fit in the connector contract
173      * per se.
174      *
175      * @param user User name for authenticating the connection
176      * @param password Password for authenticating the connection
177      * @return <code>java.sql.Connection</code>
178      * @throws <code>java.sql.SQLException</code> If connection cannot be obtained
179      */

180     public Connection JavaDoc getNonTxConnection(String JavaDoc user, String JavaDoc password) throws SQLException JavaDoc {
181         try {
182             ConnectionRequestInfo cxReqInfo = new ConnectionRequestInfo(user, password);
183             ConnectionHolder con = (ConnectionHolder)
184                 ((com.sun.enterprise.connectors.ConnectionManagerImpl)
185             cm).allocateNonTxConnection( mcf, cxReqInfo);
186
187             setConnectionType( con, true );
188             
189             return (Connection JavaDoc) con;
190         } catch( ResourceException JavaDoc re ) {
191             _logger.log(Level.WARNING, "jdbc.exc_get_conn", re.getMessage());
192             throw new SQLException JavaDoc( re.getMessage() );
193         }
194     }
195    
196     /**
197      * Get the login timeout
198      *
199      * @return login timeout.
200      * @throws SQLException If a database error occurs.
201      */

202     public int getLoginTimeout() throws SQLException JavaDoc{
203         return loginTimeout;
204     }
205     
206     /**
207      * Set the login timeout
208      *
209      * @param loginTimeout Login timeout.
210      * @throws SQLException If a database error occurs.
211      */

212     public void setLoginTimeout(int loginTimeout) throws SQLException JavaDoc{
213         this.loginTimeout = loginTimeout;
214     }
215     
216     /**
217      * Get the logwriter object.
218      *
219      * @return <code> PrintWriter </code> object.
220      * @throws SQLException If a database error occurs.
221      */

222     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc{
223         return logWriter;
224     }
225         
226     /**
227      * Set the logwriter on this object.
228      *
229      * @param <code>PrintWriter</code> object.
230      * @throws SQLException If a database error occurs.
231      */

232     public void setLogWriter(PrintWriter JavaDoc logWriter) throws SQLException JavaDoc{
233         this.logWriter = logWriter;
234     }
235         
236     /**
237      * Retrieves the description.
238      *
239      * @return Description about the DataSource.
240      */

241     public String JavaDoc getDescription() {
242         return description;
243     }
244     
245     /**
246      * Set the description.
247      *
248      * @param description Description about the DataSource.
249      */

250     public void setDescription(String JavaDoc description) {
251         this.description = description;
252     }
253     
254     /**
255      * Get the reference.
256      *
257      * @return <code>Reference</code>object.
258      */

259     public Reference JavaDoc getReference() {
260         return reference;
261     }
262     
263     /**
264      * Get the reference.
265      *
266      * @param reference <code>Reference</code> object.
267      */

268     public void setReference(Reference JavaDoc reference) {
269         this.reference = reference;
270     }
271
272     private ConnectionType findConnectionType() {
273         ConnectionType cmType = ConnectionType.STANDARD;
274         
275         if ( cm instanceof javax.resource.spi.LazyAssociatableConnectionManager JavaDoc ) {
276             if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
277                     getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX ) ) {
278                 cmType = ConnectionType.LAZY_ASSOCIATABLE;
279             }
280         } else if ( cm instanceof
281                 javax.resource.spi.LazyEnlistableConnectionManager JavaDoc ) {
282             if (! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
283                     getJndiName().endsWith( ConnectorConstants.PM_JNDI_SUFFIX) &&
284                 ! ((com.sun.enterprise.connectors.ConnectionManagerImpl)cm).
285                     getJndiName().endsWith( ConnectorConstants.NON_TX_JNDI_SUFFIX)) {
286                 cmType = ConnectionType.LAZY_ENLISTABLE;
287             }
288         }
289
290         return cmType;
291     }
292
293     private void setConnectionType( ConnectionHolder con ) {
294         this.setConnectionType( con, false);
295     }
296     
297     private void setConnectionType( ConnectionHolder con, boolean isNonTx ) {
298         con.setConnectionType( conType_ );
299         if ( conType_ == ConnectionType.LAZY_ASSOCIATABLE ) {
300             con.setLazyAssociatableConnectionManager(
301                 (javax.resource.spi.LazyAssociatableConnectionManager JavaDoc) cm);
302         } else if (conType_ == ConnectionType.LAZY_ENLISTABLE) {
303             if ( isNonTx ) {
304                 //if this is a getNonTxConnection call on the DataSource, we
305
//should not LazyEnlist
306
con.setConnectionType( ConnectionType.STANDARD );
307             } else {
308                 con.setLazyEnlistableConnectionManager(
309                     (javax.resource.spi.LazyEnlistableConnectionManager JavaDoc) cm );
310             }
311         }
312     }
313
314 }
315
Popular Tags