improved include detection
[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                 File file = new File(driverFile);
28                 if (file.exists() && file.isFile()) {
29                         URLClassLoader loader = getURLClassLoader(file);
30                         Class driverClass = loader.loadClass(className);
31                     try {
32                         result = (Driver) driverClass.newInstance();
33                     } catch (ClassCastException e) {
34                     }
35                 }
36                 } catch (MalformedURLException e) {
37                 } catch (ClassNotFoundException e) {
38                 } catch (InstantiationException e) {
39                 } catch (IllegalAccessException e) {
40                 }
41                 return result;
42         }
43         
44         public static String[] getAllDriverNames(String driverFile) {
45                 List list = new ArrayList();
46                 try {
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) {
54                                         try {
55                                                 Class driverClass = loader.loadClass(className);
56                                                 if (Driver.class.isAssignableFrom(driverClass)) {
57                                                         list.add(className);
58                                                 }
59                                         } catch (ClassNotFoundException ex) {
60                                         }
61                                 }
62                         }
63                 } catch (IOException e) {
64                 }
65                 return (String[]) list.toArray(new String[list.size()]);
66         }
67         
68         private static String getClassNameFromFileName(String name) {
69                 String result = null;
70                 if (name.endsWith(".class")) {
71                         result = name.substring(0, name.length()-6).replace('/', '.').replace('\\', '.' );
72                 }
73                 return result;
74         }
75
76         /**
77          * @param file
78          * @return
79          * @throws MalformedURLException
80          */
81         private static URLClassLoader getURLClassLoader(File file) throws MalformedURLException {
82                 String driverFile = file.getAbsolutePath();
83                 URLClassLoader loader =
84                     (URLClassLoader) classLoaderCache.get(driverFile);
85                 if (loader == null) {
86                     URL urls[] = new URL[1];
87                     urls[0] = file.toURL();
88                     loader = new URLClassLoader(urls);
89                     classLoaderCache.put(driverFile, loader);
90                 }
91                 return loader;
92         }
93 }