Skip to content

Commit 2691f08

Browse files
committed
Set classloader for JMX endpoints to application classloader, #12088.
1 parent 602f733 commit 2691f08

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public Object invoke(String actionName, Object[] params, String[] signature)
9090
+ "' has no operation named " + actionName;
9191
throw new ReflectionException(new IllegalArgumentException(message), message);
9292
}
93+
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
9394
return invoke(operation, params);
9495
}
9596

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.boot.actuate.endpoint.jmx;
1818

19+
import java.net.URL;
20+
import java.net.URLClassLoader;
21+
1922
import javax.management.Attribute;
2023
import javax.management.AttributeList;
2124
import javax.management.AttributeNotFoundException;
@@ -29,6 +32,8 @@
2932
import org.junit.rules.ExpectedException;
3033
import reactor.core.publisher.Mono;
3134

35+
import org.springframework.util.ClassUtils;
36+
3237
import static org.assertj.core.api.Assertions.assertThat;
3338
import static org.hamcrest.CoreMatchers.instanceOf;
3439
import static org.mockito.Mockito.mock;
@@ -84,6 +89,23 @@ public void invokeShouldInvokeJmxOperation()
8489
assertThat(result).isEqualTo("result");
8590
}
8691

92+
@Test
93+
public void invokeShouldInvokeJmxOperationWithApplicationClassLoader()
94+
throws ReflectionException, MBeanException {
95+
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(
96+
new TestJmxOperation((arguments) -> ClassUtils.getDefaultClassLoader()));
97+
EndpointMBean bean = new EndpointMBean(this.responseMapper, endpoint);
98+
Object result;
99+
Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[]{}, getClass().getClassLoader()));
100+
try {
101+
result = bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE);
102+
}
103+
finally {
104+
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
105+
}
106+
assertThat(result).isEqualTo(getClass().getClassLoader());
107+
}
108+
87109
@Test
88110
public void invokeWhenActionNameIsNotAnOperationShouldThrowException()
89111
throws MBeanException, ReflectionException {

0 commit comments

Comments
 (0)