1 9 package org.ozoneDB.core; 10 11 import org.ozoneDB.DxLib.DxHashMap; 12 import org.ozoneDB.DxLib.DxMap; 13 14 import java.io.InputStream ; 15 16 17 25 final class OzoneClassLoader extends ClassLoader { 26 27 private DxMap classTable; 28 29 30 public OzoneClassLoader() { 31 super(); 32 flushCache(); 33 } 34 35 36 protected void flushCache() { 37 classTable = new DxHashMap( 100 ); 38 } 39 40 41 protected Class findClass( String name ) throws ClassNotFoundException { 42 if (name.startsWith( "java" )) { 43 throw new ClassNotFoundException ( "I'm not here to load system classes." ); 44 } 45 46 System.out.println("Finding class " + name); 47 String resourceName = name.replace('.', '/') + ".class"; 48 56 InputStream classIn = getResourceAsStream(resourceName); 58 if ( classIn == null ) { 59 System.out.println("could not find class " + name + " using context classloader, trying system:"); 60 classIn = getSystemClassLoader().getSystemResourceAsStream( resourceName ); 61 } 62 if (classIn == null) { 63 throw new ClassNotFoundException ( "Unable to locate resource: " + resourceName ); 64 } 65 66 try { 67 byte[] classBytes = new byte[classIn.available()]; 68 classIn.read( classBytes ); 69 classIn.close(); 70 71 Class cl = defineClass( name, classBytes, 0, classBytes.length, null ); 73 return cl; 74 } catch (Exception e) { 75 e.printStackTrace(); 76 throw new ClassNotFoundException ( e.toString() ); 77 } 78 } 79 80 81 public Class loadClass( String name ) throws ClassNotFoundException { 82 return loadClass( name, false ); 83 } 84 85 86 protected Class loadClass( String name, boolean resolve ) throws ClassNotFoundException { 87 if (name.startsWith( "java" )) { 88 return getSystemClassLoader().loadClass( name ); 89 } else { 90 Class cl = (Class )classTable.elementForKey( name ); 91 if (cl == null) { 92 cl = primitiveType( name ); 93 if (cl == null) { 94 101 cl = Thread.currentThread().getContextClassLoader().loadClass( name ); 104 } 105 classTable.addForKey( cl, name ); 106 } 107 return cl; 108 } 109 } 110 111 112 115 protected static Class primitiveType( String name ) { 116 if (name.equals( "int" )) { 117 return Integer.TYPE; 118 } else if (name.equals( "char" )) { 119 return Character.TYPE; 120 } else if (name.equals( "byte" )) { 121 return Byte.TYPE; 122 } else if (name.equals( "double" )) { 123 return Double.TYPE; 124 } else if (name.equals( "float" )) { 125 return Float.TYPE; 126 } else if (name.equals( "long" )) { 127 return Long.TYPE; 128 } else if (name.equals( "short" )) { 129 return Short.TYPE; 130 } else if (name.equals( "boolean" )) { 131 return Boolean.TYPE; 132 } else { 133 return null; 134 } 135 } 136 137 } 138 | Popular Tags |