You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by da...@apache.org on 2007/03/07 22:29:17 UTC

svn commit: r515775 - /incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java

Author: dain
Date: Wed Mar  7 13:29:16 2007
New Revision: 515775

URL: http://svn.apache.org/viewvc?view=rev&rev=515775
Log:
Added code to deal with situation where Agent class is force loaded into a second class loader.  This will happen with child first delegation class loader models

Modified:
    incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java

Modified: incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java
URL: http://svn.apache.org/viewvc/incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java?view=diff&rev=515775&r1=515774&r2=515775
==============================================================================
--- incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java (original)
+++ incubator/openejb/trunk/openejb3/container/openejb-javaagent/src/main/java/org/apache/openejb/javaagent/Agent.java Wed Mar  7 13:29:16 2007
@@ -19,19 +19,25 @@
 
 import java.lang.instrument.Instrumentation;
 import java.lang.reflect.ReflectPermission;
+import java.lang.reflect.Field;
 import java.security.Permission;
 
 public class Agent {
     private static final Permission ACCESS_PERMISSION = new ReflectPermission("suppressAccessChecks");
     private static String agentArgs;
     private static Instrumentation instrumentation;
+    private static boolean initialized = false;
 
-    public static void premain(String agentArgs, Instrumentation instrumentation) {
+    public static synchronized void premain(String agentArgs, Instrumentation instrumentation) {
         Agent.agentArgs = agentArgs;
         Agent.instrumentation = instrumentation;
+        initialized = true;
     }
 
-    public static String getAgentArgs() {
+    public static synchronized String getAgentArgs() {
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
+        checkInitialization();
         return agentArgs;
     }
 
@@ -40,9 +46,31 @@
      * You must have java.lang.ReflectPermission(suppressAccessChecks) to call this method
      * @return the instrumentation instance
      */
-    public static Instrumentation getInstrumentation() {
+    public static synchronized Instrumentation getInstrumentation() {
         SecurityManager sm = System.getSecurityManager();
         if (sm != null) sm.checkPermission(ACCESS_PERMISSION);
+        checkInitialization();
         return instrumentation;
+    }
+
+    private static synchronized void checkInitialization() {
+        if (!initialized) {
+            try {
+                ClassLoader systemCl = ClassLoader.getSystemClassLoader();
+                Class<?> systemAgentClass = systemCl.loadClass(Agent.class.getName());
+
+                Field instrumentationField = systemAgentClass.getDeclaredField("instrumentation");
+                instrumentationField.setAccessible(true);
+                instrumentation = (Instrumentation) instrumentationField.get(null);
+
+                Field agentArgsField = systemAgentClass.getDeclaredField("agentArgs");
+                agentArgsField.setAccessible(true);
+                agentArgs = (String) agentArgsField.get(null);
+
+                initialized = true;
+            } catch (Exception e) {
+                throw new IllegalStateException("Unable to initialize agent", e);
+            }
+        }
     }
 }