1 16 package scriptella.core; 17 18 import java.io.ByteArrayOutputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.net.URLClassLoader ; 23 24 32 class DriverClassLoader extends URLClassLoader { 33 public DriverClassLoader(URL [] urls) { 34 super(urls, DriverClassLoader.class.getClassLoader()); 35 } 36 37 50 public Class <?> loadClass(String name) throws ClassNotFoundException { 51 if (name.startsWith("scriptella.jdbc.") || name.startsWith("scriptella.driver.")) { 52 byte[] b = getClassBytes(name); 53 if (b != null) { 54 definePackage(name); 55 return defineClass(name, b, 0, b.length); 56 } 57 } 58 return super.loadClass(name); 59 } 60 61 66 private void definePackage(String className) { 67 if (className==null) { 68 return; 69 } 70 int ind = className.lastIndexOf('.'); 71 if (ind<0) { 72 return; 73 } 74 String pName = className.substring(0, ind); 75 if (getPackage(pName)==null) { 76 definePackage(pName, null ,null, null ,null, null, null, null); 77 } 78 } 79 80 86 private static byte[] getClassBytes(final String name) { 87 String path = '/' + name.replace('.', '/') + ".class"; 88 InputStream is = DriverClassLoader.class.getResourceAsStream(path); 89 if (is == null) { 90 return null; 91 } 92 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 93 byte[] b = new byte[1024]; 94 try { 95 for (int c; (c = is.read(b)) >= 0;) { 96 baos.write(b, 0, c); 97 } 98 } catch (IOException e) { 99 throw new IllegalStateException (e); 100 } 101 return baos.toByteArray(); 102 103 } 104 105 106 } 107 | Popular Tags |