1 19 20 package org.apache.cayenne.jpa; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.security.SecureClassLoader ; 26 27 33 class JpaUnitClassLoader extends SecureClassLoader { 34 35 JpaUnitClassLoader(ClassLoader parent) { 36 super(parent); 37 } 38 39 @Override 40 protected synchronized Class <?> loadClass(String name, boolean resolve) 41 throws ClassNotFoundException { 42 43 if (name.startsWith("java.") || name.startsWith("javax.")) { 44 return super.loadClass(name, resolve); 45 } 46 47 Class c = findLoadedClass(name); 48 49 if (c == null) { 50 c = findClass(name); 51 } 52 53 if (resolve) { 54 resolveClass(c); 55 } 56 57 return c; 58 } 59 60 63 @Override 64 protected Class <?> findClass(String name) throws ClassNotFoundException { 65 String path = name.replace('.', '/') + ".class"; 66 67 InputStream in = getResourceAsStream(path); 68 if (in == null) { 69 throw new ClassNotFoundException (name); 70 } 71 72 try { 73 ByteArrayOutputStream out = new ByteArrayOutputStream (1024); 74 byte[] buffer = new byte[1024]; 75 int read; 76 77 while ((read = in.read(buffer, 0, 1024)) > 0) { 78 out.write(buffer, 0, read); 79 } 80 81 out.close(); 82 byte[] classBytes = out.toByteArray(); 83 84 return defineClass(name, classBytes, 0, classBytes.length); 85 } 86 catch (SecurityException e) { 87 return super.findClass(name); 90 } 91 catch (IOException e) { 92 throw new ClassNotFoundException (name, e); 93 } 94 finally { 95 try { 96 in.close(); 97 } 98 catch (IOException e) { 99 } 101 } 102 } 103 } 104 | Popular Tags |