KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > jazzpect > Initializer


1 package alt.jiapi.jazzpect;
2
3 import org.apache.log4j.Category;
4
5 import org.aopalliance.intercept.MethodInterceptor;
6
7 import alt.jiapi.InstrumentationContext;
8 import alt.jiapi.InstrumentationDescriptor;
9 import alt.jiapi.JiapiException;
10 import alt.jiapi.interceptor.InvocationInterceptor;
11 import alt.jiapi.util.Bootstrapper;
12 import alt.jiapi.util.InstrumentingClassLoader;
13
14 import alt.jiapi.jazzpect.interceptor.MInterceptor;
15
16 /**
17  * Class Initializer.
18  *
19  * @author Mika Riekkinen
20  */

21 public class Initializer {
22     private static Category log = Category.getInstance(Initializer.class);
23
24     private InstrumentationContext ctx;
25     private ClassLoader JavaDoc classLoader;
26
27     public Initializer(MethodInterceptor mi) throws JiapiException {
28         this(new String JavaDoc[] {"*"},
29              new String JavaDoc[] {"java.*", "org.*", "javax.*"},
30              "*", mi);
31     }
32
33     public Initializer(String JavaDoc inclusionRule, MethodInterceptor mi) throws JiapiException {
34         this(new String JavaDoc[] {inclusionRule}, null, "*", mi);
35     }
36
37     public Initializer(String JavaDoc[] inclusionRules, MethodInterceptor mi) throws JiapiException {
38         this(inclusionRules, null, "*", mi);
39     }
40
41     public Initializer(String JavaDoc[] inclusionRules, String JavaDoc[] exclusionRules,
42                        String JavaDoc resolution, MethodInterceptor mi) throws JiapiException {
43         this.ctx = new InstrumentationContext();
44         InstrumentationDescriptor id = new InstrumentationDescriptor();
45         ctx.addInstrumentationDescriptor(id);
46
47         if (inclusionRules != null) {
48             for (int i = 0; i < inclusionRules.length; i++) {
49                 if (inclusionRules[i] != null) {
50                     id.addInclusionRule(inclusionRules[i]);
51                 }
52                 else {
53                     log.warn("Skipping null inclusion rule");
54                 }
55             }
56         }
57
58         if (exclusionRules != null) {
59             for (int i = 0; i < exclusionRules.length; i++) {
60                 if (exclusionRules[i] != null) {
61                     id.addExclusionRule(exclusionRules[i]);
62                 }
63                 else {
64                     log.warn("Skipping null exclusion rule");
65                 }
66             }
67         }
68         
69         if (resolution == null) {
70             log.warn("Null resolution, no invocation is trapped");
71             resolution = ""; // Instrument nothing(but dummy "<clinit>")
72
}
73
74         // Associate interceptor with descriptor
75
InvocationInterceptor ii =
76             new InvocationInterceptor(id, resolution, new MInterceptor(mi));
77
78         this.classLoader = InstrumentingClassLoader.createClassLoader(ctx);
79     }
80
81
82     /**
83      * Gets the context, that specifies which classes are to be instrumented.
84      */

85     public InstrumentationContext getContext() {
86         return ctx;
87     }
88
89
90     /**
91      * Gets a ClassLoader, that instruments classes for interception
92      * according to rules given.
93      *
94      * @return a ClassLoader;
95      */

96     public ClassLoader JavaDoc getClassLoader() {
97         return classLoader;
98     }
99
100
101     /**
102      * Runs a main method of given className.
103      * Instrumentation is done according to rules given.
104      */

105     public void runMainMethod(String JavaDoc className, String JavaDoc[] args) {
106         Bootstrapper.launch(className, args, getContext(), getClassLoader());
107     }
108 }
109
Popular Tags