You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2010/07/20 16:40:23 UTC

svn commit: r965864 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ openjpa-lib/src/main/java/org/apache/openjpa/lib/util/

Author: curtisr7
Date: Tue Jul 20 14:40:23 2010
New Revision: 965864

URL: http://svn.apache.org/viewvc?rev=965864&view=rev
Log:
OPENJPA-1734: Refactor JavaVendors into an Enum and fix exception reported in JIRA.

Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java?rev=965864&r1=965863&r2=965864&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java Tue Jul 20 14:40:23 2010
@@ -56,8 +56,6 @@ public class InstrumentationFactory {
     private static final String _name = InstrumentationFactory.class.getName();
     private static final Localizer _loc = Localizer.forPackage(
         InstrumentationFactory.class);
-    private static final String IBM_VM_CLASS = "com.ibm.tools.attach.VirtualMachine";
-    private static final String SUN_VM_CLASS = "com.sun.tools.attach.VirtualMachine";
 
     /**
      * This method is not synchronized because when the agent is loaded from
@@ -108,11 +106,11 @@ public class InstrumentationFactory {
                 } catch (Throwable t) {
                     return null;
                 }
-                boolean ibm = JavaVendors.isIBM();
+                JavaVendors vendor = JavaVendors.getCurrentVendor();                
                 File toolsJar = null;
                 // When running on IBM, the attach api classes are packaged in vm.jar which is a part
                 // of the default vm classpath.
-                if (ibm == false) {
+                if (vendor.isIBM() == false) {
                     // If we can't find the tools.jar and we're not on IBM we can't load the agent. 
                     toolsJar = findToolsJar(log);
                     if (toolsJar == null) {
@@ -120,7 +118,7 @@ public class InstrumentationFactory {
                     }
                 }
 
-                Class<?> vmClass = loadVMClass(toolsJar, log, ibm);
+                Class<?> vmClass = loadVMClass(toolsJar, log, vendor);
                 if (vmClass == null) {
                     return null;
                 }
@@ -165,7 +163,8 @@ public class InstrumentationFactory {
         writer
             .println("Agent-Class: " + InstrumentationFactory.class.getName());
         writer.println("Can-Redefine-Classes: true");
-        writer.println("Can-Retransform-Classes: true");
+        // IBM doesn't support retransform
+        writer.println("Can-Retransform-Classes: " + Boolean.toString(JavaVendors.getCurrentVendor().isIBM() == false));
 
         writer.close();
 
@@ -230,7 +229,7 @@ public class InstrumentationFactory {
         // jar *should* be the same location as our agent.
         CodeSource cs =
             InstrumentationFactory.class.getProtectionDomain().getCodeSource();
-        if (cs != null) {
+        if (cs != null) {   
             URL loc = cs.getLocation();
             if(loc!=null){
                 agentJarFile = new File(loc.getFile());
@@ -298,8 +297,7 @@ public class InstrumentationFactory {
             // ### this feature, but in an implementation-dependent way
             Object vm =
                 vmClass.getMethod("attach", new Class<?>[] { String.class })
-                    .invoke(null, new String[] { pid });
-
+                    .invoke(null, new Object[] { pid });
             // now deploy the actual agent, which will wind up calling
             // agentmain()
             vmClass.getMethod("loadAgent", new Class[] { String.class })
@@ -329,11 +327,11 @@ public class InstrumentationFactory {
      * @return The AttachAPI VirtualMachine class <br>
      *         or null if something unexpected happened.
      */
-    private static Class<?> loadVMClass(File toolsJar, Log log, boolean ibm) {
+    private static Class<?> loadVMClass(File toolsJar, Log log, JavaVendors vendor) {
         try {
             ClassLoader loader = Thread.currentThread().getContextClassLoader();
-            String cls = (ibm == true) ? IBM_VM_CLASS : SUN_VM_CLASS;
-            if (ibm == false) {
+            String cls = vendor.getVirtualMachineClassName();
+            if (vendor.isIBM() == false) {
                 loader = new URLClassLoader(new URL[] { toolsJar.toURI().toURL() }, loader);
             }
             return loader.loadClass(cls);

Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java?rev=965864&r1=965863&r2=965864&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java Tue Jul 20 14:40:23 2010
@@ -833,6 +833,23 @@ public abstract class J2DoPrivHelper {
             }
         };
     }
+    
+    /**
+     * Return a PrivilegeAction object for System.getProperty().
+     * 
+     * Requires security policy:
+     *   'permission java.util.PropertyPermission "read";'
+     *   
+     * @return String
+     */
+    public static final PrivilegedAction<String> getPropertyAction(
+        final String name, final String def) {
+        return new PrivilegedAction<String>() {
+            public String run() {
+                return System.getProperty(name, def);
+            }
+        };
+    }
 
     /**
      * Return a PrivilegeAction object for Thread.currentThread

Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java?rev=965864&r1=965863&r2=965864&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java (original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java Tue Jul 20 14:40:23 2010
@@ -22,39 +22,53 @@ import java.security.AccessController;
 
 /**
  * Utilities for dealing with different Java vendors.
- * 
  */
-public class JavaVendors {
-
-    static public final int VENDOR;
-
-    static public final int OTHER = 0;
-    static public final int SUN = 1;
-    static public final int IBM = 2;
+public enum JavaVendors {
+    IBM("com.ibm.tools.attach.VirtualMachine"), SUN("com.sun.tools.attach.VirtualMachine"),
+    // When in doubt, try the Sun implementation.
+    OTHER("com.sun.tools.attach.VirtualMachine");
 
     static {
-        String vendor = AccessController.doPrivileged(J2DoPrivHelper.getPropertyAction("java.vendor"));
-
-        if (vendor.toUpperCase().contains("SUN MICROSYSTEMS")) {
-            VENDOR = SUN;
-        } else if (vendor.toUpperCase().contains("IBM")) {
-            VENDOR = IBM;
+        String vendor =
+            AccessController.doPrivileged(J2DoPrivHelper.getPropertyAction("java.vendor", "")).toUpperCase();
+        if (vendor.contains("SUN MICROSYSTEMS")) {
+            _vendor = SUN;
+        } else if (vendor.contains("IBM")) {
+            _vendor = IBM;
         } else {
-            VENDOR = OTHER;
+            _vendor = OTHER;
         }
     }
+    
+    private static final JavaVendors _vendor;
+    private String _virtualMachineClass = null;
+    
+    private JavaVendors(String vmClass) {
+        _virtualMachineClass = vmClass;
+    }
 
     /**
+     * This static worker method returns the current Vendor.
+     */
+    public static JavaVendors getCurrentVendor() {
+        return _vendor;
+    }
+    
+    /**
      * This static worker method returns <b>true</b> if the current implementation is IBM.
      */
-    public static boolean isIBM() {
-        return VENDOR == IBM;
+    public boolean isIBM() {
+        return _vendor == IBM;
     }
 
     /**
      * This static worker method returns <b>true</b> if the current implementation is Sun.
      */
-    public static boolean isSun() {
-        return VENDOR == SUN;
+    public boolean isSun() {
+        return _vendor == SUN;
+    }
+    
+    public String getVirtualMachineClassName() {
+        return _virtualMachineClass;
     }
 }