KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > agent > InstrumentationAgent


1 package alt.jiapi.agent;
2
3 import java.lang.instrument.ClassFileTransformer JavaDoc;
4 import java.lang.instrument.Instrumentation JavaDoc;
5 import java.security.ProtectionDomain JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
8
9 import alt.jiapi.reflect.JiapiClass;
10 import alt.jiapi.InstrumentationDescriptor;
11 import alt.jiapi.InstrumentationContext;
12 import alt.jiapi.interceptor.*;
13
14 import java.lang.reflect.InvocationHandler JavaDoc;
15
16 /**
17  * This class is an implementation of ClassFileTransformer and
18  * acts as a java-agent for jdk1.5+ virtual machines.<p>
19  * You can enable this agent from command line by adding <i>-javaagent</i>
20  * switch to command line like this:<br>
21  *
22  * <blockquote>
23  * <b><i>java -javaagent:<path_to_jiapi.jar> HelloWorld</i></b>
24  * </blockquote>
25  *
26  * One can give configuration parameters in command line to this agent
27  * and Transformer used. Parameters are given by appending the following
28  * to -javaagent switch:<p>
29  *
30  * <i>'=name1=value1,name2=value2,....'</i><p>
31  *
32  * This agent regoqnises one parameter:
33  * <i>'transformer=<name_of_the_transformer></i>. If this parameter is given,
34  * It is assumed to be a fully qualified name of a class, that extends
35  * <i>alt.jiapi.agent.Transformer</i>. If not given,
36  * <i>alt.jiapi.agent.HotSpotTransformer</i> is used. <p>
37  *
38  * @see alt.jiapi.agent.Transformer
39  * @see alt.jiapi.agent.HotSpotTransformer
40  */

41 public class InstrumentationAgent implements ClassFileTransformer JavaDoc {
42     private String JavaDoc agentArgs;
43     private Instrumentation JavaDoc instrumentation;
44     private Transformer transformer;
45
46     public static void premain(String JavaDoc agentArgs, Instrumentation JavaDoc i) {
47     Properties JavaDoc p = new Properties JavaDoc();
48     
49     // Parse agentArgs. Entries are separated by ','. Each entry
50
// looks like 'name=value'. Like
51
// a=b,c=d,e=f
52
if (agentArgs != null) {
53         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(agentArgs, ",");
54         while(st.hasMoreTokens()) {
55         String JavaDoc nv = st.nextToken();
56
57         StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(nv, "=");
58         String JavaDoc n = st2.nextToken();
59         String JavaDoc v = null;
60         if (st2.hasMoreTokens()) {
61             v = st2.nextToken();
62         }
63         else {
64             v = "";
65         }
66         
67         p.setProperty(n, v);
68         }
69     }
70
71     try {
72         // Get the class name of Transformer. Default to
73
// HotSpotTransformer
74
String JavaDoc tr = p.getProperty("transformer",
75                       "alt.jiapi.agent.HotSpotTransformer");
76         if (tr != null) {
77         Class JavaDoc c = Class.forName(tr);
78         
79         Object JavaDoc o = c.newInstance();
80
81         Transformer tf = (Transformer)o;
82         tf.init(p);
83
84         // Create and register InstrumentationAgent as transformer
85
InstrumentationAgent jcft =
86             new InstrumentationAgent(agentArgs, i, tf);
87         
88         i.addTransformer(jcft);
89         }
90         else {
91         System.out.println("Failed to create Transformer: Missing agent parameter 'transformer'");
92         // For testing purposes...
93
InstrumentationAgent jcft =
94             new InstrumentationAgent(agentArgs, i, null);
95         
96         i.addTransformer(jcft);
97         }
98     }
99     catch(Exception JavaDoc e) {
100         System.out.println("Failed to create Transformer: " + e);
101         e.printStackTrace();
102     }
103     }
104
105
106     public InstrumentationAgent(String JavaDoc agentArgs, Instrumentation JavaDoc i, Transformer transformer) {
107     this.agentArgs = agentArgs;
108     this.instrumentation = i;
109     this.transformer = transformer;
110     }
111
112     public byte[] transform(ClassLoader JavaDoc cl, String JavaDoc className,
113                 Class JavaDoc<?> classBeingRedefined,
114                 ProtectionDomain JavaDoc pd,
115                 byte[] classFileBuffer) {
116     if (transformer == null) {
117         return null;
118     }
119     else {
120         // trasform(...) API states, that we should make a
121
// copy of classFileBuffer
122
byte[] tempBuffer = new byte[classFileBuffer.length];
123         System.arraycopy(classFileBuffer, 0, tempBuffer, 0,
124                  classFileBuffer.length);
125
126         try {
127         JiapiClass jc = JiapiClass.parseClass(tempBuffer);
128
129         if (transformer.transform(jc)) {
130             return jc.getByteCode();
131         }
132         }
133         catch(Exception JavaDoc e) {
134         e.printStackTrace();
135         }
136
137         return null;
138     }
139     }
140 }
141
Popular Tags