1 package com.quantum.util;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
7 import java.net.URLClassLoader;
8 import java.sql.Driver;
9 import java.util.ArrayList;
10 import java.util.Enumeration;
11 import java.util.Hashtable;
12 import java.util.List;
13 import java.util.jar.JarEntry;
14 import java.util.jar.JarFile;
20 public class JarUtil {
22 private static Hashtable classLoaderCache = new Hashtable();
24 public static Driver loadDriver(String[] driverFiles, String className) {
27 Class driverClass = loadDriverClass(driverFiles, className);
28 if (driverClass != null) {
30 result = (Driver) driverClass.newInstance();
31 } catch (ClassCastException e) {
34 } catch (InstantiationException e) {
35 } catch (IllegalAccessException e) {
36 } catch (NoClassDefFoundError e) {
37 } catch (RuntimeException e) {
42 public static Class loadDriverClass(String[] driverFiles, String className) {
44 if (driverFiles != null && driverFiles.length > 0
45 && driverFiles[0].trim().length() > 0 && className != null) {
47 File[] files = getFiles(driverFiles);
48 URLClassLoader loader = getURLClassLoader(files);
49 result = loadDriverClass(className, loader);
50 } catch (MalformedURLException e) {
51 } catch (ClassNotFoundException e) {
52 } catch (NoClassDefFoundError e) {
53 } catch (RuntimeException e) {
55 } else if (className != null) {
57 result = loadDriverClass(className, JarUtil.class.getClassLoader());
58 } catch (ClassNotFoundException e) {
59 } catch (NoClassDefFoundError e) {
60 } catch (RuntimeException e) {
70 private static File[] getFiles(String[] driverFiles) {
71 List list = new ArrayList();
73 for (int i = 0, length = driverFiles == null ? 0 : driverFiles.length; i < length; i++) {
74 File file = new File(driverFiles[i]);
75 if (file.exists() && file.isFile()) {
79 return (File[]) list.toArray(new File[list.size()]);
86 * @throws ClassNotFoundException
88 private static Class loadDriverClass(String className, ClassLoader loader) throws ClassNotFoundException {
89 Class driverClass = loader.loadClass(className);
90 return Driver.class.isAssignableFrom(driverClass) ? driverClass : null;
93 public static String[] getAllDriverNames(String[] driverFile) {
94 List list = new ArrayList();
96 File[] files = getFiles(driverFile);
97 URLClassLoader loader = getURLClassLoader(files);
98 for (int i = 0, length = files == null ? 0 : files.length; i < length; i++) {
99 JarFile jar = new JarFile(files[i]);
100 addCandidateDriversToList(list, loader, jar);
102 } catch (IOException e) {
104 return (String[]) list.toArray(new String[list.size()]);
112 private static void addCandidateDriversToList(List list, URLClassLoader loader, JarFile jar) {
113 for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
114 JarEntry entry = (JarEntry) e.nextElement();
115 String className = getClassNameFromFileName(entry.getName());
116 if (className != null) {
118 Class driverClass = loadDriverClass(className, loader);
119 if (driverClass != null) {
122 } catch (NoClassDefFoundError ex) {
123 } catch (ClassNotFoundException ex) {
124 } catch (RuntimeException ex) {
130 private static String getClassNameFromFileName(String name) {
131 String result = null;
132 if (name.endsWith(".class")) {
133 result = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.' );
141 * @throws MalformedURLException
143 private static URLClassLoader getURLClassLoader(File[] files) throws MalformedURLException {
145 String driverPath = getFilePath(files);
146 URLClassLoader loader =
147 (URLClassLoader) classLoaderCache.get(driverPath);
148 if (loader == null) {
149 URL urls[] = new URL[files == null ? 0 : files.length];
150 for (int i = 0, length = urls.length; i < length; i++) {
151 urls[i] = files[i].toURL();
153 loader = urls.length > 0 ? new URLClassLoader(urls) : null;
154 if (loader != null) {
155 classLoaderCache.put(driverPath, loader);
165 private static String getFilePath(File[] files) {
166 StringBuffer buffer = new StringBuffer();
167 for (int i = 0, length = files == null ? 0 : files.length; i < length; i++) {
168 buffer.append(files[i].getAbsolutePath());
170 buffer.append(File.pathSeparator);
173 return buffer.toString();