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 driverFile, String className) {
27 File file = new File(driverFile);
28 if (file.exists() && file.isFile()) {
29 URLClassLoader loader = getURLClassLoader(file);
30 Class driverClass = loader.loadClass(className);
32 result = (Driver) driverClass.newInstance();
33 } catch (ClassCastException e) {
36 } catch (MalformedURLException e) {
37 } catch (ClassNotFoundException e) {
38 } catch (InstantiationException e) {
39 } catch (IllegalAccessException e) {
44 public static String[] getAllDriverNames(String driverFile) {
45 List list = new ArrayList();
47 File file = new File(driverFile);
48 URLClassLoader loader = getURLClassLoader(file);
49 JarFile jar = new JarFile(file);
50 for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
51 JarEntry entry = (JarEntry) e.nextElement();
52 String className = getClassNameFromFileName(entry.getName());
53 if (className != null) {
55 Class driverClass = loader.loadClass(className);
56 if (Driver.class.isAssignableFrom(driverClass)) {
59 } catch (ClassNotFoundException ex) {
63 } catch (IOException e) {
65 return (String[]) list.toArray(new String[list.size()]);
68 private static String getClassNameFromFileName(String name) {
70 if (name.endsWith(".class")) {
71 result = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.' );
79 * @throws MalformedURLException
81 private static URLClassLoader getURLClassLoader(File file) throws MalformedURLException {
82 String driverFile = file.getAbsolutePath();
83 URLClassLoader loader =
84 (URLClassLoader) classLoaderCache.get(driverFile);
86 URL urls[] = new URL[1];
87 urls[0] = file.toURL();
88 loader = new URLClassLoader(urls);
89 classLoaderCache.put(driverFile, loader);