01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20: import java.lang.instrument.Instrumentation;
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 transformer) {
48: Assert.notNull(transformer, "Transformer must not be null");
49: Instrumentation instrumentation = InstrumentationSavingAgent
50: .getInstrumentation();
51: if (instrumentation == null) {
52: throw new IllegalStateException(
53: "Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.");
54: }
55: instrumentation.addTransformer(transformer);
56: }
57:
58: /**
59: * We have the ability to weave the current class loader when starting the
60: * JVM in this way, so the instrumentable class loader will always be the
61: * current loader.
62: */
63: public ClassLoader getInstrumentableClassLoader() {
64: return ClassUtils.getDefaultClassLoader();
65: }
66:
67: /**
68: * This implementation always returns a SimpleThrowawayClassLoader.
69: * @see SimpleThrowawayClassLoader
70: */
71: public ClassLoader getThrowawayClassLoader() {
72: return new SimpleThrowawayClassLoader(
73: getInstrumentableClassLoader());
74: }
75:
76: }
|