Changes for variablemodificatin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / JarUtil.java
1 package com.quantum.util;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
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;
15
16
17 /**
18  * @author BC Holmes
19  */
20 public class JarUtil {
21
22     private static Hashtable classLoaderCache = new Hashtable();
23         
24         public static Driver loadDriver(String driverFile, String className) {
25                 Driver result = null;
26                 try {
27                         Class driverClass = loadDriverClass(driverFile, className);
28                         if (driverClass != null) {
29                     try {
30                         result = (Driver) driverClass.newInstance();
31                     } catch (ClassCastException e) {
32                     }
33                 }
34                 } catch (InstantiationException e) {
35                 } catch (IllegalAccessException e) {
36                 }
37                 return result;
38         }
39         
40         public static Class loadDriverClass(String driverFile, String className) {
41                 Class result = null;
42                 if (driverFile != null && className != null) {
43                         try {
44                         File file = new File(driverFile);
45                         if (file.exists() && file.isFile()) {
46                                 URLClassLoader loader = getURLClassLoader(file);
47                                 Class driverClass = loader.loadClass(className);
48                                 result = Driver.class.isAssignableFrom(driverClass) ? driverClass : null;
49                         }
50                         } catch (MalformedURLException e) {
51                         } catch (ClassNotFoundException e) {
52                         }
53                 }
54                 return result;
55         }
56         
57         public static String[] getAllDriverNames(String driverFile) {
58                 List list = new ArrayList();
59                 try {
60                         File file = new File(driverFile);
61                         URLClassLoader loader = getURLClassLoader(file);
62                         JarFile jar = new JarFile(file);
63                         for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
64                                 JarEntry entry = (JarEntry) e.nextElement();
65                                 String className = getClassNameFromFileName(entry.getName());
66                                 if (className != null) {
67                                         try {
68                                                 Class driverClass = loader.loadClass(className);
69                                                 if (Driver.class.isAssignableFrom(driverClass)) {
70                                                         list.add(className);
71                                                 }
72                                         } catch (ClassNotFoundException ex) {
73                                         }
74                                 }
75                         }
76                 } catch (IOException e) {
77                 }
78                 return (String[]) list.toArray(new String[list.size()]);
79         }
80         
81         private static String getClassNameFromFileName(String name) {
82                 String result = null;
83                 if (name.endsWith(".class")) {
84                         result = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.' );
85                 }
86                 return result;
87         }
88
89         /**
90          * @param file
91          * @return
92          * @throws MalformedURLException
93          */
94         private static URLClassLoader getURLClassLoader(File file) throws MalformedURLException {
95                 String driverFile = file.getAbsolutePath();
96                 URLClassLoader loader =
97                     (URLClassLoader) classLoaderCache.get(driverFile);
98                 if (loader == null) {
99                     URL urls[] = new URL[1];
100                     urls[0] = file.toURL();
101                     loader = new URLClassLoader(urls);
102                     classLoaderCache.put(driverFile, loader);
103                 }
104                 return loader;
105         }
106 }