KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > jdbc > standard > StandardDataSource


1 /*
2  * XAPool: Open Source XA JDBC Pool
3  * Copyright (C) 2003 Objectweb.org
4  * Initial Developer: Lutris Technologies Inc.
5  * Contact: xapool-public@lists.debian-sf.objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  * USA
21  */

22 package org.enhydra.jdbc.standard;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.Reference JavaDoc;
32 import javax.naming.StringRefAddr JavaDoc;
33 import javax.sql.DataSource JavaDoc;
34 import org.enhydra.jdbc.core.CoreDataSource;
35 import java.sql.Driver JavaDoc;
36 import java.util.Properties JavaDoc;
37 import org.enhydra.jdbc.util.Logger;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Provides a Data Source which can be used to generate JDBC connections.
42  * <P>
43  * This class is generic in the sense that it does not rely upon anything other
44  * than standard Java APIs. It uses java.sql.DriverManager and preconfigured
45  * properties to construct a JDBC connection.
46  * Important : networkProtocol, portNumber, serverName are not used. Please use
47  * instead the url property.
48  */

49 public class StandardDataSource extends CoreDataSource implements DataSource JavaDoc {
50
51     // Standard Data Source properties
52
transient Driver JavaDoc driver;
53     String JavaDoc driverName; // name of the Standard JDBC driver
54
String JavaDoc url; // an explicit JDBC URL used to access this data source
55
private int transIsolation; // transaction isolation level
56
private boolean loadedFromCCL = false;
57
58     /**
59      * Constructors
60      */

61     public StandardDataSource() {
62         // This constructor is needed by the object factory
63
super();
64         driver = null;
65         driverName = "";
66         url = "";
67         transIsolation = -1; //use default
68
setLogger(new Logger(LogFactory.getLog("org.enhydra.jdbc.xapool")));
69     }
70
71     protected StandardDataSource(Driver JavaDoc drv) throws SQLException JavaDoc {
72         this();
73         driver = drv;
74         driverName = drv.getClass().getName();
75                 setLogger(new Logger(LogFactory.getLog("org.enhydra.jdbc.xapool")));
76     }
77
78     /**
79      * return the name of the driver
80      * @return the string representation of the driver name
81      */

82     public String JavaDoc getDriverName() {
83         return driverName;
84     }
85
86     /**
87      * return the url of the database
88      * @return the string representation of the database url
89      */

90     public String JavaDoc getUrl() {
91         return url;
92     }
93
94     /**
95      * set the name of the jdbc driver
96      * @param driverName the string representation of the jdbc driver name
97      * @throws SQLException
98      */

99     public void setDriverName(String JavaDoc driverName) throws SQLException JavaDoc {
100         if (!this.driverName.equals(driverName)) {
101             this.driverName = driverName;
102             driver = null;
103         }
104         /*
105         try {
106         driver= (Driver)Class.forName (driverName).newInstance();
107         log("StandardDataSource:setDriverName a new driver instance is created");
108         } catch (Exception e) {
109         throw new SQLException ("Error trying to load driver: "+driverName+"\n"+e.getMessage());
110         } // try-catch
111         */

112     }
113
114     /**
115      * set the database url
116      * @param url the string representation of the database url
117      */

118     public void setUrl(String JavaDoc url) {
119         this.url = url;
120     }
121
122     /**
123      * set the level of the transaction isolation for the current database
124      * @param level the integer level
125      */

126     public void setTransactionIsolation(int level) {
127         transIsolation = level;
128     }
129
130     /**
131      * return the transaction isolation level defined for the current database
132      * @return the transaction isolation level
133      */

134     public int getTransactionIsolation() {
135         return transIsolation;
136     }
137
138     /**
139      *
140      * @return
141      * @throws SQLException
142      */

143     synchronized public Connection JavaDoc getConnection() throws SQLException JavaDoc {
144         return getConnection(user, password);
145     }
146
147     /**
148      *
149      * @param u
150      * @param p
151      * @return
152      * @throws SQLException
153      */

154     synchronized public Connection JavaDoc getConnection(String JavaDoc u, String JavaDoc p)
155         throws SQLException JavaDoc {
156         Connection JavaDoc ret = null; // the connection that gets returned
157
Properties JavaDoc prop = new Properties JavaDoc();
158         if (u != null)
159             prop.put("user", u);
160         if (p != null)
161             prop.put("password", p);
162         if (url == null) { // if no explicit url provided
163
// Build URL from serverName, NetworkProtocol etc.
164
} else { // explicit URL provided
165
if (driver == null) {
166                 try {
167                     driver = (Driver JavaDoc) Class.forName(driverName).newInstance();
168                     loadedFromCCL = false;
169                     log.debug(
170                         "StandardDataSource:getConnection a new driver instance is created");
171                 } catch (Exception JavaDoc e) {
172                     try {
173                         driver =
174                             (Driver JavaDoc) Class
175                                 .forName(
176                                     driverName,
177                                     true,
178                                     Thread
179                                         .currentThread()
180                                         .getContextClassLoader())
181                                 .newInstance();
182                         loadedFromCCL = true;
183                     } catch (Exception JavaDoc e2) {
184                         throw new SQLException JavaDoc(
185                             "Error trying to load driver: "
186                                 + driverName
187                                 + " : "
188                                 + e2.getMessage());
189                     }
190                 }
191             }
192             // commenting out since at least one driver will complain if you
193
// instantiate the driver outside the Driver Manager
194
// (ie. Cloudscape RMI)
195
/*
196             if (!driver.acceptsURL(url)) {
197             log("Driver does not accept url "+url);
198             throw new SQLException("Driver does not accept url "+url);
199             }
200             */

201             try {
202                 if (loadedFromCCL) {
203                     ret = driver.connect(url, prop);
204                     // Driver creates the connection
205
} else {
206                     ret = DriverManager.getConnection(url, prop);
207                     // DriverManager creates the connection
208
}
209                 int transIsolation = getTransactionIsolation();
210                 if (transIsolation >= 0) {
211                     ret.setTransactionIsolation(transIsolation);
212                 }
213                 log.debug(
214                     "StandardDataSource:getConnection Connection from DriverManager is returned");
215             } catch (SQLException JavaDoc e) {
216                 throw new SQLException JavaDoc(
217                     "Cannot get connection for URL "
218                         + url
219                         + " : "
220                         + e.getMessage());
221             }
222         }
223         return ret;
224     }
225
226     /**
227      * Methods inherited from referenceable
228      */

229     public Reference JavaDoc getReference() throws NamingException JavaDoc {
230         // Note that we use getClass().getName() to provide the factory
231
// class name. It is assumed that this class, and all of its
232
// descendants are their own factories.
233

234         Reference JavaDoc ref =
235             new Reference JavaDoc(getClass().getName(), getClass().getName(), null);
236         ref.add(new StringRefAddr JavaDoc("driverName", getDriverName()));
237         ref.add(new StringRefAddr JavaDoc("url", getUrl()));
238         ref.add(new StringRefAddr JavaDoc("user", getUser()));
239         ref.add(new StringRefAddr JavaDoc("password", getPassword()));
240         ref.add(new StringRefAddr JavaDoc("description", getDescription()));
241         ref.add(
242             new StringRefAddr JavaDoc(
243                 "loginTimeout",
244                 Integer.toString(getLoginTimeout())));
245         ref.add(
246             new StringRefAddr JavaDoc(
247                 "transIsolation",
248                 Integer.toString(getTransactionIsolation())));
249         log.debug("StandardDataSource:getReference object returned");
250         return ref;
251     }
252
253     /**
254      * Methods inherited from ObjectFactory
255      */

256     public Object JavaDoc getObjectInstance(
257         Object JavaDoc refObj,
258         Name JavaDoc name,
259         Context JavaDoc nameCtx,
260         Hashtable JavaDoc env)
261         throws Exception JavaDoc {
262         Reference JavaDoc ref = (Reference JavaDoc) refObj;
263         this.setDriverName((String JavaDoc) ref.get("driverName").getContent());
264         this.setUrl((String JavaDoc) ref.get("url").getContent());
265         this.setUser((String JavaDoc) ref.get("user").getContent());
266         this.setPassword((String JavaDoc) ref.get("password").getContent());
267         this.setDescription((String JavaDoc) ref.get("description").getContent());
268         this.setLoginTimeout(
269             Integer.parseInt((String JavaDoc) ref.get("loginTimeout").getContent()));
270         this.setTransactionIsolation(
271             Integer.parseInt((String JavaDoc) ref.get("transIsolation").getContent()));
272         return this;
273     }
274
275     public boolean equals(Object JavaDoc obj) {
276         if (this == obj) {
277             return true;
278         }
279         if (obj == null) {
280             return false;
281         }
282         return false;
283         /*
284                 if (obj instanceof StandardDataSource) {
285                     StandardDataSource other = (StandardDataSource)obj;
286                     if (!(this.url == null || this.user == null)) {
287                         return (this.url.equals(other.url) && this.user.equals(other.user));
288                     } else if (this.url != null) {
289                         return (this.url.equals(other.url) && this.user == other.user);
290                     } else if (this.user != null) {
291                         return (this.url == other.url && this.user.equals(other.user));
292                     } else {
293                         return (this.url == other.url && this.user == other.user);
294                     }
295                 } else {
296                     return false;
297                 }
298                 */

299     }
300
301     /* (non-Javadoc)
302      * @see java.lang.Object#toString()
303      */

304
305     public String JavaDoc toString() {
306         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
307         sb.append("StandardDataSource:\n");
308         sb.append(" driver=<" + driver +">\n");
309         sb.append(" url=<" + url + ">\n");
310         sb.append(" user=<" + user + ">\n");
311         sb.append(super.toString());
312         
313         return sb.toString();
314     }
315
316     public int hashCode() {
317         return toString().hashCode();
318     }
319 }
Popular Tags