You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2019/07/10 08:51:45 UTC

[tomcat] branch master updated: NamingManager.getObjectInstance does not work with Graal

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 984ff60  NamingManager.getObjectInstance does not work with Graal
984ff60 is described below

commit 984ff60dc0bfae9b3890cdf4a352706e797317c5
Author: remm <re...@apache.org>
AuthorDate: Wed Jul 10 10:51:37 2019 +0200

    NamingManager.getObjectInstance does not work with Graal
    
    For me it returns the reference in the native image, so obviously this
    is not good. Resolve the reference using simple less flexible code. The
    intermediate factory (that would use FactoryBase.getObjectInstance) must
    be defined in the configuration manually because Graal doesn't trace it.
---
 java/org/apache/naming/NamingContext.java | 32 ++++++++++++++++++++++++++++---
 res/tomcat-maven/tomcat-reflection.json   |  6 +++++-
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/naming/NamingContext.java b/java/org/apache/naming/NamingContext.java
index b6a97bb..0471279 100644
--- a/java/org/apache/naming/NamingContext.java
+++ b/java/org/apache/naming/NamingContext.java
@@ -39,6 +39,7 @@ import javax.naming.OperationNotSupportedException;
 import javax.naming.Reference;
 import javax.naming.Referenceable;
 import javax.naming.spi.NamingManager;
+import javax.naming.spi.ObjectFactory;
 
 import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
@@ -791,6 +792,21 @@ public class NamingContext implements Context {
     // ------------------------------------------------------ Protected Methods
 
 
+    private static final boolean GRAAL;
+
+    static {
+        boolean result = false;
+        try {
+            Class<?> nativeImageClazz = Class.forName("org.graalvm.nativeimage.ImageInfo");
+            result = Boolean.TRUE.equals(nativeImageClazz.getMethod("inImageCode").invoke(null));
+        } catch (ClassNotFoundException e) {
+            // Must be Graal
+        } catch (ReflectiveOperationException | IllegalArgumentException e) {
+            // Should never happen
+        }
+        GRAAL = result;
+    }
+
     /**
      * Retrieves the named object.
      *
@@ -836,9 +852,19 @@ public class NamingContext implements Context {
                 }
             } else if (entry.type == NamingEntry.REFERENCE) {
                 try {
-                    Object obj = NamingManager.getObjectInstance
-                        (entry.value, name, this, env);
-                    if(entry.value instanceof ResourceRef) {
+                    Object obj = null;
+                    if (!GRAAL) {
+                        obj = NamingManager.getObjectInstance(entry.value, name, this, env);
+                    } else {
+                        // NamingManager.getObjectInstance would simply return the reference here
+                        // Use the configured object factory to resolve it directly if possible
+                        // Note: This may need manual constructor reflection configuration
+                        Reference reference = (Reference) entry.value;
+                        Class<?> factoryClass = getClass().getClassLoader().loadClass(reference.getFactoryClassName());
+                        ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
+                        obj = factory.getObjectInstance(entry.value, name, this, env);
+                    }
+                    if (entry.value instanceof ResourceRef) {
                         boolean singleton = Boolean.parseBoolean(
                                     (String) ((ResourceRef) entry.value).get(
                                         "singleton").getContent());
diff --git a/res/tomcat-maven/tomcat-reflection.json b/res/tomcat-maven/tomcat-reflection.json
index 8885dac..ab1e477 100644
--- a/res/tomcat-maven/tomcat-reflection.json
+++ b/res/tomcat-maven/tomcat-reflection.json
@@ -40,5 +40,9 @@
 { "name":"com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", "methods" : [{"name": "<init>","parameterTypes":[]}] },
 { "name":"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", "methods" : [{"name": "<init>","parameterTypes":[]}] },
 { "name":"org.apache.catalina.authenticator.jaspic.AuthConfigFactoryImpl", "methods" : [{"name": "<init>","parameterTypes":[]}] },
-{ "name":"org.apache.el.ExpressionFactoryImpl", "methods" : [{"name":"<init>","parameterTypes":[]}] }
+{ "name":"org.apache.el.ExpressionFactoryImpl", "methods" : [{"name":"<init>","parameterTypes":[]}] },
+{ "name":"org.apache.naming.factory.EjbFactory", "methods" : [{"name": "<init>","parameterTypes":[]}] },
+{ "name":"org.apache.naming.factory.ResourceEnvFactory", "methods" : [{"name": "<init>","parameterTypes":[]}] },
+{ "name":"org.apache.naming.factory.ResourceFactory", "methods" : [{"name": "<init>","parameterTypes":[]}] },
+{ "name":"org.apache.naming.factory.TransactionFactory", "methods" : [{"name": "<init>","parameterTypes":[]}] }
 ]


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org