1 17 18 package org.objectweb.jac.core.dist; 19 20 import java.io.FileInputStream ; 21 import java.util.Hashtable ; 22 import org.apache.log4j.Logger; 23 24 31 32 public class DistdClassLoader extends ClassLoader { 33 static Logger logger = Logger.getLogger("dist.classloader"); 34 35 37 protected transient Hashtable loadedClasses = new Hashtable (); 38 39 42 protected Hashtable loadedByteCodes = new Hashtable (); 43 44 45 public boolean bootstrapping = true; 46 47 48 public static String classRepositoryName = null; 49 50 65 66 public Class loadClass(String name) 67 throws ClassNotFoundException { 68 69 70 if (name.equals(getClass().getName())) { 71 logger.debug("Do not reload "+getClass().getName()); 72 return getClass(); 73 } 74 75 Class cl; 76 if (name.startsWith("java.")) { 77 logger.debug("Get system class "+name+" from parent classloader"); 78 cl = getParent().loadClass(name); 79 loadedClasses.put(name, cl); 80 return cl; 81 } 82 83 84 cl = (Class )loadedClasses.get(name); 85 86 if (cl == null) { 87 88 byte[] bc = null; 89 90 91 92 if ( (!bootstrapping) && classRepositoryName != null ) { 93 94 95 logger.debug("Downloading "+name+" from "+classRepositoryName); 96 RemoteContainer rc = RemoteContainer.resolve(classRepositoryName); 97 try { 98 bc = rc.getByteCodeFor(name); 99 } catch(Exception e) { 100 logger.debug("Failed to get bytecode for "+name+ 101 " from "+classRepositoryName+": "+e); 102 } 103 if (bc != null) { 104 loadedByteCodes.put(name, bc); 105 } 106 } 107 108 109 110 if (bc == null) { 111 112 113 logger.debug("Loading "+name+" from local FS"); 114 try { 115 FileInputStream in = new FileInputStream ( 116 getParent().getResource(name.replace('.','/')+".class").getFile()); 117 bc = new byte[in.available()]; 118 in.read(bc); 119 } catch (Exception e) { 120 121 cl = getParent().loadClass(name); 122 loadedClasses.put(name, cl); 123 return cl; 124 } 125 126 } 127 128 if (bc != null) { 129 130 try { 131 cl = defineClass(name, bc, 0, bc.length); 132 loadedClasses.put(name, cl); 133 } catch (Exception e) { 134 135 logger.error("Cannot load class "+name,e); 136 } 137 } 138 139 } else { 140 logger.debug("Already loaded "+name); 141 } 142 143 return cl; 144 } 145 146 151 152 public byte[] getByteCode (String className) { 153 byte[] bc = (byte[])loadedByteCodes.get (className); 154 if (bc == null) { 155 } 156 return bc; 157 } 158 159 } 160 | Popular Tags |