001: package com.quadcap.jdbc;
002:
003: /*
004: * Copyright 2003 by Stan Bailes and Quadcap Software.
005: *
006: **/
007:
008: import java.io.File;
009:
010: import java.util.HashMap;
011: import java.util.Map;
012: import java.util.Properties;
013:
014: import java.sql.Connection;
015: import java.sql.Driver;
016: import java.sql.DriverManager;
017: import java.sql.DriverPropertyInfo;
018: import java.sql.SQLException;
019:
020: import com.quadcap.sql.Version;
021:
022: import com.quadcap.io.dir.ClassLoader;
023: import com.quadcap.io.dir.Directory;
024:
025: import com.quadcap.util.Util;
026:
027: /**
028: * This class implements a JDBC driver wrapper which uses a custom
029: * classloader to load a QED driver from a different (generally a previous
030: * version) QED version. This setup permits multiple QED database versions
031: * to coexist happily in the same JVM, to facilitate database migration
032: * and other tasks where multiple versions must be accessed simultaneously.
033: *
034: * <p>The JDBC URL for this driver is of the form:</p>
035: *
036: * <code>jdbc:mqed:<i>database</i>;qed=<i>other-qed-jar</i> [ ;<i>other-props</i> ]
037: * </code>
038: *
039: * <p>In other words, it's a regular QED url, with the subprotocol changed
040: * from <code>qed</code> to <code>mqed</code>, and with the additional
041: * connection property <code>qed=<i>other-qed-jar</i></code>.</p>
042: *
043: * @author Stan Bailes
044: */
045: public class MultiDriver implements Driver {
046: static Map versions = new HashMap();
047:
048: static {
049: try {
050: DriverManager.registerDriver(new MultiDriver());
051: } catch (Throwable t) {
052: }
053: }
054:
055: /**
056: * Default constructor
057: */
058: public MultiDriver() {
059: }
060:
061: /**
062: * Connect to the database indicated by the specified URL and properties
063: *
064: */
065: public Connection connect(String url, Properties info)
066: throws SQLException {
067: try {
068: Properties p = new Properties();
069: p.putAll(info);
070: int idx = url.indexOf(';');
071: if (idx >= 0) {
072: p.putAll(Util.parsePropsString(url.substring(idx + 1)));
073: }
074: String qed = p.getProperty("qed");
075: if (qed == null) {
076: throw new SQLException(
077: "MultiDriver requires 'qed' property");
078: }
079: Driver qdriver = (Driver) versions.get(qed);
080: if (qdriver == null) {
081: ClassLoader cl;
082: cl = new ClassLoader(Directory.getDirectory(new File(
083: qed)));
084: Class qdclass = cl
085: .loadClass("com.quadcap.jdbc.JdbcDriver");
086: qdriver = (Driver) (qdclass.newInstance());
087: versions.put(qed, qdriver);
088: }
089: String xurl = "jdbc:qed:"
090: + url.substring("jdbc:mqed:".length());
091: return qdriver.connect(xurl, info);
092: } catch (SQLException ex) {
093: throw ex;
094: } catch (Throwable t) {
095: throw new SQLException(t.toString());
096: }
097: }
098:
099: public boolean acceptsURL(String url) throws SQLException {
100: return url.startsWith("jdbc:mqed:");
101: }
102:
103: public DriverPropertyInfo[] getPropertyInfo(String url,
104: Properties info) throws SQLException {
105: return null;
106: }
107:
108: public int getMajorVersion() {
109: return Version.majorVersion;
110: }
111:
112: public int getMinorVersion() {
113: return Version.minorVersion;
114: }
115:
116: public boolean jdbcCompliant() {
117: return true;
118: }
119: }
|