-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: null-check LoggerDynamicMBean addAppender and AppenderDynamicMBean setLayout #4185
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
Merged
vy
merged 10 commits into
apache:2.x
from
SebTardif:fix/logger-dynamic-mbean-null-appender
Aug 14, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16b2582
fix: null-check LoggerDynamicMBean addAppender instantiation
SebTardif e5e9108
changelog: set issue id to PR #4185
SebTardif fd932f7
fix: null-check AppenderDynamicMBean setLayout instantiation
SebTardif 6c5fb86
changelog: point AppenderDynamicMBean fix at #4185
SebTardif 34b6142
Merge branch '2.x' into fix/logger-dynamic-mbean-null-appender
ramanathan1504 a7e7964
fix: throw MBeanException when setLayout cannot instantiate
SebTardif 3d073b3
changelog updated
ramanathan1504 d88820f
handle NPE in LoggerDynamicMBean and AppenderDynamicMBean by throwing…
ramanathan1504 b4153d5
test: address vy review on JMX DynamicMBean tests
SebTardif e8238da
Simplify changelog
vy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
log4j-1.2-api/src/test/java/org/apache/log4j/jmx/AppenderDynamicMBeanTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * 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.log4j.jmx; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import javax.management.MBeanException; | ||
| import javax.management.MBeanServer; | ||
| import javax.management.MBeanServerFactory; | ||
| import javax.management.ObjectName; | ||
| import org.apache.log4j.ConsoleAppender; | ||
| import org.apache.log4j.PatternLayout; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Regression for JMX {@code setLayout}: instantiateByClassName may return null. | ||
| */ | ||
| class AppenderDynamicMBeanTest { | ||
|
|
||
| private static final String[] SET_LAYOUT_SIGNATURE = {String.class.getName()}; | ||
|
|
||
| private MBeanServer server; | ||
|
|
||
| @BeforeEach | ||
| void createMBeanServer() { | ||
| server = MBeanServerFactory.newMBeanServer(); | ||
| } | ||
|
|
||
| /** | ||
| * Registration is required: {@code preRegister} injects the server that | ||
| * {@code registerLayoutMBean} dereferences on the success path. | ||
| */ | ||
| private AppenderDynamicMBean registerAppenderMBean(final ConsoleAppender appender) throws Exception { | ||
| final AppenderDynamicMBean mbean = new AppenderDynamicMBean(appender); | ||
| server.registerMBean(mbean, new ObjectName("log4j:appender=" + appender.getName())); | ||
| return mbean; | ||
| } | ||
|
|
||
| @Test | ||
| void setLayoutFailsWhenClassCannotBeInstantiated() throws Exception { | ||
| final ConsoleAppender appender = new ConsoleAppender(); | ||
| appender.setName("jmx-layout-test"); | ||
| final AppenderDynamicMBean mbean = registerAppenderMBean(appender); | ||
|
|
||
| final MBeanException thrown = assertThrows( | ||
| MBeanException.class, | ||
| () -> mbean.invoke( | ||
| "setLayout", new Object[] {"this.class.does.not.exist.MissingLayout"}, SET_LAYOUT_SIGNATURE)); | ||
| assertTrue(thrown.getMessage().contains("Could not instantiate layout class")); | ||
| assertInstanceOf(IllegalArgumentException.class, thrown.getTargetException()); | ||
| assertNull(appender.getLayout()); | ||
| } | ||
|
|
||
| @Test | ||
| void setLayoutStillAttachesValidLayout() throws Exception { | ||
| final ConsoleAppender appender = new ConsoleAppender(); | ||
| appender.setName("jmx-layout-valid"); | ||
| final AppenderDynamicMBean mbean = registerAppenderMBean(appender); | ||
|
|
||
| mbean.invoke("setLayout", new Object[] {PatternLayout.class.getName()}, SET_LAYOUT_SIGNATURE); | ||
| assertInstanceOf(PatternLayout.class, appender.getLayout()); | ||
| assertTrue(server.isRegistered( | ||
| new ObjectName("log4j:appender=" + appender.getName() + ",layout=" + PatternLayout.class.getName()))); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
log4j-1.2-api/src/test/java/org/apache/log4j/jmx/LoggerDynamicMBeanTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * 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.log4j.jmx; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Enumeration; | ||
| import javax.management.MBeanException; | ||
| import javax.management.MBeanServer; | ||
| import javax.management.MBeanServerFactory; | ||
| import javax.management.ObjectName; | ||
| import org.apache.log4j.Appender; | ||
| import org.apache.log4j.ConsoleAppender; | ||
| import org.apache.log4j.Logger; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Regression for invalid JMX {@code addAppender} class names: instantiation can | ||
| * return null, which must not NPE when calling {@code setName} and must not be | ||
| * reported to the client as a successful invocation. | ||
| */ | ||
| class LoggerDynamicMBeanTest { | ||
|
|
||
| private static final String[] ADD_APPENDER_SIGNATURE = {String.class.getName(), String.class.getName()}; | ||
|
|
||
| private MBeanServer server; | ||
|
|
||
| @BeforeEach | ||
| void createMBeanServer() { | ||
| server = MBeanServerFactory.newMBeanServer(); | ||
| } | ||
|
|
||
| /** | ||
| * Register through an {@link MBeanServer} so the bean goes through | ||
| * {@code preRegister}, matching real JMX use (and {@code AppenderDynamicMBeanTest}). | ||
| */ | ||
| private LoggerDynamicMBean registerLoggerMBean(final Logger logger) throws Exception { | ||
| final LoggerDynamicMBean mbean = new LoggerDynamicMBean(logger); | ||
| final String name = logger.getName().isEmpty() ? "root" : logger.getName(); | ||
| // Same ObjectName shape as HierarchyDynamicMBean.addLoggerMBean. | ||
| server.registerMBean(mbean, new ObjectName("log4j", "logger", name)); | ||
| return mbean; | ||
| } | ||
|
|
||
| @Test | ||
| void addAppenderFailsWhenClassCannotBeInstantiated() throws Exception { | ||
| final Logger logger = Logger.getLogger("jmx.LoggerDynamicMBeanTest.invalid"); | ||
| final LoggerDynamicMBean mbean = registerLoggerMBean(logger); | ||
|
|
||
| final MBeanException thrown = assertThrows( | ||
| MBeanException.class, | ||
| () -> mbean.invoke( | ||
| "addAppender", | ||
| new Object[] {"this.class.does.not.exist.MissingAppender", "should-not-attach"}, | ||
| ADD_APPENDER_SIGNATURE)); | ||
| assertTrue(thrown.getMessage().contains("Could not instantiate appender class")); | ||
| assertInstanceOf(IllegalArgumentException.class, thrown.getTargetException()); | ||
| assertFalse(hasAppenderNamed(logger, "should-not-attach")); | ||
| } | ||
|
|
||
| @Test | ||
| void addAppenderStillAttachesValidAppender() throws Exception { | ||
| final Logger logger = Logger.getLogger("jmx.LoggerDynamicMBeanTest.valid"); | ||
| final LoggerDynamicMBean mbean = registerLoggerMBean(logger); | ||
|
|
||
| final Object result = mbean.invoke( | ||
| "addAppender", new Object[] {ConsoleAppender.class.getName(), "console-jmx"}, ADD_APPENDER_SIGNATURE); | ||
| assertTrue(hasAppenderNamed(logger, "console-jmx")); | ||
| // Legacy success return value, pinned so the fix cannot silently change it. | ||
| assertEquals("Hello world.", result); | ||
| } | ||
|
|
||
| private static boolean hasAppenderNamed(final Logger logger, final String name) { | ||
| final Enumeration enumeration = logger.getAllAppenders(); | ||
| while (enumeration.hasMoreElements()) { | ||
| final Appender appender = (Appender) enumeration.nextElement(); | ||
| if (name.equals(appender.getName())) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <entry xmlns="https://logging.apache.org/xml/ns" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation=" | ||
| https://logging.apache.org/xml/ns | ||
| https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
| type="fixed"> | ||
| <issue id="4185" link="https://github.com/apache/logging-log4j2/pull/4185"/> | ||
| <description format="asciidoc"> | ||
| Fix exceptions in JMX integration | ||
| </description> | ||
| </entry> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.