1 24 25 package org.objectweb.cjdbc.controller.connection; 26 27 import java.io.BufferedInputStream ; 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FilenameFilter ; 31 import java.io.IOException ; 32 import java.net.MalformedURLException ; 33 import java.net.URL ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.jar.JarFile ; 37 import java.util.zip.ZipEntry ; 38 import java.util.zip.ZipFile ; 39 import java.util.zip.ZipInputStream ; 40 41 47 57 public class DriverClassLoader extends ClassLoader 58 { 59 60 61 private File path = null; 62 63 69 DriverClassLoader(ClassLoader parent, File pPath) 70 { 71 super(parent); 72 path = pPath; 73 if (path == null) 74 path = new File (""); 75 76 } 77 78 83 protected Class findClass(String className) throws ClassNotFoundException 84 { 85 86 FileInputStream fis = null; 87 88 try 89 { 90 byte[] classBytes = null; 91 92 String pathName = className.replace('.', File.separatorChar); 94 File file = new File (path.getAbsolutePath(), pathName + ".class"); 95 if (file.exists()) 96 { 97 fis = new FileInputStream (file); 99 classBytes = new byte[fis.available()]; 100 fis.read(classBytes); 101 } 102 else 103 { 104 classBytes = findClassInJarFile(path, className); 106 } 107 108 Class clazz = defineClass(null, classBytes, 0, classBytes.length); 110 return clazz; 111 } 112 catch (Exception e) 113 { 114 throw new ClassNotFoundException (className, e); 116 } 117 finally 118 { 119 if (null != fis) 120 { 121 try 122 { 123 fis.close(); 124 } 125 catch (Exception e) 126 { 127 } 128 } 129 } 130 } 131 132 136 private Hashtable htJarContents = new Hashtable (); 137 138 146 private byte[] findClassInJarFile(File dir, String className) 147 throws IOException 148 { 149 String resourceName = convertClassNameToResourceName(className); 151 byte[] classBytes = (byte[]) htJarContents.get(resourceName); 152 if (classBytes != null) 153 { 154 return classBytes; 156 } 157 158 if (!dir.canRead()) 159 throw new IOException (dir + " is not readable."); 160 161 if (dir.isFile()) 162 { 163 loadJarFile(dir.getAbsolutePath()); 165 return (byte[]) htJarContents.get(resourceName); 167 } 168 169 171 String [] jarFiles = dir.list(new FilenameFilter () 173 { 174 public boolean accept(File dir, String name) 175 { 176 return name.endsWith(".jar"); 177 } 178 }); 179 180 if (jarFiles == null) 181 throw new IOException ("Invalid path " + dir); 182 183 for (int i = 0; i < jarFiles.length; i++) 185 { 186 File file = new File (dir, jarFiles[i]); 187 JarFile jarFile = new JarFile (file); 188 189 if (jarFile.getEntry(resourceName) != null) 193 { 194 loadJarFile(jarFile.getName()); 196 197 classBytes = (byte[]) htJarContents.get(resourceName); 199 } 200 } 201 return classBytes; 202 } 203 204 207 protected URL findResource(String name) 208 { 209 210 if (path.isDirectory()) 212 { 213 File searchResource = new File (path, name); 214 if (searchResource.exists()) 215 { 216 try 217 { 218 return searchResource.toURL(); 219 } 220 catch (MalformedURLException mfe) 221 { 222 } 223 } 224 } 225 else if (path.isFile()) 226 { 227 try 229 { 230 new JarFile (path); 231 return new URL ("jar:" + path.toURL() + "!/" + name); 233 } 234 catch (Exception e) 235 { 236 return null; 238 } 239 } 240 241 try 243 { 244 String [] jarFiles = path.list(new FilenameFilter () 246 { 247 public boolean accept(File dir, String name) 248 { 249 return name.endsWith(".jar"); 250 } 251 }); 252 for (int i = 0; i < jarFiles.length; i++) 254 { 255 File file = new File (path, jarFiles[i]); 256 JarFile jarFile = new JarFile (file); 257 258 if (jarFile.getJarEntry(name) != null) 260 { 261 return new URL ("jar:" + file.toURL() + "!/" + name); 263 } 264 } 265 } 266 catch (Exception e) 267 { 268 e.printStackTrace(); 269 } 270 return null; 271 } 272 273 280 private String convertClassNameToResourceName(String className) 281 { 282 String resourceName = className; 283 resourceName = resourceName.replace('.', '/'); 284 resourceName = resourceName + ".class"; 285 return resourceName; 286 } 287 288 294 private void loadJarFile(String jarFileName) throws IOException 295 { 296 Hashtable htSizes = new Hashtable (); 297 ZipFile zf = new ZipFile (jarFileName); 302 Enumeration e = zf.entries(); 303 while (e.hasMoreElements()) 304 { 305 ZipEntry ze = (ZipEntry ) e.nextElement(); 306 307 htSizes.put(ze.getName(), new Integer ((int) ze.getSize())); 308 } 309 zf.close(); 310 311 FileInputStream fis = new FileInputStream (jarFileName); 313 BufferedInputStream bis = new BufferedInputStream (fis); 314 ZipInputStream zis = new ZipInputStream (bis); 315 ZipEntry ze = null; 316 while ((ze = zis.getNextEntry()) != null) 317 { 318 if (ze.isDirectory()) 319 { 320 continue; 321 } 322 323 int size = (int) ze.getSize(); 324 if (size == -1) 326 { 327 size = ((Integer ) htSizes.get(ze.getName())).intValue(); 329 } 330 331 byte[] b = new byte[size]; 332 int rb = 0; 333 int chunk = 0; 334 while ((size - rb) > 0) 335 { 336 chunk = zis.read(b, rb, size - rb); 337 if (chunk == -1) 338 { 339 break; 340 } 341 rb += chunk; 342 } 343 344 htJarContents.put(ze.getName(), b); 346 } 347 348 } 349 } | Popular Tags |