KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > mop > MOPClassLoader


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 package org.objectweb.proactive.core.mop;
32
33 import org.apache.log4j.Logger;
34
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37
38 import java.net.URL JavaDoc;
39 import java.net.URLClassLoader JavaDoc;
40
41 import java.util.Hashtable JavaDoc;
42
43
44 public class MOPClassLoader extends URLClassLoader JavaDoc {
45     static Logger logger = Logger.getLogger(MOPClassLoader.class.getName());
46
47     // retreives the optionnal byteCodeManipulator JVM arg
48
// ASM is used by default
49
public static String JavaDoc BYTE_CODE_MANIPULATOR = ((System.getProperty(
50             "byteCodeManipulator") != null)
51         ? ((System.getProperty("byteCodeManipulator").equals("BCEL")) ? "BCEL"
52                                                                       : "ASM")
53         : "ASM");
54     protected static Hashtable JavaDoc classDataCache = new Hashtable JavaDoc();
55     protected static MOPClassLoader mopCl = null;
56
57     // public static synchronized MOPClassLoader getMOPClassLoader(
58
// ClassLoader parent, URL[] urls) {
59
// if (MOPClassLoader.mopCl == null) {
60
// MOPClassLoader.mopCl = new MOPClassLoader(parent, urls);
61
// }
62
// return MOPClassLoader.mopCl;
63
// }
64

65     /**
66      * Return the unique MOPClassLoader for the current JVM
67      * Create it if it does not exist
68      */

69     public static synchronized MOPClassLoader getMOPClassLoader() {
70         if (MOPClassLoader.mopCl == null) {
71             MOPClassLoader.mopCl = MOPClassLoader.createMOPClassLoader();
72         }
73         return MOPClassLoader.mopCl;
74     }
75
76     /**
77      * Get the bytecode of a stub given its name. If the stub can not be found
78      * the cache, the MOPClassLoader tries to generate it.
79      * @param classname The name of the stub class
80      * @return An array representing the bytecode of the stub, null if the
81      * stub could not be found or created
82      */

83     public byte[] getClassData(String JavaDoc 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 JavaDoc e) {
92                 e.printStackTrace();
93             }
94             cb = (byte[]) classDataCache.get(classname);
95         }
96
97         //return (byte[]) classDataCache.get(classname);
98
return cb;
99     }
100
101     private MOPClassLoader(ClassLoader JavaDoc parent, URL JavaDoc[] urls) {
102         super(urls, parent);
103     }
104
105     public void launchMain(String JavaDoc[] args) throws Throwable JavaDoc {
106         try {
107             // Looks up the class that contains main
108
Class JavaDoc cl = Class.forName(args[0], true, this);
109
110             // Looks up method main
111
Class JavaDoc[] argTypes = { args.getClass() };
112             Method JavaDoc mainMethod = cl.getMethod("main", argTypes);
113
114             // And calls it
115
String JavaDoc[] newArgs = new String JavaDoc[args.length - 1];
116             System.arraycopy(args, 1, newArgs, 0, args.length - 1);
117
118             Object JavaDoc[] mainArgs = { newArgs };
119             mainMethod.invoke(null, mainArgs);
120         } catch (ClassNotFoundException JavaDoc e) {
121             logger.error("Launcher: cannot find class " + args[0]);
122         } catch (NoSuchMethodException JavaDoc e) {
123             logger.error("Launcher: class " + args[0] +
124                 " does not contain have method void 'public void main (String[])'");
125         } catch (InvocationTargetException JavaDoc e) {
126             throw e.getTargetException();
127         }
128         return;
129     }
130
131     protected static MOPClassLoader createMOPClassLoader() {
132         // Gets the current classloader
133
ClassLoader JavaDoc currentClassLoader = null;
134
135         try {
136             Class JavaDoc c = Class.forName(
137                     "org.objectweb.proactive.core.mop.MOPClassLoader");
138             currentClassLoader = c.getClassLoader();
139         } catch (ClassNotFoundException JavaDoc e) {
140             e.printStackTrace();
141         }
142         URL JavaDoc[] urls = null;
143
144         // Checks if the current classloader is actually an instance of
145
// java.net.URLClassLoader, or of one of its subclasses.
146
if (currentClassLoader instanceof java.net.URLClassLoader JavaDoc) {
147             // Retrieves the set of URLs from the current classloader
148
urls = ((URLClassLoader JavaDoc) currentClassLoader).getURLs();
149         } else {
150             urls = new URL JavaDoc[0];
151             // System.out.println("Current classloader is of type " +
152
// currentClassLoader.getClass().getName() +
153
// ", which is not compatible with URLClassLoader. Cannot install MOPClassLoader");
154
// return null;
155
}
156
157         // Creates a new MOPClassLoader
158
return new MOPClassLoader(currentClassLoader, urls);
159     }
160
161     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
162         return super.findClass(name);
163     }
164
165     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
166         return this.loadClass(name, null, false);
167     }
168
169     public Class JavaDoc loadClass(String JavaDoc name, ClassLoader JavaDoc cl)
170         throws ClassNotFoundException JavaDoc {
171         return this.loadClass(name, cl, false);
172     }
173
174     protected synchronized Class JavaDoc loadClass(String JavaDoc name, ClassLoader JavaDoc cl,
175         boolean resolve) throws ClassNotFoundException JavaDoc {
176         if (this.getParent() != null) {
177             try {
178                 return this.getParent().loadClass(name);
179             } catch (ClassNotFoundException JavaDoc e) {
180                 // proceeding
181
}
182         }
183
184         try {
185             if (cl != null) {
186                 return cl.loadClass(name);
187             } else {
188                 return Class.forName(name);
189             }
190         } catch (ClassNotFoundException JavaDoc e) {
191             // Test if the name of the class is actually a request for
192
// a stub class to be created
193
if (Utils.isStubClassName(name)) {
194                 logger.info("Generating class : " + name);
195
196                 String JavaDoc classname = Utils.convertStubClassNameToClassName(name);
197
198                 //ASM is now the default bytecode manipulator
199
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                     // that shouldn't happen, unless someone manually sets the BYTE_CODE_MANIPULATOR static variable
212
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                 // System.out.println ("Classfile created with length "+data.length);
219
// Now, try to define the class
220
// We use the method defineClass, as redefined in class SecureClassLoader,
221
// so that we can specify a SourceCode object
222
// Class c = this.defineClass(name, data, 0, data.length, this.getClass().getProtectionDomain().getCodeSource());
223
// this.getParent().findClass("toto");
224
// Class c = this.getParent().defineClass(name, data, 0, data.length, this.getClass().getProtectionDomain());
225
// The following code invokes defineClass on the parent classloader by Reflection
226
// System.out.println("XXXXXXXXXXXXXXX");
227
try {
228                     Class JavaDoc clc = Class.forName("java.lang.ClassLoader");
229                     Class JavaDoc[] argumentTypes = new Class JavaDoc[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 JavaDoc m = clc.getDeclaredMethod("defineClass",
238                             argumentTypes);
239                     m.setAccessible(true);
240
241                     Object JavaDoc[] effectiveArguments = new Object JavaDoc[5];
242                     effectiveArguments[0] = name;
243                     effectiveArguments[1] = data;
244                     effectiveArguments[2] = new Integer JavaDoc(0);
245                     effectiveArguments[3] = new Integer JavaDoc(data.length);
246                     effectiveArguments[4] = this.getClass().getProtectionDomain();
247
248                     // System.out.println("");
249
if (this.getParent() == null) {
250                         return (Class JavaDoc) m.invoke(Thread.currentThread()
251                                                       .getContextClassLoader(),
252                             effectiveArguments);
253                     } else {
254                         return (Class JavaDoc) m.invoke(this.getParent(),
255                             effectiveArguments);
256                     }
257                 } catch (Exception JavaDoc ex) {
258                     ex.printStackTrace();
259                     throw new ClassNotFoundException JavaDoc(ex.getMessage());
260                 }
261             } else {
262                 System.out.println("Cannot generate class " + name);
263                 throw e;
264             }
265         }
266     }
267 }
268
269
270 //=======
271
//
272
// static Logger logger = Logger.getLogger(MOPClassLoader.class.getName());
273
//
274
// // retreives the optionnal byteCodeManipulator JVM arg
275
// // ASM is used by default
276
// public static String BYTE_CODE_MANIPULATOR =
277
// ((System.getProperty("byteCodeManipulator") != null)
278
// ? ((System.getProperty("byteCodeManipulator").equals("BCEL")) ? "BCEL" : "ASM")
279
// : "ASM");
280
//
281
// protected static Hashtable classDataCache = new Hashtable();
282
//
283
// public static byte[] getClassData(String classname) {
284
// return (byte[]) classDataCache.get(classname);
285
// }
286
//
287
// public MOPClassLoader(ClassLoader parent, URL[] urls) {
288
// super(urls, parent);
289
// }
290
//
291
// public void launchMain(String[] args) throws Throwable {
292
// try {
293
// // Looks up the class that contains main
294
// Class cl = Class.forName(args[0], true, this);
295
//
296
// // Looks up method main
297
// Class[] argTypes = { args.getClass()};
298
// Method mainMethod = cl.getMethod("main", argTypes);
299
//
300
// // And calls it
301
// String[] newArgs = new String[args.length - 1];
302
// System.arraycopy(args, 1, newArgs, 0, args.length - 1);
303
//
304
// Object[] mainArgs = { newArgs };
305
// mainMethod.invoke(null, mainArgs);
306
// } catch (ClassNotFoundException e) {
307
// logger.error("Launcher: cannot find class " + args[0]);
308
// } catch (NoSuchMethodException e) {
309
// logger.error(
310
// "Launcher: class " + args[0] + " does not contain have method void 'public void main (String[])'");
311
// } catch (InvocationTargetException e) {
312
// throw e.getTargetException();
313
// }
314
//
315
// return;
316
// }
317
//
318
// public static MOPClassLoader createMOPClassLoader() {
319
// // Gets the current classloader
320
// ClassLoader currentClassLoader = null;
321
//
322
// try {
323
// Class c = Class.forName("org.objectweb.proactive.core.mop.MOPClassLoader");
324
// currentClassLoader = c.getClassLoader();
325
// } catch (ClassNotFoundException e) {
326
// e.printStackTrace();
327
// }
328
//
329
// // Checks if the current classloader is actually an instance of
330
// // java.net.URLClassLoader, or of one of its subclasses.
331
// if (currentClassLoader instanceof java.net.URLClassLoader) {
332
// // System.out.println ("Current classloader is of type "+currentClassLoader.getClass().getName()+", compatible with URLClassLoader");
333
// } else {
334
// logger.error(
335
// "Current classloader is of type "
336
// + currentClassLoader.getClass().getName()
337
// + ", which is not compatible with URLClassLoader. Cannot install MOPClassLoader");
338
//
339
// return null;
340
// }
341
//
342
// // Retrieves the set of URLs from the current classloader
343
// URL[] urls = ((URLClassLoader) currentClassLoader).getURLs();
344
//
345
// // Creates a new MOPClassLoader
346
// return new MOPClassLoader(currentClassLoader, urls);
347
// }
348
//
349
// protected Class findClass(String name) throws ClassNotFoundException {
350
// return super.findClass(name);
351
// }
352
//
353
// public Class loadClass(String name) throws ClassNotFoundException {
354
// return this.loadClass(name, null, false);
355
// }
356
//
357
//
358
// public Class loadClass(String name, ClassLoader cl) throws ClassNotFoundException {
359
// return this.loadClass(name, cl, false);
360
// }
361
//
362
// protected synchronized Class loadClass(String name, ClassLoader cl, boolean resolve) throws ClassNotFoundException {
363
// if (this.getParent() != null) {
364
// try {
365
// return this.getParent().loadClass(name);
366
// } catch (ClassNotFoundException e) {
367
// // proceeding
368
// }
369
// }
370
//
371
// try {
372
// return cl.loadClass(name);
373
// } catch (ClassNotFoundException e) {
374
// // Test if the name of the class is actually a request for
375
// // a stub class to be created
376
// if (Utils.isStubClassName(name)) {
377
// logger.info("Generating class: " + name);
378
//
379
// String classname = Utils.convertStubClassNameToClassName(name);
380
// //ASM is now the default bytecode manipulator
381
// byte[] data = null;
382
// if (BYTE_CODE_MANIPULATOR.equals("ASM")) {
383
// ASMBytecodeStubBuilder bsb = new ASMBytecodeStubBuilder(classname);
384
// long start_time = System.currentTimeMillis();
385
// data = bsb.create();
386
// MOPClassLoader.classDataCache.put(name, data);
387
// } else if (BYTE_CODE_MANIPULATOR.equals("BCEL")) {
388
// BytecodeStubBuilder bsb = new BytecodeStubBuilder(classname);
389
// long start_time = System.currentTimeMillis();
390
// data = bsb.create();
391
// MOPClassLoader.classDataCache.put(name, data);
392
// } else {
393
// // that shouldn't happen, unless someone manually sets the BYTE_CODE_MANIPULATOR static variable
394
// logger.error(
395
// "byteCodeManipulator argument is optionnal. If specified, it can only be set to BCEL.");
396
// logger.error(
397
// "Any other setting will result in the use of ASM, the default bytecode manipulator framework");
398
// }
399
//
400
// // System.out.println ("Classfile created with length "+data.length);
401
// // Now, try to define the class
402
// // We use the method defineClass, as redefined in class SecureClassLoader,
403
// // so that we can specify a SourceCode object
404
// // Class c = this.defineClass(name, data, 0, data.length, this.getClass().getProtectionDomain().getCodeSource());
405
// // this.getParent().findClass("toto");
406
// // Class c = this.getParent().defineClass(name, data, 0, data.length, this.getClass().getProtectionDomain());
407
// // The following code invokes defineClass on the parent classloader by Reflection
408
// try {
409
// Class clc = Class.forName("java.lang.ClassLoader");
410
// Class[] argumentTypes = new Class[5];
411
// argumentTypes[0] = name.getClass();
412
// argumentTypes[1] = data.getClass();
413
// argumentTypes[2] = Integer.TYPE;
414
// argumentTypes[3] = Integer.TYPE;
415
// argumentTypes[4] = Class.forName("java.security.ProtectionDomain");
416
//
417
// Method m = clc.getDeclaredMethod("defineClass", argumentTypes);
418
// m.setAccessible(true);
419
//
420
// Object[] effectiveArguments = new Object[5];
421
// effectiveArguments[0] = name;
422
// effectiveArguments[1] = data;
423
// effectiveArguments[2] = new Integer(0);
424
// effectiveArguments[3] = new Integer(data.length);
425
// effectiveArguments[4] = this.getClass().getProtectionDomain();
426
//
427
// return (Class) m.invoke(this.getParent(), effectiveArguments);
428
// } catch (Exception ex) {
429
// throw new ClassNotFoundException(ex.getMessage());
430
// }
431
// } else {
432
// logger.error("Cannot generate class " + name);
433
// throw e;
434
// }
435
// }
436
// }
437
//}>>>>>>> 1.9
438
Popular Tags