You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2018/04/04 16:40:15 UTC

logging-log4j2 git commit: [LOG4J2-2304] Log4j2 2.8.2 JMX unregister NullPointerException. Avoid some NPEs that don't seem like they should happen. Refactor the "*" context name into a constant.

Repository: logging-log4j2
Updated Branches:
  refs/heads/release-2.x c0f1400c3 -> 20cb63d74


[LOG4J2-2304] Log4j2 2.8.2 JMX unregister NullPointerException. Avoid
some NPEs that don't seem like they should happen. Refactor the "*"
context name into a constant.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/20cb63d7
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/20cb63d7
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/20cb63d7

Branch: refs/heads/release-2.x
Commit: 20cb63d746241a8f2a85ac7db5391c6ed209e051
Parents: c0f1400
Author: Gary Gregory <ga...@gmail.com>
Authored: Wed Apr 4 10:40:11 2018 -0600
Committer: Gary Gregory <ga...@gmail.com>
Committed: Wed Apr 4 10:40:11 2018 -0600

----------------------------------------------------------------------
 .../apache/logging/log4j/core/jmx/Server.java   | 38 +++++++++++---------
 1 file changed, 21 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/20cb63d7/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java
index 09bcd48..04eaa3d 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java
@@ -54,7 +54,8 @@ import org.apache.logging.log4j.util.PropertiesUtil;
  */
 public final class Server {
 
-    /**
+    private static final String CONTEXT_NAME_ALL = "*";
+	/**
      * The domain part, or prefix ({@value}) of the {@code ObjectName} of all MBeans that instrument Log4J2 components.
      */
     public static final String DOMAIN = "org.apache.logging.log4j2";
@@ -198,8 +199,7 @@ public final class Server {
             LOGGER.debug("JMX disabled for Log4j2. Not unregistering MBeans.");
             return;
         }
-        final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
-        unregisterMBeans(mbs);
+        unregisterMBeans(ManagementFactory.getPlatformMBeanServer());
     }
 
     /**
@@ -207,16 +207,18 @@ public final class Server {
      *
      * @param mbs the MBean server to unregister from.
      */
-    public static void unregisterMBeans(final MBeanServer mbs) {
-        unregisterStatusLogger("*", mbs);
-        unregisterContextSelector("*", mbs);
-        unregisterContexts(mbs);
-        unregisterLoggerConfigs("*", mbs);
-        unregisterAsyncLoggerRingBufferAdmins("*", mbs);
-        unregisterAsyncLoggerConfigRingBufferAdmins("*", mbs);
-        unregisterAppenders("*", mbs);
-        unregisterAsyncAppenders("*", mbs);
-    }
+	public static void unregisterMBeans(final MBeanServer mbs) {
+		if (mbs != null) {
+			unregisterStatusLogger(CONTEXT_NAME_ALL, mbs);
+			unregisterContextSelector(CONTEXT_NAME_ALL, mbs);
+			unregisterContexts(mbs);
+			unregisterLoggerConfigs(CONTEXT_NAME_ALL, mbs);
+			unregisterAsyncLoggerRingBufferAdmins(CONTEXT_NAME_ALL, mbs);
+			unregisterAsyncLoggerConfigRingBufferAdmins(CONTEXT_NAME_ALL, mbs);
+			unregisterAppenders(CONTEXT_NAME_ALL, mbs);
+			unregisterAsyncAppenders(CONTEXT_NAME_ALL, mbs);
+		}
+	}
 
     /**
      * Returns the {@code ContextSelector} of the current {@code Log4jContextFactory}.
@@ -333,14 +335,16 @@ public final class Server {
         try {
             final ObjectName pattern = new ObjectName(search);
             final Set<ObjectName> found = mbs.queryNames(pattern, null);
-            if (found.isEmpty()) {
+            if (found == null || found.isEmpty()) {
             	LOGGER.trace("Unregistering but no MBeans found matching '{}'", search);
             } else {
             	LOGGER.trace("Unregistering {} MBeans: {}", found.size(), found);
             }
-            for (final ObjectName objectName : found) {
-                mbs.unregisterMBean(objectName);
-            }
+			if (found != null) {
+				for (final ObjectName objectName : found) {
+					mbs.unregisterMBean(objectName);
+				}
+			}
         } catch (final InstanceNotFoundException ex) {
             LOGGER.debug("Could not unregister MBeans for " + search + ". Ignoring " + ex);
         } catch (final Exception ex) {