01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.jdbcutil;
17:
18: import java.util.ArrayList;
19: import java.util.StringTokenizer;
20: import java.util.Properties;
21: import java.net.URL;
22: import java.net.MalformedURLException;
23: import java.net.URLClassLoader;
24: import java.io.File;
25: import java.sql.*;
26:
27: public class DriverLoader {
28: /**
29: * Registeres a dynamically-loaded JDBC driver.
30: *
31: * @param classpath A comma-separated path of jar files.
32: * @param driverClass the JDBC driver class name
33: */
34: public static void loadDatabaseDriver(String classpath,
35: String driverClass) throws Exception {
36: ArrayList urls = new ArrayList();
37: StringTokenizer tokenizer = new StringTokenizer(classpath, ",");
38: while (tokenizer.hasMoreTokens()) {
39: String filename = tokenizer.nextToken().trim();
40: try {
41: URL url = new File(filename).toURL();
42: urls.add(url);
43: } catch (MalformedURLException e) {
44: throw new Exception("Invalid filename in driver path.",
45: e);
46: }
47: }
48: ClassLoader classLoader = new URLClassLoader((URL[]) urls
49: .toArray(new URL[] {}), Thread.currentThread()
50: .getContextClassLoader());
51: Driver driver = (Driver) Class.forName(driverClass, true,
52: classLoader).newInstance();
53: DriverManager.registerDriver(new DriverShim(driver));
54: }
55:
56: /**
57: * Work-around for the fact that DriverManager will only load Driver's loaded by
58: * the system classloader.
59: *
60: * More information can be found on:
61: * http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
62: */
63: private static final class DriverShim implements Driver {
64: private Driver driver;
65:
66: DriverShim(Driver d) {
67: this .driver = d;
68: }
69:
70: public final boolean acceptsURL(final String u)
71: throws SQLException {
72: return this .driver.acceptsURL(u);
73: }
74:
75: public final Connection connect(final String u,
76: final Properties p) throws SQLException {
77: return this .driver.connect(u, p);
78: }
79:
80: public final int getMajorVersion() {
81: return this .driver.getMajorVersion();
82: }
83:
84: public final int getMinorVersion() {
85: return this .driver.getMinorVersion();
86: }
87:
88: public final DriverPropertyInfo[] getPropertyInfo(
89: final String u, final Properties p) throws SQLException {
90: return this .driver.getPropertyInfo(u, p);
91: }
92:
93: public final boolean jdbcCompliant() {
94: return this.driver.jdbcCompliant();
95: }
96: }
97: }
|