You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2021/09/22 09:39:37 UTC

[tomee] branch master updated (22c5fc8 -> e9c1775)

This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git.


    from 22c5fc8  Update Johnzon to latest snapshot
     new 96ddbee  TOMEE-3795 use MethodHandles.lookup as opposed to Unsafe where available
     new 539cf6b  WIP
     new e9c1775  Merge branch 'j17'

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 container/openejb-core/pom.xml                     |  19 +++
 .../org/apache/openejb/dyni/DynamicSubclass.java   |   3 +-
 .../apache/openejb/util/proxy/ClassDefiner.java    | 160 +++++++++++++++++++++
 .../openejb/util/proxy/LocalBeanProxyFactory.java  |   4 +-
 tomee/tomee-bootstrap/pom.xml                      |   2 +-
 5 files changed, 184 insertions(+), 4 deletions(-)
 create mode 100644 container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java

[tomee] 03/03: Merge branch 'j17'

Posted by jg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit e9c17758ba48fffa38a8b98a5fa9c9083d9a3a5e
Merge: 22c5fc8 539cf6b
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Wed Sep 22 10:17:07 2021 +0100

    Merge branch 'j17'

 container/openejb-core/pom.xml                     |  19 +++
 .../org/apache/openejb/dyni/DynamicSubclass.java   |   3 +-
 .../apache/openejb/util/proxy/ClassDefiner.java    | 160 +++++++++++++++++++++
 .../openejb/util/proxy/LocalBeanProxyFactory.java  |   4 +-
 tomee/tomee-bootstrap/pom.xml                      |   2 +-
 5 files changed, 184 insertions(+), 4 deletions(-)

[tomee] 01/03: TOMEE-3795 use MethodHandles.lookup as opposed to Unsafe where available

Posted by jg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 96ddbeef57d7e90902a9f3396a77ac1765692c09
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Tue Sep 21 14:11:12 2021 +0100

    TOMEE-3795 use MethodHandles.lookup as opposed to Unsafe where available
---
 .../org/apache/openejb/dyni/DynamicSubclass.java   |   3 +-
 .../apache/openejb/util/proxy/ClassDefiner.java    | 160 +++++++++++++++++++++
 .../openejb/util/proxy/LocalBeanProxyFactory.java  |   4 +-
 3 files changed, 164 insertions(+), 3 deletions(-)

diff --git a/container/openejb-core/src/main/java/org/apache/openejb/dyni/DynamicSubclass.java b/container/openejb-core/src/main/java/org/apache/openejb/dyni/DynamicSubclass.java
index 219793d..25193aa 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/dyni/DynamicSubclass.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/dyni/DynamicSubclass.java
@@ -19,6 +19,7 @@ package org.apache.openejb.dyni;
 
 import org.apache.openejb.loader.IO;
 import org.apache.openejb.util.Debug;
+import org.apache.openejb.util.proxy.ClassDefiner;
 import org.apache.openejb.util.proxy.LocalBeanProxyFactory;
 import org.apache.openejb.util.proxy.ProxyGenerationException;
 import org.apache.xbean.asm9.AnnotationVisitor;
@@ -84,7 +85,7 @@ public class DynamicSubclass implements Opcodes {
                 // no-op
             }
 
-            return LocalBeanProxyFactory.Unsafe.defineClass(cl, abstractClass, proxyName, generateBytes(abstractClass, proxyNonAbstractMethods));
+            return ClassDefiner.defineClass(cl, proxyName, generateBytes(abstractClass, proxyNonAbstractMethods), abstractClass, abstractClass.getProtectionDomain());
 
         } catch (final Exception e) {
             throw new InternalError(DynamicSubclass.class.getSimpleName() + ".createSubclass: " + Debug.printStackTrace(e));
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java
new file mode 100644
index 0000000..9f85656
--- /dev/null
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ClassDefiner.java
@@ -0,0 +1,160 @@
+/*
+ * 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.openejb.util.proxy;
+
+import java.lang.invoke.MethodHandles;
+import java.lang.reflect.Method;
+import java.security.ProtectionDomain;
+
+public class ClassDefiner
+{
+    private static final Method CLASS_LOADER_DEFINE_CLASS;
+    private static final Method GET_MODULE;
+    private static final Method CAN_READ;
+    private static final Method ADD_READS;
+    private static final Method PRIVATE_LOOKUP_IN;
+    private static final Method DEFINE_CLASS;
+
+    static
+    {
+        Method classLoaderDefineClass = null;
+        try
+        {
+            java.lang.reflect.Method method = ClassLoader.class.getDeclaredMethod(
+                    "defineClass", String.class, byte[].class, int.class, int.class, ProtectionDomain.class);
+            method.setAccessible(true);
+            classLoaderDefineClass = method;
+        }
+        catch (Exception ex)
+        {
+            // Ignore
+        }
+        CLASS_LOADER_DEFINE_CLASS = classLoaderDefineClass;
+
+        Method getModule = null;
+        Method canRead = null;
+        Method addReads = null;
+        Method privateLookupIn = null;
+        Method defineClass = null;
+        try
+        {
+            getModule = Class.class.getMethod("getModule");
+            Class<?> moduleClass = getModule.getReturnType();
+            canRead = moduleClass.getMethod("canRead", moduleClass);
+            addReads = moduleClass.getMethod("addReads", moduleClass);
+            privateLookupIn = MethodHandles.class.getMethod("privateLookupIn", Class.class, MethodHandles.Lookup.class);
+            defineClass = MethodHandles.Lookup.class.getMethod("defineClass", byte[].class);
+        }
+        catch (Exception ex)
+        {
+            // Ignore
+        }
+        GET_MODULE = getModule;
+        CAN_READ = canRead;
+        ADD_READS = addReads;
+        PRIVATE_LOOKUP_IN = privateLookupIn;
+        DEFINE_CLASS = defineClass;
+    }
+
+    private ClassDefiner()
+    {
+
+    }
+
+    public static Class<?> defineClass(ClassLoader loader, String className, byte[] b,
+                                       Class<?> originalClass, ProtectionDomain protectionDomain)
+    {
+        if (CLASS_LOADER_DEFINE_CLASS == null)
+        {
+            return defineClassMethodHandles(loader, className, b, originalClass, protectionDomain);
+        }
+        else
+        {
+            return defineClassClassLoader(loader, className, b, originalClass, protectionDomain);
+        }
+    }
+    /**
+     * Adapted from http://asm.ow2.org/doc/faq.html#Q5
+     *
+     * @param b
+     *
+     * @return Class<?>
+     */
+    static Class<?> defineClassClassLoader(ClassLoader loader, String className, byte[] b,
+                                           Class<?> originalClass, ProtectionDomain protectionDomain)
+    {
+        try
+        {
+            return (Class<?>) CLASS_LOADER_DEFINE_CLASS.invoke(
+                    loader, className, b, Integer.valueOf(0), Integer.valueOf(b.length), protectionDomain);
+        }
+        catch (Exception e)
+        {
+            throw e instanceof RuntimeException ? ((RuntimeException) e) : new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Implementation based on MethodHandles.Lookup.
+     *
+     * @return Class<?>
+     */
+    static Class<?> defineClassMethodHandles(ClassLoader loader, String className, byte[] b,
+                                             Class<?> originalClass, ProtectionDomain protectionDomain)
+    {
+        try
+        {
+            Object thisModule = GET_MODULE.invoke(LocalBeanProxyFactory.class);
+            Object lookupClassModule = GET_MODULE.invoke(originalClass);
+            if (!(boolean) CAN_READ.invoke(thisModule, lookupClassModule))
+            {
+                // we need to read the other module in order to have privateLookup access
+                // see javadoc for MethodHandles.privateLookupIn()
+                ADD_READS.invoke(thisModule, lookupClassModule);
+            }
+            Object lookup = PRIVATE_LOOKUP_IN.invoke(null, originalClass, MethodHandles.lookup());
+            return (Class<?>) DEFINE_CLASS.invoke(lookup, b);
+        }
+        catch (Exception e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+//    static Class<?> defineClassMethodHandles(ClassLoader loader, String className, byte[] b,
+//                                Class<?> originalClass, ProtectionDomain protectionDomain)
+//    {
+//        Module thisModule = AsmDeltaSpikeProxyClassGenerator.class.getModule();
+//        try
+//        {
+//            Module lookupClassModule = originalClass.getModule();
+//            if (!thisModule.canRead(lookupClassModule))
+//            {
+//                // we need to read the other module in order to have privateLookup access
+//                // see javadoc for MethodHandles.privateLookupIn()
+//                thisModule.addReads(lookupClassModule);
+//            }
+//            MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(originalClass, MethodHandles.lookup());
+//            return lookup.defineClass(b);
+//        }
+//        catch (IllegalAccessException e)
+//        {
+//            throw new RuntimeException(e);
+//        }
+//    }
+}
\ No newline at end of file
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java
index 99f498c..5f46465 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java
@@ -139,8 +139,8 @@ public class LocalBeanProxyFactory implements Opcodes {
             }
 
             final byte[] proxyBytes = generateProxy(classToProxy, classFileName, interfaces);
-            return Unsafe.defineClass(cl, classToProxy, proxyName, proxyBytes);
-
+            return ClassDefiner.defineClass(cl, proxyName, proxyBytes, classToProxy, classToProxy.getProtectionDomain());
+            // return Unsafe.defineClass(cl, classToProxy, proxyName, proxyBytes);
         } catch (final Exception e) {
             throw new InternalError("LocalBeanProxyFactory.createProxy: " + Debug.printStackTrace(e));
         } finally {

[tomee] 02/03: WIP

Posted by jg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit 539cf6bc097cf92c6a75a2cf74c97e76964fb94f
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Wed Sep 22 09:33:41 2021 +0100

    WIP
---
 container/openejb-core/pom.xml | 19 +++++++++++++++++++
 tomee/tomee-bootstrap/pom.xml  |  2 +-
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/container/openejb-core/pom.xml b/container/openejb-core/pom.xml
index f83924e..e2303c1 100644
--- a/container/openejb-core/pom.xml
+++ b/container/openejb-core/pom.xml
@@ -752,6 +752,25 @@
         </plugins>
       </build>
     </profile>
+    <profile>
+      <id>java11plus</id>
+      <activation>
+        <jdk>[11,)</jdk>
+      </activation>
+      <dependencies>
+        <dependency>
+          <groupId>org.apache.yoko</groupId>
+          <artifactId>yoko-spec-corba</artifactId>
+          <version>1.4</version>
+          <exclusions>
+            <exclusion>
+              <artifactId>*</artifactId>
+              <groupId>*</groupId>
+            </exclusion>
+          </exclusions>
+        </dependency>
+      </dependencies>
+    </profile>
   </profiles>
 </project>
 
diff --git a/tomee/tomee-bootstrap/pom.xml b/tomee/tomee-bootstrap/pom.xml
index db1dcff..825bde3 100644
--- a/tomee/tomee-bootstrap/pom.xml
+++ b/tomee/tomee-bootstrap/pom.xml
@@ -96,7 +96,7 @@
     <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
-      <version>1.18.8</version>
+      <version>1.18.20</version>
       <scope>test</scope>
     </dependency>
   </dependencies>