1 22 23 package org.continuent.sequoia.controller.connection; 24 25 import java.io.BufferedInputStream ; 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FilenameFilter ; 29 import java.io.IOException ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.jar.JarFile ; 35 import java.util.zip.ZipEntry ; 36 import java.util.zip.ZipFile ; 37 import java.util.zip.ZipInputStream ; 38 39 50 public class DriverClassLoader extends ClassLoader 51 { 52 53 54 private File path = null; 55 56 62 public DriverClassLoader(ClassLoader parent, File pPath) 63 { 64 super(parent); 65 path = pPath; 66 if (path == null) 67 path = new File (""); 68 } 69 70 73 protected synchronized Class loadClass(String name, boolean resolve) 74 throws ClassNotFoundException 75 { 76 try 77 { 78 return super.loadClass(name, resolve); 79 } 80 catch (ClassNotFoundException e) 81 { 82 Class c = findClass(name); 83 if (resolve) 84 resolveClass(c); 85 return c; 86 } 87 } 88 89 94 protected Class findClass(String className) throws ClassNotFoundException 95 { 96 FileInputStream fis = null; 97 98 try 99 { 100 byte[] classBytes = null; 101 102 String pathName = className.replace('.', File.separatorChar); 104 File file = new File (path.getAbsolutePath(), pathName + ".class"); 105 if (file.exists()) 106 { 107 fis = new FileInputStream (file); 109 classBytes = new byte[fis.available()]; 110 fis.read(classBytes); 111 } 112 else 113 { 114 classBytes = findClassInJarFile(path, className); 116 } 117 118 Class clazz = defineClass(className, classBytes, 0, classBytes.length); 120 return clazz; 121 } 122 catch (Exception e) 123 { 124 throw new ClassNotFoundException (className, e); 126 } 127 finally 128 { 129 if (null != fis) 130 { 131 try 132 { 133 fis.close(); 134 } 135 catch (Exception e) 136 { 137 } 138 } 139 } 140 } 141 142 146 private Hashtable htJarContents = new Hashtable (); 147 148 156 private byte[] findClassInJarFile(File dir, String className) 157 throws IOException 158 { 159 String resourceName = convertClassNameToResourceName(className); 161 byte[] classBytes = (byte[]) htJarContents.get(resourceName); 162 if (classBytes != null) 163 { 164 return classBytes; 166 } 167 168 if (!dir.canRead()) 169 throw new IOException (dir + " is not readable."); 170 171 if (dir.isFile()) 172 { 173 loadJarFile(dir.getAbsolutePath()); 175 return (byte[]) htJarContents.get(resourceName); 177 } 178 179 181 String [] jarFiles = dir.list(new FilenameFilter () 183 { 184 public boolean accept(File dir, String name) 185 { 186 return name.endsWith(".jar"); 187 } 188 }); 189 190 if (jarFiles == null) 191 throw new IOException ("Invalid path " + dir); 192 193 for (int i = 0; i < jarFiles.length; i++) 195 { 196 File file = new File (dir, jarFiles[i]); 197 JarFile jarFile = new JarFile (file); 198 199 if (jarFile.getEntry(resourceName) != null) 203 { 204 loadJarFile(jarFile.getName()); 206 207 classBytes = (byte[]) htJarContents.get(resourceName); 209 } 210 } 211 return classBytes; 212 } 213 214 217 protected URL findResource(String name) 218 { 219 220 if (path.isDirectory()) 222 { 223 File searchResource = new File (path, name); 224 if (searchResource.exists()) 225 { 226 try 227 { 228 return searchResource.toURL(); 229 } 230 catch (MalformedURLException mfe) 231 { 232 } 233 } 234 } 235 else if (path.isFile()) 236 { 237 try 239 { 240 new JarFile (path); 241 return new URL ("jar:" + path.toURL() + "!/" + name); 243 } 244 catch (Exception e) 245 { 246 return null; 248 } 249 } 250 251 try 253 { 254 String [] jarFiles = path.list(new FilenameFilter () 256 { 257 public boolean accept(File dir, String name) 258 { 259 return name.endsWith(".jar"); 260 } 261 }); 262 for (int i = 0; i < jarFiles.length; i++) 264 { 265 File file = new File (path, jarFiles[i]); 266 JarFile jarFile = new JarFile (file); 267 268 if (jarFile.getJarEntry(name) != null) 270 { 271 return new URL ("jar:" + file.toURL() + "!/" + name); 273 } 274 } 275 } 276 catch (Exception e) 277 { 278 e.printStackTrace(); 279 } 280 return null; 281 } 282 283 290 private String convertClassNameToResourceName(String className) 291 { 292 String resourceName = className; 293 resourceName = resourceName.replace('.', '/'); 294 resourceName = resourceName + ".class"; 295 return resourceName; 296 } 297 298 304 private void loadJarFile(String jarFileName) throws IOException 305 { 306 Hashtable htSizes = new Hashtable (); 307 ZipFile zf = new ZipFile (jarFileName); 312 Enumeration e = zf.entries(); 313 while (e.hasMoreElements()) 314 { 315 ZipEntry ze = (ZipEntry ) e.nextElement(); 316 317 htSizes.put(ze.getName(), new Integer ((int) ze.getSize())); 318 } 319 zf.close(); 320 321 FileInputStream fis = new FileInputStream (jarFileName); 323 BufferedInputStream bis = new BufferedInputStream (fis); 324 ZipInputStream zis = new ZipInputStream (bis); 325 ZipEntry ze = null; 326 while ((ze = zis.getNextEntry()) != null) 327 { 328 if (ze.isDirectory()) 329 { 330 continue; 331 } 332 333 int size = (int) ze.getSize(); 334 if (size == -1) 336 { 337 size = ((Integer ) htSizes.get(ze.getName())).intValue(); 339 } 340 341 byte[] b = new byte[size]; 342 int rb = 0; 343 int chunk = 0; 344 while ((size - rb) > 0) 345 { 346 chunk = zis.read(b, rb, size - rb); 347 if (chunk == -1) 348 { 349 break; 350 } 351 rb += chunk; 352 } 353 354 htJarContents.put(ze.getName(), b); 356 } 357 358 } 359 } | Popular Tags |