KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.quadcap.jdbc;
2
3 /*
4  * Copyright 2003 by Stan Bailes and Quadcap Software.
5  *
6  **/

7
8 import java.io.File JavaDoc;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.Properties JavaDoc;
13
14 import java.sql.Connection JavaDoc;
15 import java.sql.Driver JavaDoc;
16 import java.sql.DriverManager JavaDoc;
17 import java.sql.DriverPropertyInfo JavaDoc;
18 import java.sql.SQLException JavaDoc;
19
20 import com.quadcap.sql.Version;
21
22 import com.quadcap.io.dir.ClassLoader;
23 import com.quadcap.io.dir.Directory;
24
25 import com.quadcap.util.Util;
26
27 /**
28  * This class implements a JDBC driver wrapper which uses a custom
29  * classloader to load a QED driver from a different (generally a previous
30  * version) QED version. This setup permits multiple QED database versions
31  * to coexist happily in the same JVM, to facilitate database migration
32  * and other tasks where multiple versions must be accessed simultaneously.
33  *
34  * <p>The JDBC URL for this driver is of the form:</p>
35  *
36  * <code>jdbc:mqed:<i>database</i>;qed=<i>other-qed-jar</i> [ ;<i>other-props</i> ]
37  * </code>
38  *
39  * <p>In other words, it's a regular QED url, with the subprotocol changed
40  * from <code>qed</code> to <code>mqed</code>, and with the additional
41  * connection property <code>qed=<i>other-qed-jar</i></code>.</p>
42  *
43  * @author Stan Bailes
44  */

45 public class MultiDriver implements Driver JavaDoc {
46     static Map JavaDoc versions = new HashMap JavaDoc();
47     
48     static {
49         try {
50             DriverManager.registerDriver(new MultiDriver());
51         } catch (Throwable JavaDoc t) {}
52     }
53
54     /**
55      * Default constructor
56      */

57     public MultiDriver() {}
58
59     /**
60      * Connect to the database indicated by the specified URL and properties
61      *
62      */

63     public Connection connect(String JavaDoc url, Properties JavaDoc info)
64         throws SQLException JavaDoc
65     {
66         try {
67             Properties JavaDoc p = new Properties JavaDoc();
68             p.putAll(info);
69             int idx = url.indexOf(';');
70             if (idx >= 0) {
71                 p.putAll(Util.parsePropsString(url.substring(idx+1)));
72             }
73             String JavaDoc qed = p.getProperty("qed");
74             if (qed == null) {
75                 throw new SQLException JavaDoc("MultiDriver requires 'qed' property");
76             }
77             Driver JavaDoc qdriver = (Driver JavaDoc)versions.get(qed);
78             if (qdriver == null) {
79                 ClassLoader JavaDoc cl;
80                 cl = new ClassLoader JavaDoc(Directory.getDirectory(new File JavaDoc(qed)));
81                 Class JavaDoc qdclass = cl.loadClass("com.quadcap.jdbc.JdbcDriver");
82                 qdriver = (Driver JavaDoc)(qdclass.newInstance());
83                 versions.put(qed, qdriver);
84             }
85             String JavaDoc xurl = "jdbc:qed:" + url.substring("jdbc:mqed:".length());
86             return qdriver.connect(xurl, info);
87         } catch (SQLException JavaDoc ex) {
88             throw ex;
89         } catch (Throwable JavaDoc t) {
90             throw new SQLException JavaDoc(t.toString());
91         }
92     }
93
94     public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
95         return url.startsWith("jdbc:mqed:");
96     }
97
98     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
99         throws SQLException JavaDoc
100     {
101         return null;
102     }
103
104     public int getMajorVersion() {
105         return Version.majorVersion;
106     }
107
108     public int getMinorVersion() {
109         return Version.minorVersion;
110     }
111
112     public boolean jdbcCompliant() {
113         return true;
114     }
115 }
116
117
Popular Tags