KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > instrument > classloading > InstrumentationLoadTimeWeaver


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.instrument.classloading;
18
19 import java.lang.instrument.ClassFileTransformer JavaDoc;
20 import java.lang.instrument.Instrumentation JavaDoc;
21
22 import org.springframework.instrument.InstrumentationSavingAgent;
23 import org.springframework.util.Assert;
24 import org.springframework.util.ClassUtils;
25
26 /**
27  * Load time weaver relying on Instrumentation.
28  * Start with java agent, with JVM options like:
29  * <code>
30  * -javaagent:path/to/spring-agent.jar
31  * </code>
32  * where <code>spring-agent.jar</code> is a JAR file
33  * containing the InstrumentationSavingAgent class.
34  *
35  * <p>In Eclipse, for example, set the Run configuration's JVM
36  * args to be of the form:
37  * <code>
38  * -javaagent:${project_loc}/lib/spring-agent.jar
39  * </code>
40  *
41  * @author Rod Johnson
42  * @since 2.0
43  * @see InstrumentationSavingAgent
44  */

45 public class InstrumentationLoadTimeWeaver implements LoadTimeWeaver {
46
47     public void addTransformer(ClassFileTransformer JavaDoc transformer) {
48         Assert.notNull(transformer, "Transformer must not be null");
49         Instrumentation JavaDoc instrumentation = InstrumentationSavingAgent.getInstrumentation();
50         if (instrumentation == null) {
51             throw new IllegalStateException JavaDoc(
52                     "Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.");
53         }
54         instrumentation.addTransformer(transformer);
55     }
56
57     /**
58      * We have the ability to weave the current class loader when starting the
59      * JVM in this way, so the instrumentable class loader will always be the
60      * current loader.
61      */

62     public ClassLoader JavaDoc getInstrumentableClassLoader() {
63         return ClassUtils.getDefaultClassLoader();
64     }
65
66     /**
67      * This implementation always returns a SimpleThrowawayClassLoader.
68      * @see SimpleThrowawayClassLoader
69      */

70     public ClassLoader JavaDoc getThrowawayClassLoader() {
71         return new SimpleThrowawayClassLoader(getInstrumentableClassLoader());
72     }
73
74 }
75
Popular Tags