KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc_xa > XADataSourceImpl


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XADataSourceImpl.java,v 1.14 2005/06/27 11:31:29 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.jdbc_xa;
28
29 import java.io.PrintWriter JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.DriverManager JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import javax.sql.XAConnection JavaDoc;
34 import javax.sql.XADataSource JavaDoc;
35 import org.objectweb.jonas.common.Log;
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * The XADataSource implementation in our pseudo JDBC-XA driver.
41  * Acts as a factory for XAConnection objects.
42  * Created by our DataSource implementation, in the JOnAS Server.
43  * There is one XADataSource for each DataSource JDBC which do not have
44  * this kind of driver in standard (all databases today!)
45  *
46  * @author Philippe Durieux
47  * Contributor(s):
48  */

49 public class XADataSourceImpl implements XADataSource JavaDoc {
50
51     private static Logger logger = null;
52
53     // properties
54
private String JavaDoc dataSourceName = null;
55     private String JavaDoc url = null;
56     private String JavaDoc className = null;
57     private String JavaDoc userName = null;
58     private String JavaDoc password = null;
59     private int isolationLevel = -1;
60
61     // private
62
private int loginTimeout = 60;
63     private PrintWriter JavaDoc log = null;
64     private Class JavaDoc driverClass = null;
65
66     // -----------------------------------------------------------------
67
// Constructors
68
// -----------------------------------------------------------------
69

70     /**
71      * Constructor for Factory
72      */

73     public XADataSourceImpl() {
74     }
75
76     // -----------------------------------------------------------------
77
// Properties
78
// -----------------------------------------------------------------
79
public String JavaDoc getDataSourceName() {
80         return dataSourceName;
81     }
82     public void setDataSourceName(String JavaDoc s) {
83         logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX);
84         dataSourceName = s;
85     }
86
87     public String JavaDoc getUrl() {
88         return url;
89     }
90     public void setUrl(String JavaDoc s) {
91         url = s;
92     }
93
94     public String JavaDoc getClassName() {
95         return className;
96     }
97
98     public void setClassName(String JavaDoc s) throws ClassNotFoundException JavaDoc {
99         className = s;
100
101         // Loads standard JDBC driver and keeps it loaded (via driverClass)
102
if (logger.isLoggable(BasicLevel.DEBUG)) {
103             logger.log(BasicLevel.DEBUG, "Load JDBC driver " + s);
104         }
105         try {
106             driverClass = Class.forName(className);
107         } catch (java.lang.ClassNotFoundException JavaDoc e) {
108             logger.log(BasicLevel.ERROR, "Cannot load JDBC driver : " + e);
109             throw e;
110         }
111     }
112
113     public String JavaDoc getUserName() {
114         return userName;
115     }
116     public void setUserName(String JavaDoc s) {
117         userName = s;
118     }
119
120     public String JavaDoc getPassword() {
121         return password;
122     }
123     public void setPassword(String JavaDoc s) {
124         password = s;
125     }
126
127     public void setTransactionIsolation(int level) {
128         isolationLevel = level;
129     }
130
131     // -----------------------------------------------------------------
132
// XADataSource implementation
133
// -----------------------------------------------------------------
134

135     /**
136      * Attempt to establish a database connection.
137      *
138      * @return a Connection to the database
139      *
140      * @exception SQLException if a database-access error occurs.
141      */

142     public XAConnection JavaDoc getXAConnection() throws SQLException JavaDoc {
143         return getXAConnection(userName, password);
144     }
145
146
147     /**
148      * Attempt to establish a database connection.
149      *
150      * @param user The database user on whose behalf the Connection is being made
151      *
152      * @param passwd the user's password
153      *
154      * @return a Connection to the database
155      *
156      * @exception SQLException if a database-access error occurs.
157      */

158     public XAConnection JavaDoc getXAConnection(String JavaDoc user, String JavaDoc passwd) throws SQLException JavaDoc {
159
160         // Create the actual connection in the std driver
161
Connection JavaDoc conn = null;
162         try {
163             if (user.length() == 0) {
164                 conn = DriverManager.getConnection(url);
165                 if (logger.isLoggable(BasicLevel.DEBUG)) {
166                     logger.log(BasicLevel.DEBUG, " * New Connection on " + url);
167                 }
168             } else {
169                 // Accept password of zero length.
170
conn = DriverManager.getConnection(url, user, passwd);
171                 if (logger.isLoggable(BasicLevel.DEBUG)) {
172                     logger.log(BasicLevel.DEBUG, " * New Connection on " + url + " for " + user);
173                 }
174             }
175         } catch (SQLException JavaDoc e) {
176             logger.log(BasicLevel.ERROR, "Could not get Connection on " + url + ":", e);
177             throw new SQLException JavaDoc("Could not get Connection on url : " + url
178                                    + " for user : " + user + " inner exception" + e.getMessage());
179         }
180
181         // Attempt to set the transaction isolation level
182
// Depending on the underlaying database, this may not succeed.
183
if (isolationLevel != -1) {
184             try {
185                 if (logger.isLoggable(BasicLevel.DEBUG)) {
186                     logger.log(BasicLevel.DEBUG, "set transaction isolation to " + isolationLevel);
187                 }
188                 conn.setTransactionIsolation(isolationLevel);
189             } catch (SQLException JavaDoc e) {
190                 String JavaDoc ilstr = "?";
191                 switch (isolationLevel) {
192                 case Connection.TRANSACTION_SERIALIZABLE:
193                     ilstr = "SERIALIZABLE";
194                     break;
195                 case Connection.TRANSACTION_NONE:
196                     ilstr = "NONE";
197                     break;
198                 case Connection.TRANSACTION_READ_COMMITTED:
199                     ilstr = "READ_COMMITTED";
200                     break;
201                 case Connection.TRANSACTION_READ_UNCOMMITTED:
202                     ilstr = "READ_UNCOMMITTED";
203                     break;
204                 case Connection.TRANSACTION_REPEATABLE_READ:
205                     ilstr = "REPEATABLE_READ";
206                     break;
207                 }
208                 logger.log(BasicLevel.ERROR, "Cannot set transaction isolation to " + ilstr + " for this DataSource:" + e);
209                 logger.log(BasicLevel.ERROR, url);
210                 isolationLevel = -1;
211             }
212         }
213
214         // Create the XAConnection object
215
XAConnectionImpl xac = new XAConnectionImpl(conn, this);
216
217         // return the XAConnection
218
return (XAConnection JavaDoc) xac;
219     }
220
221     /**
222      * Get the log writer for this data source.
223      *
224      * @return PrintWritert he log writer for this data source, null if disabled
225      *
226      * @exception SQLException if a database-access error occurs.
227      */

228     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
229         return log;
230     }
231
232     /**
233      * Set the log writer for this data source.
234      *
235      * @param out the new log writer; to disable, set to null
236      *
237      * @exception SQLException if a database-access error occurs.
238      */

239     public void setLogWriter(PrintWriter JavaDoc out) throws SQLException JavaDoc {
240         log = out;
241     }
242
243
244     /**
245      * Sets the maximum time in seconds that this data source will wait while attempting
246      * to connect to a database.
247      *
248      * @param seconds the data source login time limit
249      *
250      * @exception SQLException if a database-access error occurs.
251      */

252     public void setLoginTimeout(int seconds) throws SQLException JavaDoc {
253         loginTimeout = seconds;
254     }
255
256     /**
257      * Gets the maximum time in seconds that this data source can wait
258      * while attempting to connect to a database.
259      *
260      * @return the data source login time limit
261      *
262      * @exception SQLException if a database-access error occurs.
263      */

264     public int getLoginTimeout() throws SQLException JavaDoc {
265         return loginTimeout;
266     }
267
268 }
269
Popular Tags