KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > jdbc > base > DataSourceImpl


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
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
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 in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3.jdbc.base;
23
24 import java.io.PrintWriter JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.DriverManager JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29 import oracle.toplink.essentials.internal.ejb.cmp3.transaction.base.TransactionManagerImpl;
30
31 /**
32  * A stubbed out impl of DataSource that can be used for testing.
33  *
34  * Does not support multiple threads or multiple usernames/passwords.
35  */

36 public class DataSourceImpl implements DataSource JavaDoc {
37     String JavaDoc dsName;
38     String JavaDoc url;
39     String JavaDoc userName;
40     String JavaDoc password;
41
42     // When a transaction is active we need to get the right connection.
43
// This should not be set (will be null) when the data source is non-JTA (non-tx).
44
TransactionManagerImpl tm;
45
46     /************************/
47     /***** Internal API *****/
48     /************************/
49     private void debug(String JavaDoc s) {
50         System.out.println(s);
51     }
52
53     /*
54      * Use this constructor to create a new datasource
55      */

56     public DataSourceImpl(String JavaDoc dsName, String JavaDoc url, String JavaDoc userName, String JavaDoc password) {
57         this.dsName = dsName;
58         this.url = url;
59         this.userName = userName;
60         this.password = password;
61     }
62
63     /*
64      * Return the unique name of this data source
65      */

66     public String JavaDoc getName() {
67         return this.dsName;
68     }
69
70     /*
71      * This must be called right after initialization if data source is transactional.
72      * Must not get set if data source is a non-transactional data source.
73      */

74     public void setTransactionManager(TransactionManagerImpl tm) {
75         this.tm = tm;
76     }
77
78     /*
79      * Get all connections from the DriverManager.
80      */

81     public Connection JavaDoc internalGetConnection(String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
82         return DriverManager.getConnection(this.url, userName, password);
83     }
84
85     /*
86      * Get all connections from the DriverManager.
87      */

88     public Connection JavaDoc internalGetConnection() throws SQLException JavaDoc {
89         return internalGetConnection(this.userName, this.password);
90     }
91
92     /*
93      * Return true if this data source is transactional, false if not
94      */

95     public boolean isTransactional() {
96         return tm != null;
97     }
98
99     /************************************************************/
100     /***** Supported DataSource API *****/
101     /************************************************************/
102
103     /*
104      * Forward to the other method.
105      */

106     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
107         return getConnection(this.userName, this.password);
108     }
109
110     /*
111      * Go to the Transaction Manager to get a connection
112      */

113     public Connection JavaDoc getConnection(String JavaDoc userName, String JavaDoc password) throws SQLException JavaDoc {
114         if (isTransactional() && tm.isTransactionActive()) {
115             // This will actually eventually call back into this class, but allows
116
// the connection to be cached in the transaction first
117
return tm.getConnection(this, userName, password);
118         } else {//{
119
debug("Ds - Allocating new non-tx connection");
120         }
121         return internalGetConnection(userName, password);
122     }
123
124     /*
125      * Forward to the DriverManager.
126      */

127     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
128         return DriverManager.getLogWriter();
129     }
130
131     /*
132      * Forward to the DriverManager.
133      */

134     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
135         DriverManager.setLogWriter(out);
136     }
137
138     /*
139      * Forward to the DriverManager.
140      */

141     public int getLoginTimeout() throws SQLException JavaDoc {
142         return DriverManager.getLoginTimeout();
143     }
144
145     /*
146      * Forward to the DriverManager.
147      */

148     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
149         DriverManager.setLoginTimeout(seconds);
150     }
151 }
152
Popular Tags