[jboss-svn-commits] JBL Code SVN: r27002 - in labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman: rule and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 18 04:06:50 EDT 2009


Author: adinn
Date: 2009-06-18 04:06:50 -0400 (Thu, 18 Jun 2009)
New Revision: 27002

Modified:
   labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/agent/Transformer.java
   labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/Rule.java
   labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/compiler/Compiler.java
Log:
loaded the generated adapter classes via the trigger class loader, ensuring that references to application classes in compiled rule code are resolved correctly.

Modified: labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/agent/Transformer.java
===================================================================
--- labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/agent/Transformer.java	2009-06-18 06:48:41 UTC (rev 27001)
+++ labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/agent/Transformer.java	2009-06-18 08:06:50 UTC (rev 27002)
@@ -292,48 +292,6 @@
         }
     }
 
-    /**
-     * this is a classloader used to define classes from bytecode
-     *
-     * TODO -- we probably need to use the protection domain of the trigger class somewhere here
-     */
-    private static class ClassbyteClassLoader extends ClassLoader
-    {
-        ClassbyteClassLoader(ClassLoader cl)
-        {
-            super(cl);
-        }
-        
-        public Class addClass(String name, byte[] bytes)
-                throws ClassFormatError
-        {
-            Class cl = defineClass(name, bytes, 0, bytes.length);
-            resolveClass(cl);
-
-            return cl;
-        }
-    }
-
-    /**
-     * a singleton instance of the classloader used to define classes from bytecode
-     */
-    private static ClassbyteClassLoader theLoader = new ClassbyteClassLoader(Transformer.class.getClassLoader());
-
-    /**
-     * a helper method which allows dynamic creation of generated helper adapter classes
-     * @param helperAdapterName
-     * @param classBytes
-     * @return
-     */
-    public Class<?> loadHelperAdapter(Class<?> helperClass, String helperAdapterName, byte[] classBytes)
-    {
-         //return theLoader.addClass(helperAdapterName, classBytes);
-
-        ClassbyteClassLoader loader = new ClassbyteClassLoader(helperClass.getClassLoader());
-
-        return loader.addClass(helperAdapterName, classBytes);
-    }
-
     /* switches controlling behaviour of transformer */
 
     /**

Modified: labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/Rule.java
===================================================================
--- labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/Rule.java	2009-06-18 06:48:41 UTC (rev 27001)
+++ labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/Rule.java	2009-06-18 08:06:50 UTC (rev 27002)
@@ -59,6 +59,10 @@
      */
     private String name;
     /**
+     * the class loader for the target class
+     */
+    private ClassLoader loader;
+    /**
      * the name of the target class for this rule supplied in the rule script
      */
     private String targetClass;
@@ -183,6 +187,7 @@
             action = Action.create(this, actionTree);
         }
         checked = false;
+        this.loader = loader;
         this.targetClass = targetClass;
         this.targetMethod = targetMethod;
         this.helperClass = (helperClass != null ? helperClass : Helper.class);
@@ -241,7 +246,16 @@
     {
         return returnType;
     }
-    
+
+    /**
+     * get the class loader of the target class for the rule
+     * @return
+     */
+    public ClassLoader getLoader()
+    {
+        return loader;
+    }
+
     public static Rule create(String name, String targetClass, String targetMethod, Class<?> helperClass, Location targetLocation, String ruleSpec, int line, String file, ClassLoader loader)
             throws ParseException, TypeException, CompileException
     {

Modified: labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/compiler/Compiler.java
===================================================================
--- labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/compiler/Compiler.java	2009-06-18 06:48:41 UTC (rev 27001)
+++ labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/byteman/rule/compiler/Compiler.java	2009-06-18 08:06:50 UTC (rev 27002)
@@ -76,7 +76,9 @@
                 // dump the compiled class bytes if required
                 Transformer.getTheTransformer().maybeDumpClass(externalName, classBytes);
                 // ensure the class is loaded
-                adapterClass = Transformer.getTheTransformer().loadHelperAdapter(helperClass, externalName, classBytes);
+                // think we need to load the generated helper using the class loader of the trigger class
+                ClassLoader loader = rule.getLoader();
+                adapterClass = loadHelperAdapter(loader, externalName, classBytes);
             } catch(CompileException ce) {
                 throw ce;
             } catch (Throwable th) {
@@ -517,4 +519,46 @@
     {
         return ++nextId;
     }
+
+    /**
+     * this is a classloader used to define classes from bytecode
+     */
+    private static class ClassbyteClassLoader extends ClassLoader
+    {
+        ClassbyteClassLoader(ClassLoader cl)
+        {
+            super(cl);
+        }
+
+        public Class addClass(String name, byte[] bytes)
+                throws ClassFormatError
+        {
+            Class cl = defineClass(name, bytes, 0, bytes.length);
+            resolveClass(cl);
+
+            return cl;
+        }
+    }
+
+    /**
+     * dynamically load and return a generated helper adapter classes using a custom classloader derived from the
+     * trigger class's loader
+     * @param triggerClassLoader the class loader of the trigger class which has been matched with this
+     * helper class's rule
+     * @param helperAdapterName the name of the helper adaptter class to be loaded
+     * @param classBytes the byte array defining the class
+     * @return
+     */
+    public static Class<?> loadHelperAdapter(ClassLoader triggerClassLoader, String helperAdapterName, byte[] classBytes)
+    {
+        // create the helper class in a classloader derived from the trigger class
+        // this allows the injected code to refer to the triggger class type and related
+        // application types. the defalt helper will be accessible because it is loaded bby the
+        // ootstrap loader. custom helpers need to be made avvailable to the applicattion either
+        // by deployng them with it or by locating them in the JVM classpath.
+        ClassbyteClassLoader loader = new ClassbyteClassLoader(triggerClassLoader);
+
+        return loader.addClass(helperAdapterName, classBytes);
+    }
+
 }




More information about the jboss-svn-commits mailing list