KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > extension > jrockit > JRockitPreProcessor


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.extension.jrockit;
9
10 import org.codehaus.aspectwerkz.hook.ClassPreProcessor;
11 import org.codehaus.aspectwerkz.hook.impl.ClassPreProcessorHelper;
12 import com.bea.jvm.JVMFactory;
13 import com.jrockit.management.rmp.RmpSocketListener;
14
15 /**
16  * JRockit (tested with 7SP4 and 8.1) preprocessor Adapter based on JMAPI <p/>JRockit has a low
17  * level API for hooking ClassPreProcessor, allowing the use of online weaving at full speed.
18  * Moreover, JRockit does not allow java.lang.ClassLoader overriding thru -Xbootclasspath/p option.
19  * <p/>The main difference with standard AspectWerkz online mode is that the ClassPreProcessor
20  * implementation and all third party jars CAN reside in the standard classpath. <p/>The command
21  * line tool will look like:
22  * <code>"%JAVA_COMMAND%" -Xmanagement:class=org.codehaus.aspectwerkz.extension.jrockit.JRockitPreProcessor
23  * -cp "%ASPECTWERKZ_HOME%\target\aspectwerkz-extensions-%ASPECTWERKZ_VERSION%.jar;%ASPECTWERKZ_HOME%\lib\aspectwerkz-core-%ASPECTWERKZ_VERSION%.jar;%ASPECTWERKZ_HOME%\lib\aspectwerkz-%ASPECTWERKZ_VERSION%.jar;%ASPECTWERKZ_LIBS%"
24  * -Daspectwerkz.home="%ASPECTWERKZ_HOME%" -Daspectwerkz.transform.verbose=yes %*</code>
25  * Note: there can be some NoClassDefFoundError due to classpath limitation - as described in
26  * http://edocs.bea.com/wls/docs81/adminguide/winservice.html <p/>In order to use the BEA JRockit
27  * management server (for further connection of management console or runtime analyzer), the regular
28  * option -Xmanagement will not have any effect. Instead, use <code>-Dmanagement</code>.
29  *
30  * @author <a HREF="mailto:alex@gnilux.com">Alexandre Vasseur </a>
31  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
32  */

33 public class JRockitPreProcessor implements com.bea.jvm.ClassPreProcessor {
34
35     /**
36      * Concrete AspectWerkz preprocessor.
37      */

38     private static ClassPreProcessor s_preProcessor;
39
40     private static boolean START_RMP_SERVER = false;
41
42     static {
43         String JavaDoc clpp = System.getProperty(
44                 "aspectwerkz.classloader.preprocessor",
45                 "org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor"
46         );
47         START_RMP_SERVER = System.getProperties().containsKey("management");
48         try {
49             // note: CLPP loaded by current thread classloader which is bootstrap classloader
50
// caution: forcing loading thru Thread.setContextClassLoader() or
51
// ClassLoader.getSystemClassLoader()
52
// does not work. We then do a filtering on the caller classloader - see preProcess(..)
53
//preProcessor = (ClassPreProcessor) Class.forName(clpp).newInstance();
54
s_preProcessor = ClassPreProcessorHelper.getClassPreProcessor();
55             //(ClassPreProcessor)ClassLoader.getSystemClassLoader().loadClass(clpp).newInstance();
56
//s_preProcessor.initialize(null);
57
} catch (Exception JavaDoc e) {
58             throw new ExceptionInInitializerError JavaDoc("could not initialize jrockit preprocessor due to: " + e.toString());
59         }
60     }
61
62     /**
63      * The JMAPI ClassPreProcessor must be self registrating
64      */

65     public JRockitPreProcessor() {
66         if (START_RMP_SERVER) {
67             // the management server will be spawned in a new thread
68
RmpSocketListener management = new RmpSocketListener();
69         }
70         JVMFactory.getJVM().getClassLibrary().setClassPreProcessor(this);
71     }
72
73     /**
74      * Weave a class
75      *
76      * @param caller classloader
77      * @param name of the class to weave
78      * @param bytecode original
79      * @return bytecode weaved
80      */

81     public byte[] preProcess(ClassLoader JavaDoc caller, String JavaDoc name, byte[] bytecode) {
82         if (caller == null || caller.getParent() == null) {
83             return bytecode;
84         } else {
85             return s_preProcessor.preProcess(name, bytecode, caller);
86         }
87     }
88
89     /**
90      * Utility for testing
91      */

92     public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
93         // uncomment this lines to programmaticaly configure at runtime the CLPP
94
JRockitPreProcessor pp = new JRockitPreProcessor();//self registration
95
Class JavaDoc loadedCP = Class.forName("java.math.BigDecimal");
96         while (true) {
97             System.out.print(".");
98             /*
99              * ClassLoader nonDelegatingCL = new VerifierClassLoader( new URL[]{(new
100              * File(args[0])).toURL()}, ClassLoader.getSystemClassLoader() ); Class loaded =
101              * nonDelegatingCL.loadClass(
102              * org.codehaus.aspectwerkz.extension.jrockit.JRockitPreProcessor.class.getName() );
103              */

104             Thread.sleep(500);
105         }
106     }
107 }
Popular Tags