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

[tomcat] branch main updated: Remove Jre9Compat for Java 11+. Simplify remaining code.

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 5b4c11d  Remove Jre9Compat for Java 11+. Simplify remaining code.
5b4c11d is described below

commit 5b4c11d7180127689a79e554ab969eb8c8ea7c8e
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Jul 28 14:10:35 2021 +0100

    Remove Jre9Compat for Java 11+. Simplify remaining code.
---
 .../org/apache/tomcat/util/compat/Jre16Compat.java |   6 +-
 java/org/apache/tomcat/util/compat/Jre9Compat.java | 261 ---------------------
 java/org/apache/tomcat/util/compat/JreCompat.java  | 234 +-----------------
 3 files changed, 7 insertions(+), 494 deletions(-)

diff --git a/java/org/apache/tomcat/util/compat/Jre16Compat.java b/java/org/apache/tomcat/util/compat/Jre16Compat.java
index 0b92c59..fd05da4 100644
--- a/java/org/apache/tomcat/util/compat/Jre16Compat.java
+++ b/java/org/apache/tomcat/util/compat/Jre16Compat.java
@@ -28,7 +28,7 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.res.StringManager;
 
-class Jre16Compat extends Jre9Compat {
+class Jre16Compat extends JreCompat {
 
     private static final Log log = LogFactory.getLog(Jre16Compat.class);
     private static final StringManager sm = StringManager.getManager(Jre16Compat.class);
@@ -65,6 +65,7 @@ class Jre16Compat extends Jre9Compat {
         return unixDomainSocketAddressClazz != null;
     }
 
+
     @Override
     public SocketAddress getUnixDomainSocketAddress(String path) {
         try {
@@ -75,6 +76,7 @@ class Jre16Compat extends Jre9Compat {
         }
     }
 
+
     @Override
     public ServerSocketChannel openUnixDomainServerSocketChannel() {
         try {
@@ -85,6 +87,7 @@ class Jre16Compat extends Jre9Compat {
         }
     }
 
+
     @Override
     public SocketChannel openUnixDomainSocketChannel() {
         try {
@@ -94,5 +97,4 @@ class Jre16Compat extends Jre9Compat {
             throw new UnsupportedOperationException(e);
         }
     }
-
 }
diff --git a/java/org/apache/tomcat/util/compat/Jre9Compat.java b/java/org/apache/tomcat/util/compat/Jre9Compat.java
deleted file mode 100644
index 87a3c38..0000000
--- a/java/org/apache/tomcat/util/compat/Jre9Compat.java
+++ /dev/null
@@ -1,261 +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.File;
-import java.io.IOException;
-import java.lang.reflect.AccessibleObject;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Deque;
-import java.util.Set;
-import java.util.jar.JarFile;
-import java.util.zip.ZipFile;
-
-import org.apache.juli.logging.Log;
-import org.apache.juli.logging.LogFactory;
-import org.apache.tomcat.util.res.StringManager;
-
-class Jre9Compat extends JreCompat {
-
-    private static final Log log = LogFactory.getLog(Jre9Compat.class);
-    private static final StringManager sm = StringManager.getManager(Jre9Compat.class);
-
-    private static final Class<?> inaccessibleObjectExceptionClazz;
-    private static final Method setDefaultUseCachesMethod;
-    private static final Method bootMethod;
-    private static final Method configurationMethod;
-    private static final Method modulesMethod;
-    private static final Method referenceMethod;
-    private static final Method locationMethod;
-    private static final Method isPresentMethod;
-    private static final Method getMethod;
-    private static final Constructor<JarFile> jarFileConstructor;
-    private static final Method isMultiReleaseMethod;
-    private static final Object RUNTIME_VERSION;
-    private static final int RUNTIME_MAJOR_VERSION;
-    private static final Method canAccessMethod;
-    private static final Method getModuleMethod;
-    private static final Method isExportedMethod;
-    private static final Method getNameMethod;
-
-    static {
-        Class<?> c1 = null;
-        Method m4 = null;
-        Method m5 = null;
-        Method m6 = null;
-        Method m7 = null;
-        Method m8 = null;
-        Method m9 = null;
-        Method m10 = null;
-        Method m11 = null;
-        Constructor<JarFile> c12 = null;
-        Method m13 = null;
-        Object o14 = null;
-        Object o15 = null;
-        Method m16 = null;
-        Method m17 = null;
-        Method m18 = null;
-        Method m19 = null;
-
-        try {
-            // Order is important for the error handling below.
-            // Must look up c1 first.
-            c1 = Class.forName("java.lang.reflect.InaccessibleObjectException");
-
-            Class<?> moduleLayerClazz = Class.forName("java.lang.ModuleLayer");
-            Class<?> configurationClazz = Class.forName("java.lang.module.Configuration");
-            Class<?> resolvedModuleClazz = Class.forName("java.lang.module.ResolvedModule");
-            Class<?> moduleReferenceClazz = Class.forName("java.lang.module.ModuleReference");
-            Class<?> optionalClazz = Class.forName("java.util.Optional");
-            Class<?> versionClazz = Class.forName("java.lang.Runtime$Version");
-            Method runtimeVersionMethod = JarFile.class.getMethod("runtimeVersion");
-            Method majorMethod = versionClazz.getMethod("major");
-
-            m4 = URLConnection.class.getMethod("setDefaultUseCaches", String.class, boolean.class);
-            m5 = moduleLayerClazz.getMethod("boot");
-            m6 = moduleLayerClazz.getMethod("configuration");
-            m7 = configurationClazz.getMethod("modules");
-            m8 = resolvedModuleClazz.getMethod("reference");
-            m9 = moduleReferenceClazz.getMethod("location");
-            m10 = optionalClazz.getMethod("isPresent");
-            m11 = optionalClazz.getMethod("get");
-            c12 = JarFile.class.getConstructor(File.class, boolean.class, int.class, versionClazz);
-            m13 = JarFile.class.getMethod("isMultiRelease");
-            o14 = runtimeVersionMethod.invoke(null);
-            o15 = majorMethod.invoke(o14);
-            m16 = AccessibleObject.class.getMethod("canAccess", new Class<?>[] { Object.class });
-            m17 = Class.class.getMethod("getModule");
-            Class<?> moduleClass = Class.forName("java.lang.Module");
-            m18 = moduleClass.getMethod("isExported", String.class);
-            m19 = moduleClass.getMethod("getName");
-
-        } catch (ClassNotFoundException e) {
-            if (c1 == null) {
-                // Must be pre-Java 9
-                log.debug(sm.getString("jre9Compat.javaPre9"), e);
-            } else {
-                // Should never happen - signature error in lookup?
-                log.error(sm.getString("jre9Compat.unexpected"), e);
-            }
-        } catch (ReflectiveOperationException | IllegalArgumentException e) {
-            // Should never happen
-            log.error(sm.getString("jre9Compat.unexpected"), e);
-        }
-
-        inaccessibleObjectExceptionClazz = c1;
-        setDefaultUseCachesMethod = m4;
-        bootMethod = m5;
-        configurationMethod = m6;
-        modulesMethod = m7;
-        referenceMethod = m8;
-        locationMethod = m9;
-        isPresentMethod = m10;
-        getMethod = m11;
-        jarFileConstructor = c12;
-        isMultiReleaseMethod = m13;
-
-        RUNTIME_VERSION = o14;
-        if (o15 != null) {
-            RUNTIME_MAJOR_VERSION = ((Integer) o15).intValue();
-        } else {
-            // Must be Java 8
-            RUNTIME_MAJOR_VERSION = 8;
-        }
-
-        canAccessMethod = m16;
-        getModuleMethod = m17;
-        isExportedMethod = m18;
-        getNameMethod = m19;
-    }
-
-
-    static boolean isSupported() {
-        return inaccessibleObjectExceptionClazz != null;
-    }
-
-
-    @Override
-    public boolean isInstanceOfInaccessibleObjectException(Throwable t) {
-        if (t == null) {
-            return false;
-        }
-
-        return inaccessibleObjectExceptionClazz.isAssignableFrom(t.getClass());
-    }
-
-
-    @Override
-    public void disableCachingForJarUrlConnections() throws IOException {
-        try {
-            setDefaultUseCachesMethod.invoke(null, "JAR", Boolean.FALSE);
-        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
-            throw new UnsupportedOperationException(e);
-        }
-    }
-
-
-    @Override
-    public void addBootModulePath(Deque<URL> classPathUrlsToProcess) {
-        try {
-            Object bootLayer = bootMethod.invoke(null);
-            Object bootConfiguration = configurationMethod.invoke(bootLayer);
-            Set<?> resolvedModules = (Set<?>) modulesMethod.invoke(bootConfiguration);
-            for (Object resolvedModule : resolvedModules) {
-                Object moduleReference = referenceMethod.invoke(resolvedModule);
-                Object optionalURI = locationMethod.invoke(moduleReference);
-                Boolean isPresent = (Boolean) isPresentMethod.invoke(optionalURI);
-                if (isPresent.booleanValue()) {
-                    URI uri = (URI) getMethod.invoke(optionalURI);
-                    try {
-                        URL url = uri.toURL();
-                        classPathUrlsToProcess.add(url);
-                    } catch (MalformedURLException e) {
-                        log.warn(sm.getString("jre9Compat.invalidModuleUri", uri), e);
-                    }
-                }
-            }
-        } catch (ReflectiveOperationException e) {
-            throw new UnsupportedOperationException(e);
-        }
-    }
-
-
-    @Override
-    public JarFile jarFileNewInstance(File f) throws IOException {
-        try {
-            return jarFileConstructor.newInstance(
-                    f, Boolean.TRUE, Integer.valueOf(ZipFile.OPEN_READ), RUNTIME_VERSION);
-        } catch (ReflectiveOperationException | IllegalArgumentException e) {
-            throw new IOException(e);
-        }
-    }
-
-
-    @Override
-    public boolean jarFileIsMultiRelease(JarFile jarFile) {
-        try {
-            return ((Boolean) isMultiReleaseMethod.invoke(jarFile)).booleanValue();
-        } catch (ReflectiveOperationException | IllegalArgumentException e) {
-            return false;
-        }
-    }
-
-
-    @Override
-    public int jarFileRuntimeMajorVersion() {
-        return RUNTIME_MAJOR_VERSION;
-    }
-
-
-    @Override
-    public boolean canAccess(Object base, AccessibleObject accessibleObject) {
-        try {
-            return ((Boolean) canAccessMethod.invoke(accessibleObject, base)).booleanValue();
-        } catch (ReflectiveOperationException | IllegalArgumentException e) {
-            return false;
-        }
-    }
-
-
-    @Override
-    public boolean isExported(Class<?> type) {
-        try {
-            String packageName = type.getPackage().getName();
-            Object module = getModuleMethod.invoke(type);
-            return ((Boolean) isExportedMethod.invoke(module, packageName)).booleanValue();
-        } catch (ReflectiveOperationException e) {
-            return false;
-        }
-    }
-
-
-    @Override
-    public String getModuleName(Class<?> type) {
-        try {
-            Object module = getModuleMethod.invoke(type);
-            return (String) getNameMethod.invoke(module);
-        } catch (ReflectiveOperationException e) {
-            return "ERROR";
-        }
-    }
-}
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
index d58c1a0..6657973 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -16,21 +16,9 @@
  */
 package org.apache.tomcat.util.compat;
 
-import java.io.File;
-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.nio.channels.SocketChannel;
-import java.util.Deque;
-import java.util.jar.JarFile;
-
-import javax.net.ssl.SSLEngine;
-import javax.net.ssl.SSLParameters;
 
 import org.apache.tomcat.util.res.StringManager;
 
@@ -41,18 +29,11 @@ import org.apache.tomcat.util.res.StringManager;
  */
 public class JreCompat {
 
-    private static final int RUNTIME_MAJOR_VERSION = 8;
-
     private static final JreCompat instance;
     private static final boolean graalAvailable;
     private static final boolean jre16Available;
-    private static final boolean jre11Available;
-    private static final boolean jre9Available;
     private static final StringManager sm = StringManager.getManager(JreCompat.class);
 
-    protected static final Method setApplicationProtocolsMethod;
-    protected static final Method getApplicationProtocolMethod;
-
     static {
         boolean result = false;
         try {
@@ -65,33 +46,15 @@ public class JreCompat {
         }
         graalAvailable = result || System.getProperty("org.graalvm.nativeimage.imagecode") != null;
 
-        // This is Tomcat 10 with a minimum Java version of Java 8.
+        // This is Tomcat 10.1.x with a minimum Java version of Java 11.
         // Look for the highest supported JVM first
         if (Jre16Compat.isSupported()) {
             instance = new Jre16Compat();
-            jre9Available = true;
             jre16Available = true;
-        } else if (Jre9Compat.isSupported()) {
-            instance = new Jre9Compat();
-            jre9Available = true;
-            jre16Available = false;
         } else {
             instance = new JreCompat();
-            jre9Available = false;
             jre16Available = false;
         }
-        jre11Available = instance.jarFileRuntimeMajorVersion() >= 11;
-
-        Method m1 = null;
-        Method m2 = null;
-        try {
-            m1 = SSLParameters.class.getMethod("setApplicationProtocols", String[].class);
-            m2 = SSLEngine.class.getMethod("getApplicationProtocol");
-        } catch (ReflectiveOperationException | IllegalArgumentException e) {
-            // Only the newest Java 8 have the ALPN API, so ignore
-        }
-        setApplicationProtocolsMethod = m1;
-        getApplicationProtocolMethod = m2;
     }
 
 
@@ -105,203 +68,12 @@ public class JreCompat {
     }
 
 
-    public static boolean isAlpnSupported() {
-        return setApplicationProtocolsMethod != null && getApplicationProtocolMethod != null;
-    }
-
-
-    public static boolean isJre9Available() {
-        return jre9Available;
-    }
-
-
-    public static boolean isJre11Available() {
-        return jre11Available;
-    }
-
-
     public static boolean isJre16Available() {
         return jre16Available;
     }
 
 
-    // Java 8 implementation of Java 9 methods
-
-    /**
-     * Test if the provided exception is an instance of
-     * java.lang.reflect.InaccessibleObjectException.
-     *
-     * @param t The exception to test
-     *
-     * @return {@code true} if the exception is an instance of
-     *         InaccessibleObjectException, otherwise {@code false}
-     */
-    public boolean isInstanceOfInaccessibleObjectException(Throwable t) {
-        // Exception does not exist prior to Java 9
-        return false;
-    }
-
-
-    /**
-     * Set the application protocols the server will accept for ALPN
-     *
-     * @param sslParameters The SSL parameters for a connection
-     * @param protocols     The application protocols to be allowed for that
-     *                      connection
-     */
-    public void setApplicationProtocols(SSLParameters sslParameters, String[] protocols) {
-        if (setApplicationProtocolsMethod != null) {
-            try {
-                setApplicationProtocolsMethod.invoke(sslParameters, (Object) protocols);
-            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
-                throw new UnsupportedOperationException(e);
-            }
-        } else {
-            throw new UnsupportedOperationException(sm.getString("jreCompat.noApplicationProtocols"));
-        }
-    }
-
-
-    /**
-     * Get the application protocol that has been negotiated for connection
-     * associated with the given SSLEngine.
-     *
-     * @param sslEngine The SSLEngine for which to obtain the negotiated
-     *                  protocol
-     *
-     * @return The name of the negotiated protocol
-     */
-    public String getApplicationProtocol(SSLEngine sslEngine) {
-        if (getApplicationProtocolMethod != null) {
-            try {
-                return (String) getApplicationProtocolMethod.invoke(sslEngine);
-            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
-                throw new UnsupportedOperationException(e);
-            }
-        } else {
-            throw new UnsupportedOperationException(sm.getString("jreCompat.noApplicationProtocol"));
-        }
-    }
-
-
-    /**
-     * Disables caching for JAR URL connections. For Java 8 and earlier, this also disables
-     * caching for ALL URL connections.
-     *
-     * @throws IOException If a dummy JAR URLConnection can not be created
-     */
-    public void disableCachingForJarUrlConnections() throws IOException {
-        // Doesn't matter that this JAR doesn't exist - just as
-        // long as the URL is well-formed
-        URL url = new URL("jar:file://dummy.jar!/");
-        URLConnection uConn = url.openConnection();
-        uConn.setDefaultUseCaches(false);
-    }
-
-
-    /**
-     * Obtains the URLs for all the JARs on the module path when the JVM starts
-     * and adds them to the provided Deque.
-     *
-     * @param classPathUrlsToProcess    The Deque to which the modules should be
-     *                                  added
-     */
-    public void addBootModulePath(Deque<URL> classPathUrlsToProcess) {
-        // NO-OP for Java 8. There is no module path.
-    }
-
-
-    /**
-     * Creates a new JarFile instance. When running on Java 9 and later, the
-     * JarFile will be multi-release JAR aware. While this isn't strictly
-     * required to be in this package, it is provided as a convenience method.
-     *
-     * @param s The JAR file to open
-     *
-     * @return A JarFile instance based on the provided path
-     *
-     * @throws IOException  If an I/O error occurs creating the JarFile instance
-     */
-    public final JarFile jarFileNewInstance(String s) throws IOException {
-        return jarFileNewInstance(new File(s));
-    }
-
-
-    /**
-     * Creates a new JarFile instance. When running on Java 9 and later, the
-     * JarFile will be multi-release JAR aware.
-     *
-     * @param f The JAR file to open
-     *
-     * @return A JarFile instance based on the provided file
-     *
-     * @throws IOException  If an I/O error occurs creating the JarFile instance
-     */
-    public JarFile jarFileNewInstance(File f) throws IOException {
-        return new JarFile(f);
-    }
-
-
-    /**
-     * Is this JarFile a multi-release JAR file.
-     *
-     * @param jarFile   The JarFile to test
-     *
-     * @return {@code true} If it is a multi-release JAR file and is configured
-     *         to behave as such.
-     */
-    public boolean jarFileIsMultiRelease(JarFile jarFile) {
-        // Java 8 doesn't support multi-release so default to false
-        return false;
-    }
-
-
-    public int jarFileRuntimeMajorVersion() {
-        return RUNTIME_MAJOR_VERSION;
-    }
-
-
-    /**
-     * Is the accessibleObject accessible (as a result of appropriate module
-     * exports) on the provided instance?
-     *
-     * @param base  The specific instance to be tested.
-     * @param accessibleObject  The method/field/constructor to be tested.
-     *
-     * @return {code true} if the AccessibleObject can be accessed otherwise
-     *         {code false}
-     */
-    public boolean canAccess(Object base, AccessibleObject accessibleObject) {
-        // Java 8 doesn't support modules so default to true
-        return true;
-    }
-
-
-    /**
-     * Is the given class in an exported package?
-     *
-     * @param type  The class to test
-     *
-     * @return Always {@code true} for Java 8. {@code true} if the enclosing
-     *         package is exported for Java 9+
-     */
-    public boolean isExported(Class<?> type) {
-        return true;
-    }
-
-
-    /**
-     * What is the module of the given class?
-     *
-     * @param type  The class to test
-     *
-     * @return Always {@code true} for Java 8. {@code true} if the enclosing
-     *         package is exported for Java 9+
-     */
-    public String getModuleName(Class<?> type) {
-        return "NO_MODULE_JAVA_8";
-    }
-
+    // Java 11 implementations of Java 16 methods
 
     /**
      * Return Unix domain socket address for given path.
@@ -321,6 +93,7 @@ public class JreCompat {
         throw new UnsupportedOperationException(sm.getString("jreCompat.noUnixDomainSocket"));
     }
 
+
     /**
      * Create socket channel using the Unix domain socket ProtocolFamily.
      * @return the socket channel
@@ -328,5 +101,4 @@ public class JreCompat {
     public SocketChannel openUnixDomainSocketChannel() {
         throw new UnsupportedOperationException(sm.getString("jreCompat.noUnixDomainSocket"));
     }
-
 }

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