[jboss-svn-commits] JBL Code SVN: r23559 - in labs/jbosstm/workspace/adinn/orchestration: docs and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Oct 23 07:45:26 EDT 2008


Author: adinn
Date: 2008-10-23 07:45:25 -0400 (Thu, 23 Oct 2008)
New Revision: 23559

Modified:
   labs/jbosstm/workspace/adinn/orchestration/README
   labs/jbosstm/workspace/adinn/orchestration/docs/ProgrammersGuide.odt
   labs/jbosstm/workspace/adinn/orchestration/docs/ProgrammersGuide.pdf
   labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/jbossts/orchestration/agent/Transformer.java
Log:
added configuraton of transformed class dumping via system properties

Modified: labs/jbosstm/workspace/adinn/orchestration/README
===================================================================
--- labs/jbosstm/workspace/adinn/orchestration/README	2008-10-23 08:26:57 UTC (rev 23558)
+++ labs/jbosstm/workspace/adinn/orchestration/README	2008-10-23 11:45:25 UTC (rev 23559)
@@ -23,7 +23,7 @@
 The agent is passed to the JVM using the -javaagent option of the java
 command by setting JAVA_OPTS as follows:
 
-export JAVA_OPTS="-javaagent:${HOME}/jboss/workspace/adinn/orchestration/build/lib/orchestration.jar=script:${HOME}/jboss/workspace/adinn/orchestration/handler.txt"
+export JAVA_OPTS="-javaagent:${HOME}/jboss/workspace/adinn/orchestration/build/lib/orchestration.jar=script:${HOME}/jboss/workspace/adinn/orchestration/handler.txt -Dorg.jboss.jbossts.orchestration.dump.generated.classes=yes -Dorg.jboss.jbossts.orchestration.dump.generated.classes.directory=$HOME/jboss/workspace/adinn/orchestration/dump"
 
 The =script:<scriptFile> option to the -javaagent argument tells the
 agent premain to search <scriptFile> for rules. Multiple scripts may
@@ -35,6 +35,21 @@
 recovery. See these (or the code :-) to get a taste of what the rule
 language offers (that's all foax, for now at least -- sorry).
 
+The agent can be configured to dump transformed byte code by setting
+several Java system properties as shown in the example above:
+
+  org.jboss.jbossts.orchestration.dump.generated.classes
+
+this can be set to any value to enable dumping of generated
+code. Files will be written in a directory tree correspnding to the
+package hierarchy for the associated class.
+
+  org.jboss.jbossts.orchestration.dump.generated.classes.directory
+
+this can be set to a directory below which files will be written. if
+unset files are written to the current working directory of the JVM
+thread executing the transformer code.
+
 Rule Definition
 ---------------
 

Modified: labs/jbosstm/workspace/adinn/orchestration/docs/ProgrammersGuide.odt
===================================================================
(Binary files differ)

Modified: labs/jbosstm/workspace/adinn/orchestration/docs/ProgrammersGuide.pdf
===================================================================
(Binary files differ)

Modified: labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/jbossts/orchestration/agent/Transformer.java
===================================================================
--- labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/jbossts/orchestration/agent/Transformer.java	2008-10-23 08:26:57 UTC (rev 23558)
+++ labs/jbosstm/workspace/adinn/orchestration/src/org/jboss/jbossts/orchestration/agent/Transformer.java	2008-10-23 11:45:25 UTC (rev 23559)
@@ -46,6 +46,7 @@
 import java.io.PrintWriter;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.File;
 
 /**
  * byte code transformer used to introduce orchestration events into JBoss code
@@ -94,7 +95,7 @@
                         name = line.substring(5).trim();
                     } else if (!inRule) {
                         if (!line.trim().equals("")) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: invalid text outside of RULE/ENDRULE " + "at line " + lineNumber + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : invalid text outside of RULE/ENDRULE " + "at line " + lineNumber + " in script " + scriptPaths.get(scriptIdx));
                         }
                     } else if (line.startsWith("CLASS ")) {
                         targetClass = line.substring(6).trim();
@@ -105,17 +106,17 @@
                         try {
                             targetLine = Integer.valueOf(lineSpec);
                         } catch (NumberFormatException e) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: invalid LINE specification " + lineSpec + "for RULE " + name + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : invalid LINE specification " + lineSpec + "for RULE " + name + " in script " + scriptPaths.get(scriptIdx));
                         }
                     } else if (line.startsWith("ENDRULE")) {
                         if (name == null) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: no matching RULE for ENDRULE at line " + lineNumber + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : no matching RULE for ENDRULE at line " + lineNumber + " in script " + scriptPaths.get(scriptIdx));
                         } else if (targetClass == null) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: no CLASS for RULE  " + name + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : no CLASS for RULE  " + name + " in script " + scriptPaths.get(scriptIdx));
                         } else if (targetMethod == null) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: no METHOD for RULE  " + name + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : no METHOD for RULE  " + name + " in script " + scriptPaths.get(scriptIdx));
                         } else if (targetMethod.startsWith("<init>") && targetLine < 0) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: rule for constructor METHOD " + targetMethod + " must specify source LINE in RULE " + name + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : rule for constructor METHOD " + targetMethod + " must specify source LINE in RULE " + name + " in script " + scriptPaths.get(scriptIdx));
                         } else {
                             List<Script> scripts = targetToScriptMap.get(targetClass);
                             if (scripts == null) {
@@ -138,7 +139,7 @@
                         sepr = "";
                         inRule = false;
                     } else if (lineNumber == maxLines && !nextRule.trim().equals("")) {
-                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer: no matching ENDRULE for RULE " + name + " in script " + scriptPaths.get(scriptIdx));
+                            throw new Exception("org.jboss.jbossts.orchestration.agent.Transformer : no matching ENDRULE for RULE " + name + " in script " + scriptPaths.get(scriptIdx));
                     } else {
                         nextRule += sepr + line;
                         sepr = "\n";
@@ -258,19 +259,9 @@
         }
 
         if (newBuffer != classfileBuffer) {
-            // switch on to dump transformed bytecode for checking
-            if (true) {
-                String name = (dotIdx < 0 ? internalClassName : internalClassName.substring(dotIdx + 1));
-                name += ".class";
-                System.out.println("Saving transformed bytes to " + name);
-                try {
-                    FileOutputStream fio = new FileOutputStream(name);
-                    fio.write(newBuffer);
-                    fio.close();
-                } catch (IOException ioe) {
-                    System.out.println("Error saving transformed bytes to" + name);
-                    ioe.printStackTrace(System.out);
-                }
+            // see if we need to dump the transformed bytecode for checking
+            if (dumpGeneratedClasses) {
+                dumpClass(internalClassName, newBuffer);
             }
             return newBuffer;
         } else {
@@ -278,13 +269,41 @@
         }
     }
 
+    /* switches controlling behaviour of transformer */
 
+    /**
+     * prefix for orchestration package
+     */
+    private static final String ORCHESTRATION_PACKAGE_PREFIX = "org.jboss.jbossts.orchestration.";
+
+    /**
+     * prefix for com.arjuna package
+     */
+    private static final String COM_ARJUNA_PACKAGE_PREFIX = "com.arjuna.";
+
+    /**
+     * prefix for org.jboss package
+     */
+    private static final String ORG_JBOSS_PACKAGE_PREFIX = "org.jboss.";
+
+    /**
+     * system property set (to any value) in order to switch on dumping of generated bytecode to .class files
+     */
+    public static final String DUMP_GENERATED_CLASSES = ORCHESTRATION_PACKAGE_PREFIX + "dump.generated.classes";
+
+    /* implementation */
+
+    /**
+     * system property identifying directory in which to dump generated bytecode .class files
+     */
+    public static final String DUMP_GENERATED_CLASSES_DIR = ORCHESTRATION_PACKAGE_PREFIX + "dump.generated.classes.directory";
+
     private byte[] transform(Script script, ClassLoader loader, String className, Class classBeingRedefined, byte[] targetClassBytes)
     {
         final String handlerClass = script.getTargetClass();
         final String handlerMethod = script.getTargetMethod();
         final int handlerLine = script.getTargetLine();
-        System.out.println("org.jboss.jbossts.orchestration.agent.Transformer: Inserting trigger event");
+        System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : Inserting trigger event");
         System.out.println("  class " + handlerClass);
         System.out.println("  method " + handlerMethod);
         System.out.println("  line " + handlerLine);
@@ -293,13 +312,13 @@
         try {
             rule = Rule.create(ruleName, handlerClass, handlerMethod, handlerLine, script.getRuleText(), loader);
         } catch (ParseException pe) {
-            System.out.println("Transformer : error parsing rule : " + pe);
+            System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : error parsing rule : " + pe);
             return targetClassBytes;
         } catch (TypeException te) {
-            System.out.println("Transformer : error checking rule : " + te);
+            System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : error checking rule : " + te);
             return targetClassBytes;
         } catch (Throwable th) {
-            System.out.println("Transformer : error processing rule : " + th);
+            System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : error processing rule : " + th);
             return targetClassBytes;
         }
         System.out.println(rule);
@@ -318,7 +337,7 @@
         try {
             cr.accept(checkAdapter, 0);
         } catch (Throwable th) {
-            System.out.println("Transformer : error applying rule " + rule.getName() + " to class " + className + th);
+            System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : error applying rule " + rule.getName() + " to class " + className + th);
             th.printStackTrace(System.out);
             return targetClassBytes;
         }
@@ -333,7 +352,7 @@
             try {
                 cr.accept(adapter, 0);
             } catch (Throwable th) {
-                System.out.println("Transformer : error compiling rule " + rule.getName() + " for class " + className + th);
+                System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : error compiling rule " + rule.getName() + " for class " + className + th);
                 th.printStackTrace(System.out);
                 return targetClassBytes;
             }
@@ -351,7 +370,7 @@
      */
     private boolean isOrchestrationClass(String className)
     {
-        return className.startsWith("org.jboss.jbossts.orchestration.");
+        return className.startsWith(ORCHESTRATION_PACKAGE_PREFIX);
     }
 
     /**
@@ -361,7 +380,7 @@
      */
     private boolean isTransformable(String className)
     {
-        return (className.startsWith("com.arjuna.") || className.startsWith("org.jboss."));
+        return (className.startsWith(COM_ARJUNA_PACKAGE_PREFIX) || className.startsWith(ORG_JBOSS_PACKAGE_PREFIX));
     }
     /**
      * the instrumentation interface to the JVM
@@ -416,4 +435,60 @@
             return ruleText;
         }
     }
+    /**
+     *  switch to control dumping of generated bytecode to .class files
+     */
+    private final static boolean dumpGeneratedClasses = (System.getProperty(DUMP_GENERATED_CLASSES) != null);
+
+    /**
+     *  directory in which to dump generated bytecode .class files (defaults to "."
+     */
+    private final static String dumpGeneratedClassesDir;
+
+    static {
+        String userDir = System.getProperty(DUMP_GENERATED_CLASSES_DIR);
+        if (userDir != null) {
+            File userFile = new File(userDir);
+            if (userFile.exists() && userFile.isDirectory() && userFile.canWrite()) {
+                dumpGeneratedClassesDir = userDir;
+            } else {
+                dumpGeneratedClassesDir = ".";
+            }
+        } else {
+            dumpGeneratedClassesDir =  ".";
+        }
+    }
+
+    private void dumpClass(String fullName, byte[] bytes)
+    {
+        int dotIdx = fullName.lastIndexOf('.');
+
+        String name = (dotIdx < 0 ? fullName : fullName.substring(dotIdx + 1));
+        String prefix = (dotIdx > 0 ? fullName.substring(0, dotIdx) : "");
+        String dir = dumpGeneratedClassesDir + File.separator + prefix.replaceAll("\\.", File.separator);
+        if (!ensureDumpDirectory(dir)) {
+            System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : Cannot dump transformed bytes to directory " + dir + File.separator + prefix);
+            return;
+        }
+        name = dir + File.separator + name + ".class";
+        System.out.println("org.jboss.jbossts.orchestration.agent.Transformer : Saving transformed bytes to " + name);
+        try {
+            FileOutputStream fio = new FileOutputStream(name);
+            fio.write(bytes);
+            fio.close();
+        } catch (IOException ioe) {
+            System.out.println("Error saving transformed bytes to" + name);
+            ioe.printStackTrace(System.out);
+        }
+    }
+
+    private boolean ensureDumpDirectory(String fileName)
+    {
+        File file = new File(fileName);
+        if (file.exists()) {
+            return (file.isDirectory() && file.canWrite());
+        } else {
+            return file.mkdirs();
+        }
+    }
 }




More information about the jboss-svn-commits mailing list