1 31 package org.objectweb.proactive.core.mop; 32 33 import org.apache.log4j.Logger; 34 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Method ; 37 38 import java.net.URL ; 39 import java.net.URLClassLoader ; 40 41 import java.util.Hashtable ; 42 43 44 public class MOPClassLoader extends URLClassLoader { 45 static Logger logger = Logger.getLogger(MOPClassLoader.class.getName()); 46 47 public static String BYTE_CODE_MANIPULATOR = ((System.getProperty( 50 "byteCodeManipulator") != null) 51 ? ((System.getProperty("byteCodeManipulator").equals("BCEL")) ? "BCEL" 52 : "ASM") 53 : "ASM"); 54 protected static Hashtable classDataCache = new Hashtable (); 55 protected static MOPClassLoader mopCl = null; 56 57 65 69 public static synchronized MOPClassLoader getMOPClassLoader() { 70 if (MOPClassLoader.mopCl == null) { 71 MOPClassLoader.mopCl = MOPClassLoader.createMOPClassLoader(); 72 } 73 return MOPClassLoader.mopCl; 74 } 75 76 83 public byte[] getClassData(String classname) { 84 byte[] cb = null; 85 cb = (byte[]) classDataCache.get(classname); 86 if (cb == null) { 87 logger.info( 88 "MOPClassLoader: class not found, trying to generate it"); 89 try { 90 this.loadClass(classname); 91 } catch (ClassNotFoundException e) { 92 e.printStackTrace(); 93 } 94 cb = (byte[]) classDataCache.get(classname); 95 } 96 97 return cb; 99 } 100 101 private MOPClassLoader(ClassLoader parent, URL [] urls) { 102 super(urls, parent); 103 } 104 105 public void launchMain(String [] args) throws Throwable { 106 try { 107 Class cl = Class.forName(args[0], true, this); 109 110 Class [] argTypes = { args.getClass() }; 112 Method mainMethod = cl.getMethod("main", argTypes); 113 114 String [] newArgs = new String [args.length - 1]; 116 System.arraycopy(args, 1, newArgs, 0, args.length - 1); 117 118 Object [] mainArgs = { newArgs }; 119 mainMethod.invoke(null, mainArgs); 120 } catch (ClassNotFoundException e) { 121 logger.error("Launcher: cannot find class " + args[0]); 122 } catch (NoSuchMethodException e) { 123 logger.error("Launcher: class " + args[0] + 124 " does not contain have method void 'public void main (String[])'"); 125 } catch (InvocationTargetException e) { 126 throw e.getTargetException(); 127 } 128 return; 129 } 130 131 protected static MOPClassLoader createMOPClassLoader() { 132 ClassLoader currentClassLoader = null; 134 135 try { 136 Class c = Class.forName( 137 "org.objectweb.proactive.core.mop.MOPClassLoader"); 138 currentClassLoader = c.getClassLoader(); 139 } catch (ClassNotFoundException e) { 140 e.printStackTrace(); 141 } 142 URL [] urls = null; 143 144 if (currentClassLoader instanceof java.net.URLClassLoader ) { 147 urls = ((URLClassLoader ) currentClassLoader).getURLs(); 149 } else { 150 urls = new URL [0]; 151 } 156 157 return new MOPClassLoader(currentClassLoader, urls); 159 } 160 161 protected Class findClass(String name) throws ClassNotFoundException { 162 return super.findClass(name); 163 } 164 165 public Class loadClass(String name) throws ClassNotFoundException { 166 return this.loadClass(name, null, false); 167 } 168 169 public Class loadClass(String name, ClassLoader cl) 170 throws ClassNotFoundException { 171 return this.loadClass(name, cl, false); 172 } 173 174 protected synchronized Class loadClass(String name, ClassLoader cl, 175 boolean resolve) throws ClassNotFoundException { 176 if (this.getParent() != null) { 177 try { 178 return this.getParent().loadClass(name); 179 } catch (ClassNotFoundException e) { 180 } 182 } 183 184 try { 185 if (cl != null) { 186 return cl.loadClass(name); 187 } else { 188 return Class.forName(name); 189 } 190 } catch (ClassNotFoundException e) { 191 if (Utils.isStubClassName(name)) { 194 logger.info("Generating class : " + name); 195 196 String classname = Utils.convertStubClassNameToClassName(name); 197 198 byte[] data = null; 200 if (BYTE_CODE_MANIPULATOR.equals("ASM")) { 201 ASMBytecodeStubBuilder bsb = new ASMBytecodeStubBuilder(classname); 202 long start_time = System.currentTimeMillis(); 203 data = bsb.create(); 204 MOPClassLoader.classDataCache.put(name, data); 205 } else if (BYTE_CODE_MANIPULATOR.equals("BCEL")) { 206 BytecodeStubBuilder bsb = new BytecodeStubBuilder(classname); 207 long start_time = System.currentTimeMillis(); 208 data = bsb.create(); 209 MOPClassLoader.classDataCache.put(name, data); 210 } else { 211 logger.error( 213 "byteCodeManipulator argument is optionnal. If specified, it can only be set to BCEL."); 214 logger.error( 215 "Any other setting will result in the use of ASM, the default bytecode manipulator framework"); 216 } 217 218 try { 228 Class clc = Class.forName("java.lang.ClassLoader"); 229 Class [] argumentTypes = new Class [5]; 230 argumentTypes[0] = name.getClass(); 231 argumentTypes[1] = data.getClass(); 232 argumentTypes[2] = Integer.TYPE; 233 argumentTypes[3] = Integer.TYPE; 234 argumentTypes[4] = Class.forName( 235 "java.security.ProtectionDomain"); 236 237 Method m = clc.getDeclaredMethod("defineClass", 238 argumentTypes); 239 m.setAccessible(true); 240 241 Object [] effectiveArguments = new Object [5]; 242 effectiveArguments[0] = name; 243 effectiveArguments[1] = data; 244 effectiveArguments[2] = new Integer (0); 245 effectiveArguments[3] = new Integer (data.length); 246 effectiveArguments[4] = this.getClass().getProtectionDomain(); 247 248 if (this.getParent() == null) { 250 return (Class ) m.invoke(Thread.currentThread() 251 .getContextClassLoader(), 252 effectiveArguments); 253 } else { 254 return (Class ) m.invoke(this.getParent(), 255 effectiveArguments); 256 } 257 } catch (Exception ex) { 258 ex.printStackTrace(); 259 throw new ClassNotFoundException (ex.getMessage()); 260 } 261 } else { 262 System.out.println("Cannot generate class " + name); 263 throw e; 264 } 265 } 266 } 267 } 268 269 270 | Popular Tags |