Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid java.management dependency when JMX is disabled #2775

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions log4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
javax.lang.model.*;resolution:=optional,
javax.tools;resolution:=optional,
<!-- `java.sql`, which depends on `java.logging` is optional -->
java.sql;resolution:=optional,
javax.sql;resolution:=optional,
java.util.logging;resolution:=optional,
<!-- `java.management` is optional -->
java.lang.management;resolution:=optional,
javax.management.*;resolution:=optional,
<!-- `java.naming` is optional -->
javax.naming;resolution:=optional
</bnd-extra-package-options>
Expand All @@ -88,6 +92,7 @@
com.fasterxml.jackson.databind;transitive=false,
com.fasterxml.jackson.dataformat.xml;transitive=false,
com.fasterxml.jackson.dataformat.yaml;transitive=false,
java.management;transitive=false;static=true,
java.naming;transitive=false,
org.apache.commons.csv;transitive=false,
org.fusesource.jansi;transitive=false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core;

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

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

this.setStopping();
try {
Server.unregisterLoggerContext(getName()); // LOG4J2-406, LOG4J2-500
} catch (final LinkageError | Exception e) {
// LOG4J2-1506 Hello Android, GAE
LOGGER.error("Unable to unregister MBeans", e);
}
unregisterJmxBeans();
if (shutdownCallback != null) {
shutdownCallback.cancel();
shutdownCallback = null;
Expand Down Expand Up @@ -407,6 +403,17 @@ public boolean stop(final long timeout, final TimeUnit timeUnit) {
return true;
}

private void unregisterJmxBeans() {
if (!isJmxDisabled()) {
try {
Server.unregisterLoggerContext(getName()); // LOG4J2-406, LOG4J2-500
} catch (final LinkageError | Exception error) {
// LOG4J2-1506 Hello Android, GAE
LOGGER.error("Unable to unregister MBeans", error);
}
}
}

/**
* Gets the name.
*
Expand Down Expand Up @@ -638,12 +645,8 @@ public Configuration setConfiguration(final Configuration config) {

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

try {
Server.reregisterMBeansAfterReconfigure();
} catch (final LinkageError | Exception e) {
// LOG4J2-716: Android has no java.lang.management
LOGGER.error("Could not reconfigure JMX", e);
}
registerJmxBeans();

// AsyncLoggers update their nanoClock when the configuration changes
Log4jLogEvent.setNanoClock(configuration.getNanoClock());

Expand All @@ -653,6 +656,17 @@ public Configuration setConfiguration(final Configuration config) {
}
}

private static void registerJmxBeans() {
if (!isJmxDisabled()) {
try {
Server.reregisterMBeansAfterReconfigure();
} catch (final LinkageError | Exception error) {
// LOG4J2-716: Android has no java.lang.management
LOGGER.error("Could not reconfigure JMX", error);
}
}
}

private void firePropertyChangeEvent(final PropertyChangeEvent event) {
for (final PropertyChangeListener listener : propertyChangeListeners) {
listener.propertyChange(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.logging.log4j.core.jmx;

import static org.apache.logging.log4j.core.jmx.internal.JmxUtil.isJmxDisabled;

import java.lang.management.ManagementFactory;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,7 +60,6 @@ public final class Server {
*/
public static final String DOMAIN = "org.apache.logging.log4j2";

private static final String PROPERTY_DISABLE_JMX = "log4j2.disable.jmx";
private static final String PROPERTY_ASYNC_NOTIF = "log4j2.jmx.notify.async";
private static final String THREAD_NAME_PREFIX = "jmx.notif";
private static final StatusLogger LOGGER = StatusLogger.getLogger();
Expand Down Expand Up @@ -126,10 +127,6 @@ public static String escape(final String name) {
return sb.toString();
}

private static boolean isJmxDisabled() {
return PropertiesUtil.getProperties().getBooleanProperty(PROPERTY_DISABLE_JMX, true);
}

public static void reregisterMBeansAfterReconfigure() {
// avoid creating Platform MBean Server if JMX disabled
if (isJmxDisabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.logging.log4j.core.jmx.internal;

import org.apache.logging.log4j.util.PropertiesUtil;

// WARNING!
// This class must be free of any dependencies to the `java.management` module!
// Otherwise, `isJmxDisabled()` call sites would unnecessarily require the `java.management` module.
// For details, see: https://github.com/apache/logging-log4j2/issues/2774

public final class JmxUtil {

public static boolean isJmxDisabled() {
return PropertiesUtil.getProperties().getBooleanProperty("log4j2.disable.jmx", true);
}

private JmxUtil() {}
}
8 changes: 8 additions & 0 deletions src/changelog/.2.x.x/fix_jmx_module_dep.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="2775" link="https://github.com/apache/logging-log4j2/pull/2775"/>
<description format="asciidoc">Fix requirement on the `java.management` module when JMX is disabled, which is the default</description>
</entry>