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/19 23:02:36 UTC

svn commit: r965633 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java openjpa-project/src/doc/manual/ref_guide_pc.xml

Author: curtisr7
Date: Mon Jul 19 21:02:36 2010
New Revision: 965633

URL: http://svn.apache.org/viewvc?rev=965633&view=rev
Log:
OPENJPA-1734: Add support for the DynamicEnhancer on IBM jvm.

Added:
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java   (with props)
Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_pc.xml

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=965633&r1=965632&r2=965633&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 Mon Jul 19 21:02:36 2010
@@ -38,6 +38,8 @@ import java.util.zip.ZipEntry;
 import java.util.zip.ZipOutputStream;
 
 import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.lib.util.J2DoPrivHelper;
+import org.apache.openjpa.lib.util.JavaVendors;
 import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
 
@@ -54,7 +56,9 @@ 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
      * getInstrumentation() that method will cause agentmain(..) to be called.
@@ -80,7 +84,11 @@ public class InstrumentationFactory {
      * Exceptions are encountered.
      */
     public static synchronized Instrumentation getInstrumentation(final Log log) {
-        if (_inst != null || !_dynamicallyInstall)
+        if (log.isTraceEnabled() == true) {
+            log.trace(InstrumentationFactory.class.getName() + "getInstrumentation() _disabled:" + " _inst:" + _inst
+                + "_dynamicallyInstall:" + _dynamicallyInstall);
+        }
+        if ( _inst != null || !_dynamicallyInstall)
             return _inst;
 
         // dynamic loading of the agent is only available in JDK 1.6+
@@ -100,14 +108,19 @@ public class InstrumentationFactory {
                 } catch (Throwable t) {
                     return null;
                 }
-                
-                // If we can't find the tools.jar, we can't load the agent.
-                File toolsJar = findToolsJar(log);
-                if (toolsJar == null) {
-                    return null;
+                boolean ibm = JavaVendors.isIBM();
+                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 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) {
+                        return null;
+                    }
                 }
 
-                Class<?> vmClass = loadVMClass(toolsJar, log);
+                Class<?> vmClass = loadVMClass(toolsJar, log, ibm);
                 if (vmClass == null) {
                     return null;
                 }
@@ -305,19 +318,25 @@ public class InstrumentationFactory {
     }
 
     /**
-     * This private method will create a new classloader and attempt to load the
-     * com.sun.tools.attach.VirtualMachine class from the provided toolsJar
-     * file.
+     * If <b>ibm</b> is false, this private method will create a new URLClassLoader and attempt to load the
+     * com.sun.tools.attach.VirtualMachine class from the provided toolsJar file. 
+     * 
+     * <p>
+     * If <b>ibm</b> is true, this private method will ignore the toolsJar parameter and load the 
+     * com.ibm.tools.attach.VirtualMachine class. 
      * 
-     * @return com.sun.tools.attach.VirtualMachine class <br>
+     * 
+     * @return The AttachAPI VirtualMachine class <br>
      *         or null if something unexpected happened.
      */
-    private static Class<?> loadVMClass(File toolsJar, Log log) {
+    private static Class<?> loadVMClass(File toolsJar, Log log, boolean ibm) {
         try {
-            URLClassLoader loader =
-                new URLClassLoader(new URL[] { toolsJar.toURI().toURL() },
-                    Thread.currentThread().getContextClassLoader());
-            return loader.loadClass("com.sun.tools.attach.VirtualMachine");
+            ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            String cls = (ibm == true) ? IBM_VM_CLASS : SUN_VM_CLASS;
+            if (ibm == false) {
+                loader = new URLClassLoader(new URL[] { toolsJar.toURI().toURL() }, loader);
+            }
+            return loader.loadClass(cls);
         } catch (Exception e) {
             if (log.isTraceEnabled()) {
                 log.trace(_name

Added: 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=965633&view=auto
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java (added)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java Mon Jul 19 21:02:36 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.lib.util;
+
+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;
+
+    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;
+        } else {
+            VENDOR = OTHER;
+        }
+    }
+
+    /**
+     * This static worker method returns <b>true</b> if the current implementation is IBM.
+     */
+    public static 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;
+    }
+}

Propchange: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/JavaVendors.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_pc.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_pc.xml?rev=965633&r1=965632&r2=965633&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_pc.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_pc.xml Mon Jul 19 21:02:36 2010
@@ -386,7 +386,7 @@ java -javaagent:/home/dev/openjpa/lib/op
 	</title>          
 	<para>
 		If a javaagent is not provided via the command line and 
-		OpenJPA is running on the Sun 1.6 SDK (not the JRE), OpenJPA
+		OpenJPA is running on the Sun 1.6 SDK or IBM 1.6 JDK (SR8+), OpenJPA
 		will attempt to dynamically load the Enhancer that was 
 		mentioned in the previous section. This support is 
 		provided as an ease of use feature and it is not recommended
@@ -396,12 +396,6 @@ java -javaagent:/home/dev/openjpa/lib/op
 	<itemizedlist>
 		<listitem>
 			<para>
-				As stated previously, this is only supported on
-				the Sun 1.6 SDK.			
-			</para>
-		</listitem>
-		<listitem>
-			<para>
 				The dynamic runtime enhancer is plugged into 
 				the JVM during creation of the 
 				EntityManagerFactory. Any Entity classes that