|
|
|
@ -25,18 +25,26 @@ import javax.management.ReflectionException;
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.Endpoint;
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.jmx.EndpointMBeanExporter;
|
|
|
|
|
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.context.annotation.EnableMBeanExport;
|
|
|
|
|
import org.springframework.jmx.export.MBeanExporter;
|
|
|
|
|
import org.springframework.jmx.export.annotation.ManagedResource;
|
|
|
|
|
import org.springframework.jmx.support.ObjectNameManager;
|
|
|
|
|
import org.springframework.mock.env.MockEnvironment;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -55,13 +63,51 @@ public class EndpointMBeanExportAutoConfigurationTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testEndpointMBeanExporterIsInstalled() {
|
|
|
|
|
public void testEndpointMBeanExporterIsInstalled() throws Exception {
|
|
|
|
|
this.context = new AnnotationConfigApplicationContext();
|
|
|
|
|
this.context.register(TestConfiguration.class, JmxAutoConfiguration.class,
|
|
|
|
|
EndpointAutoConfiguration.class,
|
|
|
|
|
EndpointMBeanExportAutoConfiguration.class);
|
|
|
|
|
EndpointMBeanExportAutoConfiguration.class,
|
|
|
|
|
PropertyPlaceholderAutoConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
assertNotNull(this.context.getBean(EndpointMBeanExporter.class));
|
|
|
|
|
MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
|
|
|
|
|
|
|
|
|
|
assertFalse(mbeanExporter.getServer()
|
|
|
|
|
.queryNames(getObjectName("*", "*,*", this.context), null).isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testEndpointMBeanExporterIsNotInstalledIfManagedResource()
|
|
|
|
|
throws Exception {
|
|
|
|
|
this.context = new AnnotationConfigApplicationContext();
|
|
|
|
|
this.context.register(TestConfiguration.class, JmxAutoConfiguration.class,
|
|
|
|
|
ManagedEndpoint.class, EndpointMBeanExportAutoConfiguration.class,
|
|
|
|
|
PropertyPlaceholderAutoConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
assertNotNull(this.context.getBean(EndpointMBeanExporter.class));
|
|
|
|
|
|
|
|
|
|
MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
|
|
|
|
|
|
|
|
|
|
assertTrue(mbeanExporter.getServer()
|
|
|
|
|
.queryNames(getObjectName("*", "*,*", this.context), null).isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testEndpointMBeanExporterIsNotInstalledIfNestedInManagedResource()
|
|
|
|
|
throws Exception {
|
|
|
|
|
this.context = new AnnotationConfigApplicationContext();
|
|
|
|
|
this.context.register(TestConfiguration.class, JmxAutoConfiguration.class,
|
|
|
|
|
NestedInManagedEndpoint.class,
|
|
|
|
|
EndpointMBeanExportAutoConfiguration.class,
|
|
|
|
|
PropertyPlaceholderAutoConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
assertNotNull(this.context.getBean(EndpointMBeanExporter.class));
|
|
|
|
|
|
|
|
|
|
MBeanExporter mbeanExporter = this.context.getBean(EndpointMBeanExporter.class);
|
|
|
|
|
|
|
|
|
|
assertTrue(mbeanExporter.getServer()
|
|
|
|
|
.queryNames(getObjectName("*", "*,*", this.context), null).isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test(expected = NoSuchBeanDefinitionException.class)
|
|
|
|
@ -125,21 +171,23 @@ public class EndpointMBeanExportAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
|
private ObjectName getObjectName(String domain, String beanKey,
|
|
|
|
|
ApplicationContext applicationContext) throws MalformedObjectNameException {
|
|
|
|
|
String name = "%s:type=Endpoint,name=%s";
|
|
|
|
|
if (applicationContext.getParent() != null) {
|
|
|
|
|
name = name + ",context=%s";
|
|
|
|
|
}
|
|
|
|
|
if (applicationContext.getEnvironment().getProperty("endpoints.jmx.unique_names",
|
|
|
|
|
Boolean.class, false)) {
|
|
|
|
|
name = name
|
|
|
|
|
+ ",identity="
|
|
|
|
|
+ ObjectUtils.getIdentityHexString(applicationContext
|
|
|
|
|
.getBean(beanKey));
|
|
|
|
|
}
|
|
|
|
|
if (applicationContext.getParent() != null) {
|
|
|
|
|
return ObjectNameManager
|
|
|
|
|
.getInstance(String.format(
|
|
|
|
|
"%s:type=Endpoint,name=%s,context=%s,identity=%s", domain,
|
|
|
|
|
beanKey,
|
|
|
|
|
ObjectUtils.getIdentityHexString(applicationContext),
|
|
|
|
|
ObjectUtils.getIdentityHexString(applicationContext
|
|
|
|
|
.getBean(beanKey))));
|
|
|
|
|
return ObjectNameManager.getInstance(String.format(name, domain, beanKey,
|
|
|
|
|
ObjectUtils.getIdentityHexString(applicationContext)));
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return ObjectNameManager
|
|
|
|
|
.getInstance(String.format("%s:type=Endpoint,name=%s,identity=%s",
|
|
|
|
|
domain, beanKey, ObjectUtils
|
|
|
|
|
.getIdentityHexString(applicationContext
|
|
|
|
|
.getBean(beanKey))));
|
|
|
|
|
return ObjectNameManager.getInstance(String.format(name, domain, beanKey));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -148,4 +196,43 @@ public class EndpointMBeanExportAutoConfigurationTests {
|
|
|
|
|
public static class TestConfiguration {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
@ManagedResource
|
|
|
|
|
protected static class ManagedEndpoint extends AbstractEndpoint<Boolean> {
|
|
|
|
|
|
|
|
|
|
public ManagedEndpoint() {
|
|
|
|
|
super("managed", true, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean invoke() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@ManagedResource
|
|
|
|
|
protected static class NestedInManagedEndpoint {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public Endpoint<Boolean> nested() {
|
|
|
|
|
return new Nested();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Nested extends AbstractEndpoint<Boolean> {
|
|
|
|
|
|
|
|
|
|
public Nested() {
|
|
|
|
|
super("managed", true, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean invoke() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|