01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ContinuationsAgent.java 3811 2007-06-25 15:06:16Z gbevin $
07: */
08: package com.uwyn.rife.continuations.instrument;
09:
10: import com.uwyn.rife.continuations.ContinuationConfigInstrument;
11: import com.uwyn.rife.instrument.FinalTransformer;
12: import com.uwyn.rife.instrument.InitialTransformer;
13: import java.lang.instrument.Instrumentation;
14:
15: /**
16: * Provides a continuations instrumentation agent that will modify
17: * the bytecode of the classes that are loaded. It enhances the classes with
18: * continuations capabilities that are otherwise provided by a class-loader.
19: * <p>To activate the agent you need to execute the Java application with the
20: * proper argument, for example:
21: * <pre>java -javaagent:/path/to/rife-continuations-agent-1.6-jdk15.jar=com.your.ContinuationConfigInstrumentClass com.your.mainClass</pre>
22: * <p>When the agent is active the {@link com.uwyn.rife.continuations.basic.BasicContinuableClassLoader} will
23: * automatically be disabled to ensure that they are not conflicting with each
24: * other. The agent is packaged in its own jar file which should correspond
25: * to the RIFE/Continuations version that you are using in your application.
26: * <p>It is possible to debug the bytecode instrumentation by using the
27: * functionatilies provided by the {@link InitialTransformer} and
28: * {@link FinalTransformer} transformers that are included in this agent.
29: *
30: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
31: * @version $Revision: 3811 $
32: * @since 1.6
33: */
34: public class ContinuationsAgent {
35: public final static String AGENT_ACTIVE_PROPERTY = "rife.agent.active";
36:
37: public static void premain(String agentArguments,
38: Instrumentation instrumentation) {
39: if (null == agentArguments)
40: throw new IllegalArgumentException(
41: "expecting the fully qualified class name of a ContinuationConfigInstrument class");
42: ContinuationConfigInstrument config = null;
43: try {
44: Class config_class = Class.forName(agentArguments);
45: config = (ContinuationConfigInstrument) config_class
46: .newInstance();
47: } catch (Exception e) {
48: throw new RuntimeException(
49: "Unexpected error while creating an instance of the instrumentation configuration with class name '"
50: + agentArguments + "'", e);
51: }
52:
53: System.getProperties().setProperty(AGENT_ACTIVE_PROPERTY,
54: Boolean.TRUE.toString());
55:
56: instrumentation.addTransformer(new InitialTransformer());
57: instrumentation.addTransformer(new ContinuationsTransformer(
58: config));
59: instrumentation.addTransformer(new FinalTransformer());
60: }
61: }
|