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

remm 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 17d119711c Add test utilities similar to APR
17d119711c is described below

commit 17d119711c369179ac3060742b81174535d71833
Author: remm <r...@apache.org>
AuthorDate: Wed Oct 25 14:38:45 2023 +0200

    Add test utilities similar to APR
---
 .../catalina/core/OpenSSLLifecycleListener.java    | 94 +++++++++++++++-------
 .../util/net/openssl/panama/OpenSSLLibrary.java    |  4 -
 2 files changed, 63 insertions(+), 35 deletions(-)

diff --git a/java/org/apache/catalina/core/OpenSSLLifecycleListener.java 
b/java/org/apache/catalina/core/OpenSSLLifecycleListener.java
index c5bb8f7e6f..98d7d7d6b0 100644
--- a/java/org/apache/catalina/core/OpenSSLLifecycleListener.java
+++ b/java/org/apache/catalina/core/OpenSSLLifecycleListener.java
@@ -25,6 +25,7 @@ import org.apache.juli.logging.Log;
 import org.apache.juli.logging.LogFactory;
 import org.apache.tomcat.util.ExceptionUtils;
 import org.apache.tomcat.util.compat.JreCompat;
+import org.apache.tomcat.util.net.openssl.OpenSSLStatus;
 import org.apache.tomcat.util.res.StringManager;
 
 
@@ -44,6 +45,32 @@ public class OpenSSLLifecycleListener implements 
LifecycleListener {
      */
     protected static final StringManager sm = 
StringManager.getManager(OpenSSLLifecycleListener.class);
 
+    protected static final Object lock = new Object();
+
+    public static boolean isAvailable() {
+        // https://bz.apache.org/bugzilla/show_bug.cgi?id=48613
+        if (OpenSSLStatus.isInstanceCreated()) {
+            synchronized (lock) {
+                if (!JreCompat.isJre22Available()) {
+                    OpenSSLStatus.setInitialized(true);
+                } else {
+                    try {
+                        Class<?> openSSLLibraryClass = 
Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLLibrary");
+                        openSSLLibraryClass.getMethod("init").invoke(null);
+                    } catch (Throwable t) {
+                        t = ExceptionUtils.unwrapInvocationTargetException(t);
+                        ExceptionUtils.handleThrowable(t);
+                        log.error(sm.getString("openssllistener.sslInit"), t);
+                    }
+                }
+            }
+        }
+        return OpenSSLStatus.isAvailable();
+    }
+
+    public OpenSSLLifecycleListener() {
+        OpenSSLStatus.setInstanceCreated(true);
+    }
 
     // ---------------------------------------------- LifecycleListener Methods
 
@@ -61,40 +88,45 @@ public class OpenSSLLifecycleListener implements 
LifecycleListener {
                 log.warn(sm.getString("listener.notServer",
                         event.getLifecycle().getClass().getSimpleName()));
             }
-            if (!JreCompat.isJre22Available()) {
-                log.info(sm.getString("openssllistener.java22"));
-                return;
-            }
-            try {
-                Class<?> openSSLLibraryClass = 
Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLLibrary");
-                openSSLLibraryClass.getMethod("init").invoke(null);
-            } catch (Throwable t) {
-                t = ExceptionUtils.unwrapInvocationTargetException(t);
-                ExceptionUtils.handleThrowable(t);
-                log.error(sm.getString("openssllistener.sslInit"), t);
-                initError = true;
-            }
-            // Failure to initialize FIPS mode is fatal
-            if (!(null == getFIPSMode() || 
"off".equalsIgnoreCase(getFIPSMode())) && !isFIPSModeActive()) {
-                String errorMessage = 
sm.getString("openssllistener.initializeFIPSFailed");
-                Error e = new Error(errorMessage);
-                // Log here, because thrown error might be not logged
-                log.fatal(errorMessage, e);
-                initError = true;
+            synchronized (lock) {
+                if (!JreCompat.isJre22Available()) {
+                    log.info(sm.getString("openssllistener.java22"));
+                    OpenSSLStatus.setInitialized(true);
+                    return;
+                }
+                try {
+                    Class<?> openSSLLibraryClass = 
Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLLibrary");
+                    openSSLLibraryClass.getMethod("init").invoke(null);
+                } catch (Throwable t) {
+                    t = ExceptionUtils.unwrapInvocationTargetException(t);
+                    ExceptionUtils.handleThrowable(t);
+                    log.error(sm.getString("openssllistener.sslInit"), t);
+                    initError = true;
+                }
+                // Failure to initialize FIPS mode is fatal
+                if (!(null == getFIPSMode() || 
"off".equalsIgnoreCase(getFIPSMode())) && !isFIPSModeActive()) {
+                    String errorMessage = 
sm.getString("openssllistener.initializeFIPSFailed");
+                    Error e = new Error(errorMessage);
+                    // Log here, because thrown error might be not logged
+                    log.fatal(errorMessage, e);
+                    initError = true;
+                }
             }
         }
         if (initError || 
Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType())) {
-            if (!JreCompat.isJre22Available()) {
-                return;
-            }
-            // Note: Without the listener, destroy will never be called (which 
is not a significant problem)
-            try {
-                Class<?> openSSLLibraryClass = 
Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLLibrary");
-                openSSLLibraryClass.getMethod("destroy").invoke(null);
-            } catch (Throwable t) {
-                t = ExceptionUtils.unwrapInvocationTargetException(t);
-                ExceptionUtils.handleThrowable(t);
-                log.info(sm.getString("openssllistener.destroy"));
+            synchronized (lock) {
+                if (!JreCompat.isJre22Available()) {
+                    return;
+                }
+                // Note: Without the listener, destroy will never be called 
(which is not a significant problem)
+                try {
+                    Class<?> openSSLLibraryClass = 
Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLLibrary");
+                    openSSLLibraryClass.getMethod("destroy").invoke(null);
+                } catch (Throwable t) {
+                    t = ExceptionUtils.unwrapInvocationTargetException(t);
+                    ExceptionUtils.handleThrowable(t);
+                    log.info(sm.getString("openssllistener.destroy"));
+                }
             }
         }
 
diff --git a/java/org/apache/tomcat/util/net/openssl/panama/OpenSSLLibrary.java 
b/java/org/apache/tomcat/util/net/openssl/panama/OpenSSLLibrary.java
index 260db5614f..7d6fcc1808 100644
--- a/java/org/apache/tomcat/util/net/openssl/panama/OpenSSLLibrary.java
+++ b/java/org/apache/tomcat/util/net/openssl/panama/OpenSSLLibrary.java
@@ -74,10 +74,6 @@ public class OpenSSLLibrary {
 
     protected static final Object lock = new Object();
 
-    public OpenSSLLibrary() {
-        OpenSSLStatus.setInstanceCreated(true);
-    }
-
     static MemorySegment enginePointer = MemorySegment.NULL;
 
     static void initLibrary() {


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

Reply via email to