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 2020/12/22 10:41:21 UTC

[tomcat] branch master updated: Add reflection for using Unix domain sockets

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 f331cde  Add reflection for using Unix domain sockets
f331cde is described below

commit f331cdee808a0753044fc4103d869d02ae79ad79
Author: remm <re...@apache.org>
AuthorDate: Tue Dec 22 11:27:35 2020 +0100

    Add reflection for using Unix domain sockets
    
    This requires Java 16.
    Also remove the compat class for Graal, since it's was only used for one
    method. Elsewhere, Graal incompatible code is filtered out using the
    flag, so no need to do something else here (and
    JreMemoryLeakPreventionListener the only caller already does not make
    sense with AOT compilation, so it's not a problem to not even use the
    flag). This will allow Graal to use the compat for the underlying JRE it
    actually uses, like 8 or 11 at the moment.
---
 .../org/apache/tomcat/util/compat/GraalCompat.java | 47 ------------
 .../org/apache/tomcat/util/compat/Jre16Compat.java | 85 ++++++++++++++++++++++
 java/org/apache/tomcat/util/compat/JreCompat.java  | 42 +++++++++--
 .../tomcat/util/compat/LocalStrings.properties     |  4 +
 4 files changed, 125 insertions(+), 53 deletions(-)

diff --git a/java/org/apache/tomcat/util/compat/GraalCompat.java b/java/org/apache/tomcat/util/compat/GraalCompat.java
deleted file mode 100644
index 6187eb6..0000000
--- a/java/org/apache/tomcat/util/compat/GraalCompat.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- *  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.tomcat.util.compat;
-
-import java.io.IOException;
-
-class GraalCompat extends Jre9Compat {
-
-    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 || System.getProperty("org.graalvm.nativeimage.imagecode") != null;
-    }
-
-    static boolean isSupported() {
-        // This property does not exist for a native image
-        return GRAAL;
-    }
-
-    @Override
-    public void disableCachingForJarUrlConnections() throws IOException {
-    }
-
-}
diff --git a/java/org/apache/tomcat/util/compat/Jre16Compat.java b/java/org/apache/tomcat/util/compat/Jre16Compat.java
new file mode 100644
index 0000000..406824f
--- /dev/null
+++ b/java/org/apache/tomcat/util/compat/Jre16Compat.java
@@ -0,0 +1,85 @@
+/*
+ *  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.tomcat.util.compat;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.ProtocolFamily;
+import java.net.SocketAddress;
+import java.net.StandardProtocolFamily;
+import java.nio.channels.ServerSocketChannel;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.res.StringManager;
+
+class Jre16Compat extends Jre9Compat {
+
+    private static final Log log = LogFactory.getLog(Jre16Compat.class);
+    private static final StringManager sm = StringManager.getManager(Jre16Compat.class);
+
+    private static final Class<?> unixDomainSocketAddressClazz;
+    private static final Method openServerSocketChannelFamilyMethod;
+    private static final Method unixDomainSocketAddressOfMethod;
+
+    static {
+        Class<?> c1 = null;
+        Method m1 = null;
+        Method m2 = null;
+        try {
+            c1 = Class.forName("java.net.UnixDomainSocketAddress");
+            m1 = ServerSocketChannel.class.getMethod("open", ProtocolFamily.class);
+            m2 = c1.getMethod("of", String.class);
+        } catch (ClassNotFoundException e) {
+            if (c1 == null) {
+                // Must be pre-Java 16
+                log.debug(sm.getString("jre16Compat.javaPre16"), e);
+            }
+        } catch (ReflectiveOperationException | IllegalArgumentException e) {
+            // Should never happen
+            log.error(sm.getString("jre16Compat.unexpected"), e);
+        }
+        unixDomainSocketAddressClazz = c1;
+        openServerSocketChannelFamilyMethod = m1;
+        unixDomainSocketAddressOfMethod = m2;
+    }
+
+    static boolean isSupported() {
+        return unixDomainSocketAddressClazz != null;
+    }
+
+    @Override
+    public SocketAddress getUnixDomainSocketAddress(String path) {
+        try {
+            return (SocketAddress) unixDomainSocketAddressOfMethod.invoke(null, path);
+        } catch (IllegalAccessException | IllegalArgumentException
+                | InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+    @Override
+    public ServerSocketChannel openUnixDomainServerSocketChannel() {
+        try {
+            return (ServerSocketChannel) openServerSocketChannelFamilyMethod.invoke
+                    (null, StandardProtocolFamily.valueOf("UNIX"));
+        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+}
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
index 0914502..543eb52 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -21,8 +21,10 @@ import java.io.IOException;
 import java.lang.reflect.AccessibleObject;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.net.SocketAddress;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.channels.ServerSocketChannel;
 import java.util.Deque;
 import java.util.jar.JarFile;
 
@@ -50,19 +52,27 @@ public class JreCompat {
     protected static final Method getApplicationProtocolMethod;
 
     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
+        }
+        graalAvailable = result || System.getProperty("org.graalvm.nativeimage.imagecode") != null;
+
         // This is Tomcat 10 with a minimum Java version of Java 8.
         // Look for the highest supported JVM first
-        if (GraalCompat.isSupported()) {
-            instance = new GraalCompat();
-            graalAvailable = true;
-            jre9Available = Jre9Compat.isSupported();
+        if (Jre16Compat.isSupported()) {
+            instance = new Jre16Compat();
+            jre9Available = true;
         } else if (Jre9Compat.isSupported()) {
             instance = new Jre9Compat();
-            graalAvailable = false;
             jre9Available = true;
         } else {
             instance = new JreCompat();
-            graalAvailable = false;
             jre9Available = false;
         }
         jre11Available = instance.jarFileRuntimeMajorVersion() >= 11;
@@ -281,4 +291,24 @@ public class JreCompat {
     public String getModuleName(Class<?> type) {
         return "NO_MODULE_JAVA_8";
     }
+
+
+    /**
+     * Return Unix domain socket address for given path.
+     * @param path The path
+     * @return the socket address
+     */
+    public SocketAddress getUnixDomainSocketAddress(String path) {
+        return null;
+    }
+
+
+    /**
+     * Create server socket channel using the specified socket domain socket address.
+     * @return the server socket channel
+     */
+    public ServerSocketChannel openUnixDomainServerSocketChannel() {
+        throw new UnsupportedOperationException(sm.getString("jreCompat.noUnixDomainSocket"));
+    }
+
 }
diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties
index 891782c..8b55643 100644
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
@@ -17,5 +17,9 @@ jre9Compat.invalidModuleUri=The module URI provided [{0}] could not be converted
 jre9Compat.javaPre9=Class not found so assuming code is running on a pre-Java 9 JVM
 jre9Compat.unexpected=Failed to create references to Java 9 classes and methods
 
+jre16Compat.javaPre16=Class not found so assuming code is running on a pre-Java 16 JVM
+jre16Compat.unexpected=Failed to create references to Java 16 classes and methods
+
 jreCompat.noApplicationProtocol=Java Runtime does not support SSLEngine.getApplicationProtocol(). You must use Java 9 to use this feature.
 jreCompat.noApplicationProtocols=Java Runtime does not support SSLParameters.setApplicationProtocols(). You must use Java 9 to use this feature.
+jreCompat.noUnixDomainSocket=Java Runtime does not support Unix domain sockets. You must use Java 16 to use this feature.


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