[jboss-cvs] JBossAS SVN: r86705 - projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 2 17:56:09 EDT 2009


Author: alesj
Date: 2009-04-02 17:56:09 -0400 (Thu, 02 Apr 2009)
New Revision: 86705

Added:
   projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss50ClassLoader.java
   projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss51ClassLoader.java
   projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/ReflectionHelper.java
Modified:
   projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java
   projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5LoadTimeWeaver.java
Log:
Use more optimized version in JBossAS5.1.

Copied: projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss50ClassLoader.java (from rev 86583, projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java)
===================================================================
--- projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss50ClassLoader.java	                        (rev 0)
+++ projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss50ClassLoader.java	2009-04-02 21:56:09 UTC (rev 86705)
@@ -0,0 +1,94 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.instrument.classloading;
+
+import java.lang.reflect.Method;
+
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.base.BaseClassLoader;
+import org.jboss.classloader.spi.base.BaseClassLoaderDomain;
+import org.jboss.classloader.spi.base.BaseClassLoaderPolicy;
+import org.jboss.classloader.spi.base.BaseClassLoaderSystem;
+import org.jboss.util.loading.Translator;
+
+/**
+ * Reflective wrapper around a JBoss_5.0.x class loader. Used to
+ * encapsulate the classloader-specific methods (discovered and
+ * called through reflection) from the load-time weaver.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class JBoss50ClassLoader extends JBoss5ClassLoader
+{
+   private Method addTranslator;
+   private ClassLoaderSystem system;
+
+   public JBoss50ClassLoader(BaseClassLoader classLoader)
+   {
+      super(classLoader);
+   }
+
+   protected void fallbackStrategy() throws Exception
+   {
+      try
+      {
+         // let's check if we have a patched policy, with translator per policy
+         addTranslator = getMethod(BaseClassLoaderPolicy.class, "addTranslator");
+      }
+      catch (Exception ignored)
+      {
+         log.info("Policy doesn't have setTranslator, falling back to ClassLoaderSystem.");
+
+         Method getClassLoaderDomain = getMethod(BaseClassLoaderPolicy.class, "getClassLoaderDomain");
+         BaseClassLoaderDomain domain = invokeMethod(getClassLoaderDomain, getPolicy(), BaseClassLoaderDomain.class);
+         Method getClassLoaderSystem = getMethod(BaseClassLoaderDomain.class, "getClassLoaderSystem");
+         BaseClassLoaderSystem system = invokeMethod(getClassLoaderSystem, domain, BaseClassLoaderSystem.class);
+         if (system instanceof ClassLoaderSystem)
+         {
+            this.system = ClassLoaderSystem.class.cast(system);
+         }
+         else
+         {
+            throw new IllegalArgumentException("ClassLoaderSyatem must be instance of [" + ClassLoaderSystem.class.getName() + "]");
+         }
+      }
+   }
+
+   protected void addTranslator(Translator translator)
+   {
+      if (addTranslator != null)
+      {
+         try
+         {
+            addTranslator.invoke(translator);
+         }
+         catch (Exception e)
+         {
+            throw new IllegalArgumentException(e);
+         }
+      }
+      else
+      {
+         system.setTranslator(translator);
+      }
+   }
+}
\ No newline at end of file

Added: projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss51ClassLoader.java
===================================================================
--- projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss51ClassLoader.java	                        (rev 0)
+++ projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss51ClassLoader.java	2009-04-02 21:56:09 UTC (rev 86705)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.instrument.classloading;
+
+import org.jboss.classloader.spi.base.BaseClassLoader;
+import org.jboss.util.loading.Translator;
+
+/**
+ * Reflective wrapper around a JBoss_5.1.x class loader. Used to
+ * encapsulate the classloader-specific methods (discovered and
+ * called through reflection) from the load-time weaver.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class JBoss51ClassLoader extends JBoss5ClassLoader
+{
+   public JBoss51ClassLoader(BaseClassLoader classLoader)
+   {
+      super(classLoader);
+   }
+
+   protected void addTranslator(Translator translator)
+   {
+      // TODO - once we update JBCL to 2.0.5.GA 
+      // getPolicy().addTranslator(translator);
+   }
+}
\ No newline at end of file

Modified: projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java
===================================================================
--- projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java	2009-04-02 21:53:37 UTC (rev 86704)
+++ projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java	2009-04-02 21:56:09 UTC (rev 86705)
@@ -27,12 +27,7 @@
 import java.security.PrivilegedExceptionAction;
 
 import org.jboss.classloader.spi.ClassLoaderPolicy;
-import org.jboss.classloader.spi.ClassLoaderSystem;
 import org.jboss.classloader.spi.base.BaseClassLoader;
-import org.jboss.classloader.spi.base.BaseClassLoaderDomain;
-import org.jboss.classloader.spi.base.BaseClassLoaderPolicy;
-import org.jboss.classloader.spi.base.BaseClassLoaderSystem;
-import org.jboss.logging.Logger;
 import org.jboss.util.loading.Translator;
 import org.springframework.util.Assert;
 
@@ -43,18 +38,13 @@
  *
  * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
-public class JBoss5ClassLoader
+public abstract class JBoss5ClassLoader extends ReflectionHelper
 {
-   private static Logger log = Logger.getLogger(JBoss5ClassLoader.class);
-
    private final BaseClassLoader classLoader;
-
    private ClassLoaderPolicy policy;
-   private Method addTranslator;
-   private ClassLoaderSystem system;
 
    @SuppressWarnings("unchecked")
-   public JBoss5ClassLoader(BaseClassLoader classLoader)
+   protected JBoss5ClassLoader(BaseClassLoader classLoader)
    {
       Assert.notNull(classLoader, "ClassLoader must not be null");
       this.classLoader = classLoader;
@@ -74,93 +64,50 @@
    }
 
    /**
-    * Do instantiate method, variables.
+    * Get the policy.
     *
-    * @throws Exception for any error
+    * @return the policy
     */
-   private void doInstantiate() throws Exception
+   protected ClassLoaderPolicy getPolicy()
    {
-      Method getPolicy = getMethod(BaseClassLoader.class, "getPolicy");
-      policy = invokeMethod(getPolicy, classLoader, ClassLoaderPolicy.class);
-      try
-      {
-         // let's check if we have a patched policy, with translator per policy
-         addTranslator = getMethod(BaseClassLoaderPolicy.class, "addTranslator");
-      }
-      catch (Exception ignored)
-      {
-         log.info("Policy doesn't have setTranslator, falling back to ClassLoaderSystem.");
-
-         Method getClassLoaderDomain = getMethod(BaseClassLoaderPolicy.class, "getClassLoaderDomain");
-         BaseClassLoaderDomain domain = invokeMethod(getClassLoaderDomain, policy, BaseClassLoaderDomain.class);
-         Method getClassLoaderSystem = getMethod(BaseClassLoaderDomain.class, "getClassLoaderSystem");
-         BaseClassLoaderSystem system = invokeMethod(getClassLoaderSystem, domain, BaseClassLoaderSystem.class);
-         if (system instanceof ClassLoaderSystem)
-         {
-            this.system = ClassLoaderSystem.class.cast(system);
-         }
-         else
-         {
-            throw new IllegalArgumentException("ClassLoaderSyatem must be instance of [" + ClassLoaderSystem.class.getName() + "]");
-         }
-      }
+      return policy;
    }
 
    /**
-    * Get method from class.
+    * Do instantiate method, variables.
     *
-    * @param clazz the owner class
-    * @param name the method name
-    * @return declared method
     * @throws Exception for any error
     */
-   private static Method getMethod(Class<?> clazz, String name) throws Exception
+   private void doInstantiate() throws Exception
    {
-      Method method = clazz.getDeclaredMethod(name);
-      method.setAccessible(true);
-      return method;
+      Method getPolicy = getMethod(BaseClassLoader.class, "getPolicy");
+      policy = invokeMethod(getPolicy, classLoader, ClassLoaderPolicy.class);
+      fallbackStrategy();
    }
 
    /**
-    * Invoke method and check the result.
+    * The fallback strategy.
     *
-    * @param method the method
-    * @param target the target
-    * @param expectedType the expected type
-    * @param <T> the exact type
-    * @return invocation's result
     * @throws Exception for any error
     */
-   private static <T> T invokeMethod(Method method, Object target, Class<T> expectedType) throws Exception
+   protected void fallbackStrategy() throws Exception
    {
-      Object result = method.invoke(target);
-      if (expectedType.isInstance(result) == false)
-         throw new IllegalArgumentException("Returned result must be instance of [" + expectedType.getName() + "]");
-
-      return expectedType.cast(result);
    }
 
    public void addTransformer(ClassFileTransformer transformer)
    {
       Assert.notNull(transformer, "ClassFileTransformer must not be null");
       Translator translator = new ClassFileTransformer2Translator(transformer);
-      if (addTranslator != null)
-      {
-         try
-         {
-            addTranslator.invoke(translator);
-         }
-         catch (Exception e)
-         {
-            throw new IllegalArgumentException(e);
-         }
-      }
-      else
-      {
-         system.setTranslator(translator);   
-      }
+      addTranslator(translator);
    }
 
+   /**
+    * Add the translator.
+    *
+    * @param translator the translator
+    */
+   protected abstract void addTranslator(Translator translator);
+
    public ClassLoader getInternalClassLoader()
    {
       return classLoader;

Modified: projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5LoadTimeWeaver.java
===================================================================
--- projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5LoadTimeWeaver.java	2009-04-02 21:53:37 UTC (rev 86704)
+++ projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5LoadTimeWeaver.java	2009-04-02 21:56:09 UTC (rev 86705)
@@ -22,6 +22,7 @@
 package org.jboss.instrument.classloading;
 
 import java.lang.instrument.ClassFileTransformer;
+import java.lang.reflect.Method;
 
 import org.jboss.classloader.spi.base.BaseClassLoader;
 import org.springframework.instrument.classloading.LoadTimeWeaver;
@@ -33,7 +34,7 @@
  *
  * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  */
-public class JBoss5LoadTimeWeaver implements LoadTimeWeaver
+public class JBoss5LoadTimeWeaver extends ReflectionHelper implements LoadTimeWeaver
 {
    private JBoss5ClassLoader classLoader;
 
@@ -51,10 +52,52 @@
          throw new IllegalArgumentException(classLoader + " and its parents are not suitable ClassLoaders: " +
                "An [" + BaseClassLoader.class.getName() + "] implementation is required.");
       }
-      this.classLoader = new JBoss5ClassLoader(bcl);
+      this.classLoader = createJBoss5ClassLoader(bcl);
    }
 
    /**
+    * Create a JBoss5 classloader wrapper
+    * based on the underlying JBossAS version.
+    *
+    * @param bcl the base classloader
+    * @return new JBoss5 classloader wrapper
+    */
+   protected JBoss5ClassLoader createJBoss5ClassLoader(BaseClassLoader bcl)
+   {
+      int versionNumber = 0;
+      String tag;
+      
+      try
+      {
+         // BCL should see Version class
+         Class<?> versionClass = bcl.loadClass("org.jboss.Version");
+         Method getInstance = getMethod(versionClass, "getInstance");
+         Object version = getInstance.invoke(null); // static method
+
+         Method getMajor = getMethod(versionClass, "getMajor");
+         versionNumber += 100 * invokeMethod(getMajor, version, Integer.class);
+         Method getMinor = getMethod(versionClass, "getMinor");
+         versionNumber += 10 * invokeMethod(getMinor, version, Integer.class);
+         Method getRevision = getMethod(versionClass, "getRevision");
+         versionNumber += invokeMethod(getRevision, version, Integer.class);
+         Method getTag = getMethod(versionClass, "getTag");
+         tag = invokeMethod(getTag, version, String.class);
+      }
+      catch (Exception e)
+      {
+         log.warn("Exception creating JBoss5 CL wrapper: " + e + ", falling back to JBoss50ClassLoader wrapper.");
+         return new JBoss50ClassLoader(bcl);
+      }
+
+      if (versionNumber < 500) // this only works on new MC code
+         throw new IllegalArgumentException("JBoss5LoadTimeWeaver can only be used on new JBoss Microcontainer ClassLoader.");
+      else if (versionNumber <= 501 || (versionNumber == 510 && "Beta1".equals(tag)))
+         return new JBoss50ClassLoader(bcl);
+      else
+         return new JBoss51ClassLoader(bcl);
+   }
+
+   /**
     * Find first BaseClassLoader implementation.
     *
     * @param classLoader the classloader

Copied: projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/ReflectionHelper.java (from rev 86583, projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/JBoss5ClassLoader.java)
===================================================================
--- projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/ReflectionHelper.java	                        (rev 0)
+++ projects/spring-int/trunk/weaving/src/main/java/org/jboss/instrument/classloading/ReflectionHelper.java	2009-04-02 21:56:09 UTC (rev 86705)
@@ -0,0 +1,70 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.instrument.classloading;
+
+import java.lang.reflect.Method;
+
+import org.jboss.logging.Logger;
+
+/**
+ * Reflection helper.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class ReflectionHelper
+{
+   protected Logger log = Logger.getLogger(getClass());
+
+   /**
+    * Get method from class.
+    *
+    * @param clazz the owner class
+    * @param name the method name
+    * @return declared method
+    * @throws Exception for any error
+    */
+   protected static Method getMethod(Class<?> clazz, String name) throws Exception
+   {
+      Method method = clazz.getDeclaredMethod(name);
+      method.setAccessible(true);
+      return method;
+   }
+
+   /**
+    * Invoke method and check the result.
+    *
+    * @param method the method
+    * @param target the target
+    * @param expectedType the expected type
+    * @param <T> the exact type
+    * @return invocation's result
+    * @throws Exception for any error
+    */
+   protected static <T> T invokeMethod(Method method, Object target, Class<T> expectedType) throws Exception
+   {
+      Object result = method.invoke(target);
+      if (expectedType.isInstance(result) == false)
+         throw new IllegalArgumentException("Returned result must be instance of [" + expectedType.getName() + "]");
+
+      return expectedType.cast(result);
+   }
+}
\ No newline at end of file




More information about the jboss-cvs-commits mailing list