Skip to content

Commit 1e2e920

Browse files
vyppkarwasz
andauthored
Avoid java.management dependency when JMX is disabled (#2775)
Co-authored-by: Piotr P. Karwasz <[email protected]>
1 parent 13bf08d commit 1e2e920

File tree

5 files changed

+74
-17
lines changed

5 files changed

+74
-17
lines changed

log4j-core/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@
7070
javax.lang.model.*;resolution:=optional,
7171
javax.tools;resolution:=optional,
7272
<!-- `java.sql`, which depends on `java.logging` is optional -->
73+
java.sql;resolution:=optional,
7374
javax.sql;resolution:=optional,
7475
java.util.logging;resolution:=optional,
76+
<!-- `java.management` is optional -->
77+
java.lang.management;resolution:=optional,
78+
javax.management.*;resolution:=optional,
7579
<!-- `java.naming` is optional -->
7680
javax.naming;resolution:=optional
7781
</bnd-extra-package-options>
@@ -88,6 +92,7 @@
8892
com.fasterxml.jackson.databind;transitive=false,
8993
com.fasterxml.jackson.dataformat.xml;transitive=false,
9094
com.fasterxml.jackson.dataformat.yaml;transitive=false,
95+
java.management;transitive=false;static=true,
9196
java.naming;transitive=false,
9297
org.apache.commons.csv;transitive=false,
9398
org.fusesource.jansi;transitive=false,

log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

+26-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.core;
1818

19+
import static org.apache.logging.log4j.core.jmx.internal.JmxUtil.isJmxDisabled;
1920
import static org.apache.logging.log4j.core.util.ShutdownCallbackRegistry.SHUTDOWN_HOOK_MARKER;
2021

2122
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -370,12 +371,7 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
370371
}
371372

372373
this.setStopping();
373-
try {
374-
Server.unregisterLoggerContext(getName()); // LOG4J2-406, LOG4J2-500
375-
} catch (final LinkageError | Exception e) {
376-
// LOG4J2-1506 Hello Android, GAE
377-
LOGGER.error("Unable to unregister MBeans", e);
378-
}
374+
unregisterJmxBeans();
379375
if (shutdownCallback != null) {
380376
shutdownCallback.cancel();
381377
shutdownCallback = null;
@@ -407,6 +403,17 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
407403
return true;
408404
}
409405

406+
private void unregisterJmxBeans() {
407+
if (!isJmxDisabled()) {
408+
try {
409+
Server.unregisterLoggerContext(getName()); // LOG4J2-406, LOG4J2-500
410+
} catch (final LinkageError | Exception error) {
411+
// LOG4J2-1506 Hello Android, GAE
412+
LOGGER.error("Unable to unregister MBeans", error);
413+
}
414+
}
415+
}
416+
410417
/**
411418
* Gets the name.
412419
*
@@ -638,12 +645,8 @@ public Configuration setConfiguration(final Configuration config) {
638645

639646
firePropertyChangeEvent(new PropertyChangeEvent(this, PROPERTY_CONFIG, prev, config));
640647

641-
try {
642-
Server.reregisterMBeansAfterReconfigure();
643-
} catch (final LinkageError | Exception e) {
644-
// LOG4J2-716: Android has no java.lang.management
645-
LOGGER.error("Could not reconfigure JMX", e);
646-
}
648+
registerJmxBeans();
649+
647650
// AsyncLoggers update their nanoClock when the configuration changes
648651
Log4jLogEvent.setNanoClock(configuration.getNanoClock());
649652

@@ -653,6 +656,17 @@ public Configuration setConfiguration(final Configuration config) {
653656
}
654657
}
655658

659+
private static void registerJmxBeans() {
660+
if (!isJmxDisabled()) {
661+
try {
662+
Server.reregisterMBeansAfterReconfigure();
663+
} catch (final LinkageError | Exception error) {
664+
// LOG4J2-716: Android has no java.lang.management
665+
LOGGER.error("Could not reconfigure JMX", error);
666+
}
667+
}
668+
}
669+
656670
private void firePropertyChangeEvent(final PropertyChangeEvent event) {
657671
for (final PropertyChangeListener listener : propertyChangeListeners) {
658672
listener.propertyChange(event);

log4j-core/src/main/java/org/apache/logging/log4j/core/jmx/Server.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.jmx;
1818

19+
import static org.apache.logging.log4j.core.jmx.internal.JmxUtil.isJmxDisabled;
20+
1921
import java.lang.management.ManagementFactory;
2022
import java.util.List;
2123
import java.util.Map;
@@ -58,7 +60,6 @@ public final class Server {
5860
*/
5961
public static final String DOMAIN = "org.apache.logging.log4j2";
6062

61-
private static final String PROPERTY_DISABLE_JMX = "log4j2.disable.jmx";
6263
private static final String PROPERTY_ASYNC_NOTIF = "log4j2.jmx.notify.async";
6364
private static final String THREAD_NAME_PREFIX = "jmx.notif";
6465
private static final StatusLogger LOGGER = StatusLogger.getLogger();
@@ -126,10 +127,6 @@ public static String escape(final String name) {
126127
return sb.toString();
127128
}
128129

129-
private static boolean isJmxDisabled() {
130-
return PropertiesUtil.getProperties().getBooleanProperty(PROPERTY_DISABLE_JMX, true);
131-
}
132-
133130
public static void reregisterMBeansAfterReconfigure() {
134131
// avoid creating Platform MBean Server if JMX disabled
135132
if (isJmxDisabled()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.jmx.internal;
18+
19+
import org.apache.logging.log4j.util.PropertiesUtil;
20+
21+
// WARNING!
22+
// This class must be free of any dependencies to the `java.management` module!
23+
// Otherwise, `isJmxDisabled()` call sites would unnecessarily require the `java.management` module.
24+
// For details, see: https://github.com/apache/logging-log4j2/issues/2774
25+
26+
public final class JmxUtil {
27+
28+
public static boolean isJmxDisabled() {
29+
return PropertiesUtil.getProperties().getBooleanProperty("log4j2.disable.jmx", true);
30+
}
31+
32+
private JmxUtil() {}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="2775" link="https://github.com/apache/logging-log4j2/pull/2775"/>
7+
<description format="asciidoc">Fix requirement on the `java.management` module when JMX is disabled, which is the default</description>
8+
</entry>

0 commit comments

Comments
 (0)