KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > InstrumentationContext


1 /*
2  * Copyright(C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.log4j.Category;
26
27 import alt.jiapi.reflect.JiapiClass;
28 import alt.jiapi.reflect.Loader;
29
30 /**
31  * This class acts as a glue, that binds all the classes needed in
32  * instrumentation together. Mainly, it contains a List of
33  * InstrumentationDescriptors which define what classes will be
34  * instrumented and how.
35  *
36  * @author Mika Riekkinen
37  * @author Joni Suominen
38  * @version $Revision: 1.23 $ $Date: 2004/08/04 08:58:04 $
39  */

40 public class InstrumentationContext {
41     private static Category log = Runtime.getLogCategory(InstrumentationContext.class);
42
43     private List JavaDoc descriptors = new ArrayList JavaDoc();
44     private Loader loader;
45
46     /**
47      * Creates a context.
48      */

49     public InstrumentationContext() {
50         loader = new Loader();
51     }
52
53     /**
54      * Adds an instrumentation descriptor to this context.
55      * Descriptor contains all the items, that makes it possible to
56      * bind together inclusion rules and instrumentors. And exclusion rules.
57      * @param id InstrumentationDescriptor
58      * @see alt.jiapi.InstrumentationDescriptor
59      */

60     public void addInstrumentationDescriptor(InstrumentationDescriptor id) {
61         descriptors.add(id);
62     }
63
64     /**
65      * Gets a Loader which is configured with this context.
66      *
67      * @return a Loader
68      */

69     public Loader getLoader() {
70         return loader;
71     }
72
73     /**
74      * Get a List of <code>InstrumentationDescriptor</code>s added to
75      * this context.
76      *
77      * @return a List of descriptors. Returned List is never null.
78      */

79     public List JavaDoc getDescriptors() {
80         return descriptors;
81     }
82
83     /**
84      * Manipulates the class by configured Instrumentors. All of the
85      * InstrumentorChains that are matched with inclusion rule for the
86      * given class, will be started.
87      *
88      * @param clazz JiapiClass to instrument
89      */

90     public void instrument(JiapiClass clazz) {
91         List JavaDoc list = new ArrayList JavaDoc();
92         for (Iterator JavaDoc i = getDescriptors().iterator(); i.hasNext(); ) {
93             InstrumentationDescriptor id = (InstrumentationDescriptor)i.next();
94
95             if (id.match(clazz.getName())) {
96                 list.addAll(id.getInstrumentors());
97             }
98         }
99
100         if (!list.isEmpty()) {
101             Iterator JavaDoc i = list.iterator();
102             long l1 = System.currentTimeMillis();
103             while(i.hasNext()) {
104                 Instrumentor instrumentor = (Instrumentor)i.next();
105                 instrumentor.instrument(clazz);
106             }
107             long l2 = System.currentTimeMillis();
108
109             log.debug("It took " + (l2-l1) + " ms to instrument " + clazz);
110         }
111         else {
112             log.debug("No inclusion rules match " + clazz.getName());
113         }
114     }
115 }
116
Popular Tags