KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > connection > DriverPool


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * DriverPool.java
28  *
29  * Created on 25 maggio 2004, 16.34
30  *
31  */

32
33 package it.businesslogic.ireport.connection;
34
35 /*
36  * Copyright 2002 by Mark A. Kobold
37  *
38  * The contents of this file are subject to the Mozilla Public License Version 1.1
39  * (the "License"); you may not use this file except in compliance with the License.
40  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
41  *
42  * Software distributed under the License is distributed on an "AS IS" basis,
43  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
44  * for the specific language governing rights and limitations under the License.
45  *
46  * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
47  *
48  * The Initial Developer of the Original Code is Markus A. Kobold.
49  *
50  * Portions created by Mark A. Kobold are
51  * Copyright (C) Copyright (C) 2000 Mark A. Kobold. All Rights Reserved.
52  * Contributor(s): Mark A. Kobold <mkobold@sprintpcs.com>.
53  *
54  * Contributor(s): all the names of the contributors are added in the source code
55  * where applicable.
56  *
57  * If you didn't download this code from the following link, you should check if
58  * you aren't using an obsolete version:
59  * http://isql.sourceforge.net/
60  */

61 import java.sql.Driver JavaDoc;
62 import java.sql.SQLException JavaDoc;
63 import java.util.ArrayList JavaDoc;
64 import java.util.Iterator JavaDoc;
65
66 /**
67  * Utility object for loading and pooling JDBC drivers.
68  * <p>
69  * This class was originally embedded into the DatabaseConnection object, it has
70  * been moved out for clarity. The main goal of this class is to provide a few of the
71  * same functions provided java.sql.DriverManager object but in a way that is better
72  * suited for the iSQL-Viewer program.
73  * <p>
74  * The main shortcoming of the java.sql.DriverManager is that the driver manager
75  * will not allow you to use a registered driver that was loaded with a different
76  * classloader than classloader from the calling class. Basically in short terms
77  * the default DriverManager really only seems to work when all the code and JDBC
78  * drivers are from the same classloader, which doesn't work with the iSQL-Viewer
79  * system since there are runtime and classloader for each ServiceDefinition object.
80  *
81  * @see java.sql.DriverManager
82  * @author Markus A. Kobold &lt;mkobold at sprintpcs dot com&gt;
83  */

84 public final class DriverPool {
85     private DriverPool(){
86         
87     }
88     
89     // This is a collection of Driver Objects that are registered through the
90
// RegisterDriver method.
91
private static final ArrayList JavaDoc driverSet = new ArrayList JavaDoc();
92     /**
93      * Check method to ensure we don't have multiple instances of the same
94      * driver.
95      * <p>
96      * If the Major version and the Minors version of the Driver plus the class
97      * names are the same the the driver will be considerd all ready
98      * registered.
99      * <p>
100      * This method is generally called from the registerDriver() method.
101      *
102      * @param jdbcDriver Driver Instance to check.
103      * @return true if similar class is registered
104      * @see #registerDriver(String, ClassLoader)
105      */

106     private static boolean isDriverAllReadyRegistered(Driver JavaDoc jdbcDriver) {
107         int verCheckSum = (jdbcDriver.getMajorVersion() * 10) + jdbcDriver.getMinorVersion();
108         Iterator JavaDoc drivers = driverSet.iterator();
109         while (drivers.hasNext()) {
110             Driver JavaDoc driver = (Driver JavaDoc) drivers.next();
111             int checkSum = (driver.getMajorVersion() * 10) + driver.getMinorVersion();
112             if (checkSum == verCheckSum && (driver.getClass().getName().equals(jdbcDriver.getClass().getName())))
113                 return true;
114         }
115         return false;
116     }
117
118     /**
119      * Simple Driver registration method.
120      * <p>
121      * To overcome the security configuration of the java.sql.DriverManager
122      * object this method provides simple instantiation of the given driver by
123      * class name with a given ClassLoader. The java.sql.DriverManager works
124      * really only if all classes are under the same ClassLoader, for most
125      * cases this is not allways an option.
126      * <p>
127      * If a Driver is all ready registered this method will actually do
128      * nothing.
129      *
130      * @param driverClass the fully qaulified name of the Driver e.g.
131      * 'org.my.sql.Driver'
132      * @param cl ClassLoader to try and load the Driver with.
133      * @throws ClassNotFoundException if an Error Occurs with creating the
134      * Driver instance.
135      * @see #deregisterDriver(Driver)
136      * @see #getDriver(String)
137      */

138     public static void registerDriver(String JavaDoc driverClass, ClassLoader JavaDoc cl) throws ClassNotFoundException JavaDoc {
139         if (cl == null)
140             cl = Thread.currentThread().getContextClassLoader();
141
142         if (driverClass == null) return;
143
144         try {
145             Driver JavaDoc jdbcDriver = (Driver JavaDoc) Class.forName(driverClass, false, cl).newInstance();
146             if (!isDriverAllReadyRegistered(jdbcDriver)) {
147                 synchronized (driverSet) {
148                     driverSet.add(jdbcDriver);
149                 }
150                 String JavaDoc[] p = new String JavaDoc[] { driverClass, jdbcDriver.toString()};
151
152
153             }
154         } catch (Throwable JavaDoc t) {
155             throw new ClassNotFoundException JavaDoc(driverClass);
156         }
157     }
158
159     /**
160      * Deregisters JDBC Driver object by refrence.
161      * <p>
162      * This will search through all registered drivers and remove the driver if
163      * the object refrences are the same.
164      *
165      * @param jdbcDriver Driver Object that needs to be deregistered.
166      * @see #registerDriver(String,ClassLoader)
167      */

168     public static void deregisterDriver(Driver JavaDoc jdbcDriver) {
169         if (jdbcDriver == null) {
170             return;
171         }
172
173         try {
174
175             String JavaDoc[] p = new String JavaDoc[] { jdbcDriver.getClass().getName(), jdbcDriver.toString()};
176             synchronized (driverSet) {
177                 int i = 0;
178                 for (i = 0; i < driverSet.size(); i++) {
179                     Driver JavaDoc di = (Driver JavaDoc) driverSet.get(i);
180                     if (di == jdbcDriver) {
181                         break;
182                     }
183                 }
184
185                 // If we can't find the driver just return.
186
if (i >= driverSet.size()) {
187                     return;
188                 }
189
190                 driverSet.remove(i);
191             }
192         } catch (Throwable JavaDoc t) {
193                     t.printStackTrace();
194                 }
195     }
196
197     /**
198      * Retrieval method for getting a driver by URL.
199      * <p>
200      * This will iterate through all registered drivers and if the value
201      * returned by the acceptsURL() of the driver is true then the driver is
202      * returned.
203      * <p>
204      * if all internally registered drivers fail against the URL then an
205      * attempt to fall back to the original DriverManager and attempt to
206      * retrieve a driver from there will be made.
207      *
208      * @param jdbcURL URL of JDBC Connection
209      * @return Instance of the JDBC Driver that can accept the given URL
210      * @throws SQLException if the java.sql.DriverManager fallback fails.
211      */

212     public static Driver JavaDoc getDriver(String JavaDoc jdbcURL) throws SQLException JavaDoc {
213         Iterator JavaDoc drivers = driverSet.iterator();
214         while (drivers.hasNext()) {
215             Driver JavaDoc jdbcDriver = (Driver JavaDoc) drivers.next();
216             try {
217                 if (jdbcDriver.acceptsURL(jdbcURL))
218                     return jdbcDriver;
219             } catch (Throwable JavaDoc t) {
220             }
221
222         }
223         return null; //DriverPool.getDriver(jdbcURL);
224
}
225
226 }
227
Popular Tags