KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > DriverFactory


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
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 scriptella.core;
17
18 import scriptella.jdbc.GenericDriver;
19 import scriptella.spi.ScriptellaDriver;
20
21 import java.sql.Driver JavaDoc;
22
23 /**
24  * Factory for Scriptella Service Providers and JDBC drivers.
25  *
26  * @author Fyodor Kupolov
27  * @version 1.0
28  */

29 public final class DriverFactory {
30     //singleton
31
private DriverFactory() {
32     }
33
34     /**
35      * Loads a driver specified by a full or a short name.
36      * This method tries to lookup a driver class specified by a name.
37      * <p>The looking up procedure is the following:
38      * <ol>
39      * <li>Class with name <code>driverName</code> is looked up.
40      * <li>If class not found, a class with the following name is checked:
41      * scriptella.driver.&lt;driverName&gt;.Driver
42      * <li>If no classes found - an exception is raised.
43      * </ol>
44      * <p>
45      * @param driverName driver class short or full name.
46      * @param loader class loader to use when loading driver classes.
47      * @return found driver class.
48      * @throws ClassNotFoundException if no drivers satisfy specified name.
49      */

50     public static ScriptellaDriver getDriver(String JavaDoc driverName, ClassLoader JavaDoc loader) throws ClassNotFoundException JavaDoc {
51         //try direct match
52
try {
53             return getDriver(Class.forName(driverName, true, loader));
54         } catch (ClassNotFoundException JavaDoc e) {
55             //if not found try to produce a full name from a short one
56
String JavaDoc fullName = "scriptella.driver."+driverName+".Driver";
57             try {
58                 return getDriver(Class.forName(fullName, true, loader));
59             } catch (ClassNotFoundException JavaDoc ignoredException) {
60                 throw e;//it's not a typo - return the first exception
61
}
62
63         }
64
65
66     }
67
68     /**
69      * Creates a Scriptella Driver using specified class.
70      * <p>If class is a {@link Driver} {@link GenericDriver JDBC Bridge} is used.
71      * <p>To be successfully instantiated the driver class must implement {@link ScriptellaDriver} class
72      * and has no-arg public constructor.
73      *
74      * @param drvClass driver class.
75      * @return Scriptella Driver
76      */

77     @SuppressWarnings JavaDoc("unchecked")
78     public static ScriptellaDriver getDriver(Class JavaDoc drvClass) {
79         if (Driver.class.isAssignableFrom(drvClass)) {
80             //We must load JDBC driver using the same classloader as drvClass
81
try {
82
83                 ClassLoader JavaDoc drvClassLoader = drvClass.getClassLoader();
84                 if (drvClassLoader==null) { //if boot classloader is used,
85
// load scriptella driver using the class loader for this class.
86
drvClassLoader=DriverFactory.class.getClassLoader();
87                 }
88                 Class JavaDoc<?> cl = Class.forName("scriptella.jdbc.GenericDriver", true, drvClassLoader);
89                 return (ScriptellaDriver)cl.newInstance();
90             } catch (Exception JavaDoc e) {
91                 throw new IllegalStateException JavaDoc("Unable to instantiate JDBC driver for " + drvClass, e);
92
93             }
94         } else if (ScriptellaDriver.class.isAssignableFrom(drvClass)) {
95             try {
96                 return (ScriptellaDriver) drvClass.newInstance();
97             } catch (Exception JavaDoc e) {
98                 throw new IllegalStateException JavaDoc("Unable to instantiate driver for " + drvClass, e);
99             }
100
101         } else {
102             throw new IllegalArgumentException JavaDoc("Class " + drvClass + " is not a scriptella compatible driver.");
103         }
104
105
106     }
107 }
108
Popular Tags