openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1808082 - in /openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave: Meecrowave.java logging/log4j2/Log4j2Shutdown.java
Date Tue, 12 Sep 2017 05:18:17 GMT
Author: struberg
Date: Tue Sep 12 05:18:16 2017
New Revision: 1808082

URL: http://svn.apache.org/viewvc?rev=1808082&view=rev
Log:
MEECROWAVE-65 improve system props backup and log4j2 shutdown hook handling

log4j.shutdownHookEnabled got set a bit too late, so it didn't work.
Which made the logs during shutdown disappear. 

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
    openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/logging/log4j2/Log4j2Shutdown.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java?rev=1808082&r1=1808081&r2=1808082&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/Meecrowave.java
Tue Sep 12 05:18:16 2017
@@ -372,10 +372,32 @@ public class Meecrowave implements AutoC
     }
 
     public Meecrowave start() {
+        final Map<String, String> systemPropsToRestore = new HashMap<>();
+
         if (configuration.getMeecrowaveProperties() != null && !"meecrowave.properties".equals(configuration.getMeecrowaveProperties()))
{
             configuration.loadFrom(configuration.getMeecrowaveProperties());
         }
 
+        if (configuration.loggingGlobalSetup) {
+
+            setSystemProperty(systemPropsToRestore, "log4j.shutdownHookEnabled", "false");
+            setSystemProperty(systemPropsToRestore, "openwebbeans.logging.factory", Log4j2LoggerFactory.class.getName());
+            setSystemProperty(systemPropsToRestore, "org.apache.cxf.Logger", Log4j2Logger.class.getName());
+            setSystemProperty(systemPropsToRestore, "org.apache.tomcat.Logger", Log4j2Log.class.getName());
+
+            postTask = () -> {
+                new Log4j2Shutdown().shutodwn();
+                systemPropsToRestore.entrySet().forEach(entry -> {
+                    if (entry.getValue() == null) {
+                        System.clearProperty(entry.getKey());
+                    }
+                    else {
+                        System.setProperty(entry.getKey(), entry.getValue());
+                    }
+                });
+            };
+        }
+
         setupJmx(configuration.isTomcatNoJmx());
 
         clearCatalinaSystemProperties = System.getProperty("catalina.base") == null &&
System.getProperty("catalina.home") == null;
@@ -383,42 +405,6 @@ public class Meecrowave implements AutoC
             System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
         }
 
-        if (configuration.loggingGlobalSetup) {
-            final String[] toRestore = new String[]{
-                    System.getProperty("openwebbeans.logging.factory"),
-                    System.getProperty("org.apache.cxf.Logger"),
-                    System.getProperty("org.apache.tomcat.Logger"),
-                    System.getProperty("log4j.shutdownHookEnabled")
-            };
-            System.setProperty("openwebbeans.logging.factory", Log4j2LoggerFactory.class.getName());
-            System.setProperty("org.apache.cxf.Logger", Log4j2Logger.class.getName());
-            System.setProperty("org.apache.tomcat.Logger", Log4j2Log.class.getName());
-            System.setProperty("log4j.shutdownHookEnabled", "false");
-            postTask = () -> {
-                if (toRestore[0] == null) {
-                    System.clearProperty("openwebbeans.logging.factory");
-                } else {
-                    System.setProperty("openwebbeans.logging.factory", toRestore[0]);
-                }
-                if (toRestore[1] == null) {
-                    System.clearProperty("org.apache.cxf.Logger");
-                } else {
-                    System.setProperty("org.apache.cxf.Logger", toRestore[1]);
-                }
-                if (toRestore[2] == null) {
-                    System.clearProperty("org.apache.tomcat.Logger");
-                } else {
-                    System.setProperty("org.apache.tomcat.Logger", toRestore[2]);
-                }
-
-                new Log4j2Shutdown().run();
-                if (toRestore[3] == null) { // note this can be too late
-                    System.clearProperty("log4j.shutdownHookEnabled");
-                } else {
-                    System.setProperty("log4j.shutdownHookEnabled", toRestore[3]);
-                }
-            };
-        }
         if (configuration.quickSession) {
             tomcat = new TomcatWithFastSessionIDs();
         } else {
@@ -660,6 +646,20 @@ public class Meecrowave implements AutoC
         return this;
     }
 
+
+    /**
+     * Store away the current system property for restoring it later
+     * during shutdown.
+     * @param backupPropertyMap a Map to store away the previous value before setting the
newValue
+     * @param propertyKey
+     * @param newValue
+     */
+    private void setSystemProperty(Map<String, String> backupPropertyMap, String propertyKey,
String newValue) {
+        backupPropertyMap.put(propertyKey, System.getProperty(propertyKey));
+
+        System.setProperty(propertyKey, newValue);
+    }
+
     private void broadcastHostEvent(final String event, final Host host) {
         switch (event) {
             case Lifecycle.AFTER_START_EVENT: {

Modified: openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/logging/log4j2/Log4j2Shutdown.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/logging/log4j2/Log4j2Shutdown.java?rev=1808082&r1=1808081&r2=1808082&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/logging/log4j2/Log4j2Shutdown.java
(original)
+++ openwebbeans/meecrowave/trunk/meecrowave-core/src/main/java/org/apache/meecrowave/logging/log4j2/Log4j2Shutdown.java
Tue Sep 12 05:18:16 2017
@@ -20,9 +20,12 @@ package org.apache.meecrowave.logging.lo
 
 import org.apache.logging.log4j.LogManager;
 
-public class Log4j2Shutdown implements Runnable {
-    @Override
-    public void run() {
+/**
+ * Hide away Log4j2 stuff
+ */
+public class Log4j2Shutdown {
+
+    public void shutodwn() {
         try {
             // We disabled the log4j shutdown hook to gain more control and keep logs during
shutdown.
             // See #disableLog4jShutdownHook()



Mime
View raw message