Fixed: 1776098 - Cannot open source file from Breakpoints View
[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[] driverFiles, String className) {
25                 Driver result = null;
26                 try {
27                         Class driverClass = loadDriverClass(driverFiles, 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                 } catch (NoClassDefFoundError e) {
37                 } catch (RuntimeException e) {
38                 }
39                 return result;
40         }
41         
42         public static Class loadDriverClass(String[] driverFiles, String className) {
43                 Class result = null;
44                 if (driverFiles != null && driverFiles.length > 0 
45                                 && driverFiles[0].trim().length() > 0 && className != null) {
46                         try {
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) {
54                         }
55                 } else if (className != null) {
56                         try {
57                                 result = loadDriverClass(className, JarUtil.class.getClassLoader());
58                         } catch (ClassNotFoundException e) {
59                         } catch (NoClassDefFoundError e) {
60                         } catch (RuntimeException e) {
61                         }
62                 }
63                 return result;
64         }
65         
66         /**
67          * @param driverFiles
68          * @return
69          */
70         private static File[] getFiles(String[] driverFiles) {
71                 List list = new ArrayList();
72                 
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()) {
76                                 list.add(file);
77                         }
78                 }
79                 return (File[]) list.toArray(new File[list.size()]);
80         }
81
82         /**
83          * @param className
84          * @param loader
85          * @return
86          * @throws ClassNotFoundException
87          */
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;
91         }
92
93         public static String[] getAllDriverNames(String[] driverFile) {
94                 List list = new ArrayList();
95                 try {
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);
101                         }
102                 } catch (IOException e) {
103                 }
104                 return (String[]) list.toArray(new String[list.size()]);
105         }
106         
107         /**
108          * @param list
109          * @param loader
110          * @param jar
111          */
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) {
117                                 try {
118                                         Class driverClass = loadDriverClass(className, loader);
119                                         if (driverClass != null) {
120                                                 list.add(className);
121                                         }
122                                 } catch (NoClassDefFoundError ex) {
123                                 } catch (ClassNotFoundException ex) {
124                                 } catch (RuntimeException ex) {
125                                 }
126                         }
127                 }
128         }
129
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('\\', '.' );
134                 }
135                 return result;
136         }
137
138         /**
139          * @param file
140          * @return
141          * @throws MalformedURLException
142          */
143         private static URLClassLoader getURLClassLoader(File[] files) throws MalformedURLException {
144                 
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();
152                         }
153                     loader = urls.length > 0 ? new URLClassLoader(urls) : null;
154                     if (loader != null) {
155                         classLoaderCache.put(driverPath, loader);
156                     }
157                 }
158                 return loader;
159         }
160
161         /**
162          * @param files
163          * @return
164          */
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());
169                         if (i < length-1) {
170                                 buffer.append(File.pathSeparator);
171                         }
172                 }
173                 return buffer.toString();
174         }
175 }