From 13be7d06688a6914076fadf085056ff342c46220 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Fri, 17 Jan 2025 17:38:27 +0000 Subject: [PATCH] Add Java 24 detection to JreCompat --- .../tomcat/util/compat/Jre24Compat.java | 49 +++++++++++++++++++ .../apache/tomcat/util/compat/JreCompat.java | 14 +++++- .../util/compat/LocalStrings.properties | 2 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 java/org/apache/tomcat/util/compat/Jre24Compat.java diff --git a/java/org/apache/tomcat/util/compat/Jre24Compat.java b/java/org/apache/tomcat/util/compat/Jre24Compat.java new file mode 100644 index 000000000000..07fcd5c93bb1 --- /dev/null +++ b/java/org/apache/tomcat/util/compat/Jre24Compat.java @@ -0,0 +1,49 @@ +/* + * 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.Method; +import java.time.Duration; + +import org.apache.juli.logging.Log; +import org.apache.juli.logging.LogFactory; +import org.apache.tomcat.util.res.StringManager; + +public class Jre24Compat extends Jre22Compat { + + private static final Log log = LogFactory.getLog(Jre24Compat.class); + private static final StringManager sm = StringManager.getManager(Jre24Compat.class); + + private static final boolean supported; + + + static { + Method m1 = null; + try { + m1 = Process.class.getMethod("waitFor", Duration.class); + } catch (NoSuchMethodException e) { + // Must be pre-Java 24 + log.debug(sm.getString("jre22Compat.javaPre24"), e); + } + supported = (m1 != null); + } + + static boolean isSupported() { + return supported; + } + +} diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java index 6fe6254e044b..f243178c5b2f 100644 --- a/java/org/apache/tomcat/util/compat/JreCompat.java +++ b/java/org/apache/tomcat/util/compat/JreCompat.java @@ -24,6 +24,7 @@ public class JreCompat { private static final JreCompat instance; private static final boolean graalAvailable; + private static final boolean jre24Available; private static final boolean jre22Available; static { @@ -40,11 +41,17 @@ public class JreCompat { // This is Tomcat 12.0.x with a minimum Java version of Java 21. // Look for the highest supported JVM first - if (Jre22Compat.isSupported()) { + if (Jre24Compat.isSupported()) { + instance = new Jre24Compat(); + jre24Available = true; + jre22Available = true; + } else if (Jre22Compat.isSupported()) { instance = new Jre22Compat(); + jre24Available = false; jre22Available = true; } else { instance = new JreCompat(); + jre24Available = false; jre22Available = false; } } @@ -63,4 +70,9 @@ public static boolean isGraalAvailable() { public static boolean isJre22Available() { return jre22Available; } + + + public static boolean isJre24Available() { + return jre24Available; + } } diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties index 0df209897efc..51e2b7fc847e 100644 --- a/java/org/apache/tomcat/util/compat/LocalStrings.properties +++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties @@ -14,3 +14,5 @@ # limitations under the License. jre22Compat.javaPre22=Class not found so assuming code is running on a pre-Java 22 JVM + +jre24Compat.javaPre24=Method not found so assuming code is running on a pre-Java 24 JVM