01: package net.sourceforge.squirrel_sql.fw.sql;
02:
03: /*
04: * Copyright (C) 2001-2004 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import java.io.File;
22: import java.lang.reflect.Modifier;
23: import java.net.MalformedURLException;
24: import java.net.URL;
25: import java.sql.Driver;
26: import java.util.ArrayList;
27: import java.util.List;
28:
29: import net.sourceforge.squirrel_sql.fw.util.MyURLClassLoader;
30: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
31: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
32:
33: public class SQLDriverClassLoader extends MyURLClassLoader {
34:
35: /** Logger for this class. */
36: private final static ILogger s_log = LoggerController
37: .createLogger(SQLDriverClassLoader.class);
38:
39: public SQLDriverClassLoader(ISQLDriver sqlDriver)
40: throws MalformedURLException {
41: super (createURLs(sqlDriver.getName(), sqlDriver
42: .getJarFileNames()));
43: }
44:
45: public SQLDriverClassLoader(URL[] urls) {
46: super (urls);
47: }
48:
49: public SQLDriverClassLoader(URL url) {
50: super (url);
51: }
52:
53: public Class<?>[] getDriverClasses(ILogger logger) {
54: final Class<?>[] classes = getAssignableClasses(Driver.class,
55: logger);
56: final List<Class<?>> list = new ArrayList<Class<?>>();
57: for (int i = 0; i < classes.length; ++i) {
58: Class<?> clazz = classes[i];
59: if (!Modifier.isAbstract(clazz.getModifiers())) {
60: list.add(clazz);
61: }
62: }
63: return list.toArray(new Class[list.size()]);
64: }
65:
66: private static URL[] createURLs(String driverName,
67: String[] fileNames) throws MalformedURLException {
68: if (fileNames == null) {
69: fileNames = new String[0];
70: }
71: URL[] urls = new URL[fileNames.length];
72: for (int i = 0; i < fileNames.length; ++i) {
73: File f = new File(fileNames[i]);
74: if (!f.exists()) {
75: s_log.info("For driver '" + driverName
76: + "', the JVM says file doesn't exist: "
77: + fileNames[i]);
78: }
79: if (f.isDirectory()) {
80: s_log.info("For driver '" + driverName
81: + "', the JVM says the file is a directory: "
82: + fileNames[i]);
83: }
84: if (!f.canRead()) {
85: s_log.info("For driver '" + driverName
86: + "', the JVM says the file can't be read: "
87: + fileNames[i]);
88: }
89: urls[i] = f.toURL();
90:
91: }
92: return urls;
93: }
94: }
|