KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > jdbcutil > DriverLoader


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24 import java.io.File JavaDoc;
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 JavaDoc classpath, String JavaDoc driverClass) throws Exception JavaDoc {
35         ArrayList JavaDoc urls = new ArrayList JavaDoc();
36         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(classpath, ",");
37         while (tokenizer.hasMoreTokens()) {
38             String JavaDoc filename = tokenizer.nextToken().trim();
39             try
40             {
41                 URL JavaDoc url = new File JavaDoc(filename).toURL();
42                 urls.add(url);
43             }
44             catch (MalformedURLException JavaDoc e)
45             {
46                 throw new Exception JavaDoc("Invalid filename in driver path.", e);
47             }
48         }
49         ClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc((URL JavaDoc[])urls.toArray(new URL JavaDoc[]{}), Thread.currentThread().getContextClassLoader());
50         Driver driver = (Driver) Class.forName(driverClass, true, classLoader).newInstance();
51         DriverManager.registerDriver(new DriverShim(driver));
52     }
53
54     /**
55      * Work-around for the fact that DriverManager will only load Driver's loaded by
56      * the system classloader.
57      *
58      * More information can be found on:
59      * http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
60      */

61     private static final class DriverShim implements Driver
62     {
63         private Driver driver;
64
65         DriverShim(Driver d)
66         {
67             this.driver = d;
68         }
69
70         public final boolean acceptsURL(final String JavaDoc u) throws SQLException
71         {
72             return this.driver.acceptsURL(u);
73         }
74
75         public final Connection connect(final String JavaDoc u, final Properties JavaDoc p) throws SQLException
76         {
77             return this.driver.connect(u, p);
78         }
79
80         public final int getMajorVersion()
81         {
82             return this.driver.getMajorVersion();
83         }
84
85         public final int getMinorVersion()
86         {
87             return this.driver.getMinorVersion();
88         }
89
90         public final DriverPropertyInfo[] getPropertyInfo(final String JavaDoc u, final Properties JavaDoc p) throws SQLException
91         {
92             return this.driver.getPropertyInfo(u, p);
93         }
94
95         public final boolean jdbcCompliant()
96         {
97             return this.driver.jdbcCompliant();
98         }
99     }
100 }
101
Popular Tags