KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > jdbc > JdbcDriver


1 package com.quadcap.jdbc;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.Properties JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 import java.sql.Driver JavaDoc;
50 import java.sql.DriverManager JavaDoc;
51 import java.sql.DriverPropertyInfo JavaDoc;
52 import java.sql.SQLException JavaDoc;
53
54 import com.quadcap.sql.Cursor;
55 import com.quadcap.sql.Database;
56 import com.quadcap.sql.QedResultSet;
57 import com.quadcap.sql.QDriver;
58 import com.quadcap.sql.Session;
59 import com.quadcap.sql.Version;
60
61 import com.quadcap.util.Config;
62 import com.quadcap.util.ConfigNumber;
63 import com.quadcap.util.Debug;
64 import com.quadcap.util.Util;
65
66 /**
67  * This class implements the <code>java.sql.Driver</code> interface,
68  * which provides a basic mechanism for establishing connections to
69  * a QED database. A driver can support multiple simultaneously open
70  * databases -- the database URL specifies a file name which is the
71  * root directory of the database, and which is used to select the
72  * correct database.
73  *
74  * @author Stan Bailes
75  */

76 public class JdbcDriver implements QDriver {
77     /*{com.quadcap.qed.Trace-vars.xml-0}
78      *
79      * <config>
80      */

81
82     /*{com.quadcap.qed.Trace-vars.xml-999999}
83      *
84      * </config>
85      */

86
87     /*{com.quadcap.qed.Trace-vars.xml-1050}
88      * <config-var>
89      * <config-name>qed.trace.JdbcDriver</config-name>
90      * <config-dflt>0</config-dflt>
91      * <config-desc>
92      * <pre>
93      * bit 0: API execution
94      * bit 1: Database open/close
95      * </pre>
96      * </config-desc>
97      * </config-var>
98      */

99     //#ifdef DEBUG
100
static final ConfigNumber trace =
101     ConfigNumber.find("qed.trace.JdbcDriver", "0");
102     //#endif
103

104     static Hashtable JavaDoc dbs = new Hashtable JavaDoc();
105
106     public static JdbcDriver jdbcDriver = null;
107
108     static {
109     try {
110         DriverManager.registerDriver(jdbcDriver = new JdbcDriver());
111     } catch (SQLException JavaDoc e) {
112         Debug.print(e);
113     }
114     }
115
116     /**
117      * No-argument constructor
118      */

119     public JdbcDriver() {
120     }
121     
122     /**
123      * Make a new <code>Connection</code> for the specified database
124      * and user.
125      * @deprecated <i>Pay no attention to that man behind the curtains.</i>
126      *
127      * @param db the database
128      * @param auth the userid
129      */

130     public java.sql.Connection JavaDoc makeConnection(Database db, String JavaDoc auth,
131                                               String JavaDoc passwd)
132         throws SQLException JavaDoc
133     {
134         //#ifdef DEBUG
135
if (trace.bit(0)) {
136             Debug.println("JdbcDriver.makeConnection()");
137         }
138         //#endif
139
return new Connection(db, auth, passwd);
140     }
141
142     /**
143      * Make a <code>QedResultSet</code> to wrap the internal database
144      * cursor.
145      * @deprecated <i>Pay no attention to that man behind the curtains.</i>
146      *
147      * @param c the cursor
148      * @return the resultset
149      */

150     public QedResultSet makeResultSet(Cursor c) {
151     return new ResultSet(c);
152     }
153
154     /**
155      * Return true if this is a QED JDBC URL. QED URLS are of the
156      * form <code>jdbc:qed:<i>database</i></code>.
157      *
158      * @param url a URL
159      * @return true if <code>url</code> is a QED JDBC URL.
160      */

161     public boolean acceptsURL(String JavaDoc url) {
162     if (url.startsWith("jdbc:")) {
163         url = url.substring(5);
164         int idx = url.indexOf(':');
165         if (idx > 0) {
166         String JavaDoc protocol = url.substring(0, idx);
167         if (protocol.equals("quadcap")) return true;
168         if (protocol.equals("qed")) return true;
169         }
170     }
171     return false;
172     }
173
174     /**
175      * Assuming that <code>url</code> is a QED URL, return a new
176      * <code>Connection</code> object that can be used to access the
177      * database specified by that url.
178      *
179      * @param url a JDBC URL. If the URL string contains a semicolon,
180      * then the URL proper is interpreted as the portion of the
181      * string preceeding the semicolon, and the remaining string
182      * is treated as a semi-colon separated list of 'name=value'
183      * connection properties. E.g., <code>jdbc:qed:db1;create=true</code>
184      *
185      * @param props a set of connection properties.
186      * @return a new Connection
187      * @exception SQLException may be thrown
188      */

189     public java.sql.Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc props)
190     throws SQLException JavaDoc
191     {
192         //#ifdef DEBUG
193
if (trace.bit(0)) {
194             Debug.println("JdbcDriver.connect(" + url + ", " + props + ")");
195         }
196         //#endif
197
if (!acceptsURL(url)) return null;
198     String JavaDoc origUrl = url;
199     url = url.substring(5);
200         int idx = url.indexOf(';');
201         if (idx >= 0) {
202             String JavaDoc extraProps = url.substring(idx+1);
203             url = url.substring(0, idx);
204             Properties JavaDoc props2 = new Properties JavaDoc(Config.getPropSubset("qed.*"));
205             props2.putAll(props);
206             props2.putAll(Util.parsePropsString(extraProps));
207             props = props2;
208         }
209
210     idx = url.indexOf(':');
211     String JavaDoc param = url.substring(idx+1);
212         
213     Database db = null;
214         try {
215             param = new File JavaDoc(param).getCanonicalPath();
216             synchronized (dbs) {
217                 db = (Database)dbs.get(param);
218                 if (db == null) {
219                     //#ifdef DEBUG
220
if (trace.bit(1)) {
221                         Debug.println("Open database " + dbs.size() + ": " + param);
222                     }
223                     //#endif
224
db = new Database();
225                     db.init(this, origUrl, param, props);
226                     db.driverLock = dbs;
227                     dbs.put(param, db);
228                 }
229                 db.addConnection();
230             }
231         } catch (IOException JavaDoc e) {
232             Debug.print(e);
233             throw new SQLException JavaDoc(e.toString(), "Q000Z");
234         }
235         
236     String JavaDoc auth = props.getProperty("user");
237         String JavaDoc pass = props.getProperty("passwd");
238         Connection conn = null;
239         try {
240             conn = new Connection(db, auth, pass);
241         } finally {
242             // in case an (auth?) exception is thrown, we need to avoid
243
// leaking the connection.
244
if (conn == null) {
245                 synchronized (dbs) {
246                     db.removeConnection();
247                 }
248             }
249         }
250         return conn;
251     }
252
253     /**
254      * Return the driver major version number.
255      *
256      * @return the driver's major version number
257      */

258     public int getMajorVersion() { return Version.majorVersion; }
259
260     /**
261      * Return the driver minor version number.
262      *
263      * @return the driver's minor version number
264      */

265     public int getMinorVersion() { return Version.minorVersion; }
266     
267     /**
268      * Return an array of <code>DriverPropertyInfo</code> objects
269      * that describe the properties required to connect to a
270      * QED database. Currently no properties are required except
271      * <code>user</code>
272      *
273      * @param url the database url
274      * @param info the properties the client has so far.
275      * @return the required connect properties
276      */

277     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url,
278                         Properties JavaDoc info)
279     throws SQLException JavaDoc
280     {
281     int cnt = 0;
282     if (info == null || info.get("user") == null) cnt++;
283     DriverPropertyInfo JavaDoc[] ret = new DriverPropertyInfo JavaDoc[cnt];
284     if (cnt != 0) {
285         ret[0] = new DriverPropertyInfo JavaDoc("user", null);
286         ret[0].required = true;
287     }
288     return ret;
289     }
290     
291     /**
292      * QED is a JDBC compliant driver
293      *
294      * @return true
295      */

296     public boolean jdbcCompliant() {
297     return true; // since 3.1, we're declaring success...
298
}
299
300     /**
301      * Close the specified database.
302      * @deprecated <i>Pay no attention to that man behind the curtains.</i>
303      *
304      * @param name the name of the database.
305      */

306     public void closeDatabase(final String JavaDoc name) {
307         //#ifdef DEBUG
308
if (trace.bit(1)) {
309             Debug.println("closeDatabase(" + name + ")");
310         }
311         //#endif
312
synchronized (dbs) {
313             Database db = (Database)dbs.get(name);
314             if (db != null) {
315                 try {
316                     db.close();
317                 } finally {
318                     dbs.remove(name);
319                 }
320             }
321         }
322         //#ifdef DEBUG
323
if (trace.bit(1)) {
324             Debug.println("closeDatabase(" + name + ") complete");
325         }
326         //#endif
327
}
328         
329     /**
330      * Enumerate the currently open databases
331      * @deprecated <i>Pay no attention to that man behind the curtains.</i>.
332      *
333      * @return an enumeration of the names of the currently open databases
334      */

335     public Enumeration JavaDoc getDatabaseNames() {
336     return dbs.keys();
337     }
338     
339     /**
340      * Locate a specific database by name
341      * @deprecated <i>Pay no attention to that man behind the curtains.</i>.
342      *
343      * @param name the name of the database
344      * @return the database.
345      */

346     public Database getDatabase(String JavaDoc name) {
347         synchronized (dbs) {
348             return (Database)dbs.get(name);
349         }
350     }
351     
352     /**
353      * Close all databases (presumably at program exit)
354      */

355     public static void closeAll() {
356         synchronized (dbs) {
357             Enumeration JavaDoc e = JdbcDriver.dbs.elements();
358             while (e.hasMoreElements()) {
359                 Database db = (Database)e.nextElement();
360                 try {
361                     db.close();
362                 } catch (Throwable JavaDoc t) {
363                 }
364             }
365             dbs.clear();
366         }
367     }
368
369     /**
370      * Return the session for this connection
371      */

372     public Session getSession(java.sql.Connection JavaDoc conn) {
373         try {
374             QDriver d = (QDriver)DriverManager.getDriver("jdbc:qed");
375             return d.getSession(conn);
376         } catch (SQLException JavaDoc ex) {
377             return null;
378         }
379     }
380 }
381
Popular Tags