You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2006/10/10 17:04:13 UTC
svn commit: r454772 -
/incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java
Author: aadamchik
Date: Tue Oct 10 08:04:12 2006
New Revision: 454772
URL: http://svn.apache.org/viewvc?view=rev&rev=454772
Log:
CAY-684 - adding relection based method to register class transformers with the agent
Modified:
incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java
Modified: incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java
URL: http://svn.apache.org/viewvc/incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java?view=diff&rev=454772&r1=454771&r2=454772
==============================================================================
--- incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java (original)
+++ incubator/cayenne/main/trunk/core/cayenne-jdk1.5/src/main/java/org/apache/cayenne/instrument/InstrumentUtil.java Tue Oct 10 08:04:12 2006
@@ -18,8 +18,12 @@
****************************************************************/
package org.apache.cayenne.instrument;
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
+import org.apache.cayenne.CayenneRuntimeException;
+
/**
* Instrumentation utilities.
*
@@ -29,22 +33,49 @@
public class InstrumentUtil {
/**
+ * Registers class transformer with the instrumentation agent. Throws an exception if
+ * the application wasn't started with CayenneAgent.
+ */
+ public static void addTransformer(ClassFileTransformer transformer) {
+ Instrumentation instrumentation;
+ try {
+ instrumentation = getInstrumentation();
+ }
+ catch (Throwable th) {
+ throw new CayenneRuntimeException("CayenneAgent is not started", th);
+ }
+
+ if (instrumentation == null) {
+ throw new CayenneRuntimeException("CayenneAgent is not started");
+ }
+
+ instrumentation.addTransformer(transformer);
+ }
+
+ /**
* Checks whether the JVM was started with CayenneAgent.
*/
public static boolean isAgentLoaded() {
// check whether CayenneAgent class is initialized and instrumentation is set.
try {
- Class agent = Class.forName(
- "org.apache.cayenne.instrument.CayenneAgent",
- false,
- Thread.currentThread().getContextClassLoader());
-
- Method getInstrumentation = agent.getDeclaredMethod("getInstrumentation");
- return getInstrumentation.invoke(null) != null;
+ return getInstrumentation() != null;
}
catch (Throwable th) {
return false;
}
+ }
+
+ /**
+ * Returns CayenneAgent instrumentation.
+ */
+ static Instrumentation getInstrumentation() throws Exception {
+ Class agent = Class.forName(
+ "org.apache.cayenne.instrument.CayenneAgent",
+ false,
+ Thread.currentThread().getContextClassLoader());
+
+ Method getInstrumentation = agent.getDeclaredMethod("getInstrumentation");
+ return (Instrumentation) getInstrumentation.invoke(null);
}
}