Quadcap Embeddable Database

com/quadcap/jdbc/MultiDriver.java

Go to the documentation of this file.
00001 package com.quadcap.jdbc; 00002 00003 /* 00004 * Copyright 2003 by Stan Bailes and Quadcap Software. 00005 * 00006 **/ 00007 00008 import java.io.File; 00009 00010 import java.util.HashMap; 00011 import java.util.Map; 00012 import java.util.Properties; 00013 00014 import java.sql.Connection; 00015 import java.sql.Driver; 00016 import java.sql.DriverManager; 00017 import java.sql.DriverPropertyInfo; 00018 import java.sql.SQLException; 00019 00020 import com.quadcap.sql.Version; 00021 00022 import com.quadcap.io.dir.ClassLoader; 00023 import com.quadcap.io.dir.Directory; 00024 00025 import com.quadcap.util.Util; 00026 00027 /** 00028 * This class implements a JDBC driver wrapper which uses a custom 00029 * classloader to load a QED driver from a different (generally a previous 00030 * version) QED version. This setup permits multiple QED database versions 00031 * to coexist happily in the same JVM, to facilitate database migration 00032 * and other tasks where multiple versions must be accessed simultaneously. 00033 * 00034 * <p>The JDBC URL for this driver is of the form:</p> 00035 * 00036 * <code>jdbc:mqed:<i>database</i>;qed=<i>other-qed-jar</i> [ ;<i>other-props</i> ] 00037 * </code> 00038 * 00039 * <p>In other words, it's a regular QED url, with the subprotocol changed 00040 * from <code>qed</code> to <code>mqed</code>, and with the additional 00041 * connection property <code>qed=<i>other-qed-jar</i></code>.</p> 00042 * 00043 * @author Stan Bailes 00044 */ 00045 public class MultiDriver implements Driver { 00046 static Map versions = new HashMap(); 00047 00048 static { 00049 try { 00050 DriverManager.registerDriver(new MultiDriver()); 00051 } catch (Throwable t) {} 00052 } 00053 00054 /** 00055 * Default constructor 00056 */ 00057 public MultiDriver() {} 00058 00059 /** 00060 * Connect to the database indicated by the specified URL and properties 00061 * 00062 */ 00063 public Connection connect(String url, Properties info) 00064 throws SQLException 00065 { 00066 try { 00067 Properties p = new Properties(); 00068 p.putAll(info); 00069 int idx = url.indexOf(';'); 00070 if (idx >= 0) { 00071 p.putAll(Util.parsePropsString(url.substring(idx+1))); 00072 } 00073 String qed = p.getProperty("qed"); 00074 if (qed == null) { 00075 throw new SQLException("MultiDriver requires 'qed' property"); 00076 } 00077 Driver qdriver = (Driver)versions.get(qed); 00078 if (qdriver == null) { 00079 ClassLoader cl; 00080 cl = new ClassLoader(Directory.getDirectory(new File(qed))); 00081 Class qdclass = cl.loadClass("com.quadcap.jdbc.JdbcDriver"); 00082 qdriver = (Driver)(qdclass.newInstance()); 00083 versions.put(qed, qdriver); 00084 } 00085 String xurl = "jdbc:qed:" + url.substring("jdbc:mqed:".length()); 00086 return qdriver.connect(xurl, info); 00087 } catch (SQLException ex) { 00088 throw ex; 00089 } catch (Throwable t) { 00090 throw new SQLException(t.toString()); 00091 } 00092 } 00093 00094 public boolean acceptsURL(String url) throws SQLException { 00095 return url.startsWith("jdbc:mqed:"); 00096 } 00097 00098 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) 00099 throws SQLException 00100 { 00101 return null; 00102 } 00103 00104 public int getMajorVersion() { 00105 return Version.majorVersion; 00106 } 00107 00108 public int getMinorVersion() { 00109 return Version.minorVersion; 00110 } 00111 00112 public boolean jdbcCompliant() { 00113 return true; 00114 } 00115 } 00116