Skip to content

Commit a7e7964

Browse files
committed
fix: throw MBeanException when setLayout cannot instantiate
Returning a diagnostic string from a void-declared JMX operation is easy for clients to treat as success. Throw MBeanException with an IllegalArgumentException target instead so failure is unambiguous, while still logging the error. Update the regression test to assert MBeanException. Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
1 parent 34b6142 commit a7e7964

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

log4j-1.2-api/src/main/java/org/apache/log4j/jmx/AppenderDynamicMBean.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,12 @@ public Object invoke(final String operationName, final Object params[], final St
188188
final Layout layout =
189189
(Layout) OptionConverter.instantiateByClassName((String) params[0], Layout.class, null);
190190
if (layout == null) {
191-
cat.error("Could not instantiate layout class [" + params[0] + "] for appender ["
192-
+ getAppenderName(appender) + "].");
193-
return "Could not instantiate layout class.";
191+
final String message = "Could not instantiate layout class [" + params[0] + "] for appender ["
192+
+ getAppenderName(appender) + "].";
193+
cat.error(message);
194+
// Fail via MBeanException so JMX clients can distinguish failure from
195+
// success (setLayout is declared void; a return string is not reliable).
196+
throw new MBeanException(new IllegalArgumentException(message), message);
194197
}
195198
appender.setLayout(layout);
196199
registerLayoutMBean(layout);

log4j-1.2-api/src/test/java/org/apache/log4j/jmx/AppenderDynamicMBeanTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
*/
1717
package org.apache.log4j.jmx;
1818

19-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
19+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

25+
import javax.management.MBeanException;
2426
import org.apache.log4j.ConsoleAppender;
2527
import org.apache.log4j.PatternLayout;
2628
import org.junit.jupiter.api.Test;
@@ -36,11 +38,13 @@ void setLayoutDoesNotNpeWhenClassCannotBeInstantiated() throws Exception {
3638
appender.setName("jmx-layout-test");
3739
final AppenderDynamicMBean mbean = new AppenderDynamicMBean(appender);
3840

39-
final Object result = assertDoesNotThrow(
41+
final MBeanException thrown = assertThrows(
42+
MBeanException.class,
4043
() -> mbean.invoke("setLayout", new Object[] {"this.class.does.not.exist.MissingLayout"}, new String[] {
4144
String.class.getName()
4245
}));
43-
assertTrue(result == null || result.toString().contains("Could not instantiate"));
46+
assertTrue(thrown.getMessage().contains("Could not instantiate layout class"));
47+
assertInstanceOf(IllegalArgumentException.class, thrown.getTargetException());
4448
assertNull(appender.getLayout());
4549
}
4650

0 commit comments

Comments
 (0)