@EnableMBeanExport supports placeholders for its attributes now
Issue: SPR-11105
This commit is contained in:
parent
ad402dcb4a
commit
009e362709
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -17,15 +17,15 @@
|
||||||
package org.springframework.context.annotation;
|
package org.springframework.context.annotation;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.management.MBeanServer;
|
import javax.management.MBeanServer;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
import org.springframework.beans.factory.FactoryBean;
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
import org.springframework.context.EnvironmentAware;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
|
||||||
import org.springframework.jmx.support.RegistrationPolicy;
|
import org.springframework.jmx.support.RegistrationPolicy;
|
||||||
|
|
@ -38,8 +38,8 @@ import org.springframework.util.StringUtils;
|
||||||
/**
|
/**
|
||||||
* {@code @Configuration} class that registers a {@link AnnotationMBeanExporter} bean.
|
* {@code @Configuration} class that registers a {@link AnnotationMBeanExporter} bean.
|
||||||
*
|
*
|
||||||
* <p>This configuration class is automatically imported when using the @{@link
|
* <p>This configuration class is automatically imported when using the
|
||||||
* EnableMBeanExport} annotation. See its Javadoc for complete usage details.
|
* @{@link EnableMBeanExport} annotation. See its javadoc for complete usage details.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
|
@ -47,12 +47,14 @@ import org.springframework.util.StringUtils;
|
||||||
* @see EnableMBeanExport
|
* @see EnableMBeanExport
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
public class MBeanExportConfiguration implements ImportAware, EnvironmentAware, BeanFactoryAware {
|
||||||
|
|
||||||
private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter";
|
private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter";
|
||||||
|
|
||||||
private AnnotationAttributes attributes;
|
private AnnotationAttributes attributes;
|
||||||
|
|
||||||
|
private Environment environment;
|
||||||
|
|
||||||
private BeanFactory beanFactory;
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -60,15 +62,21 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||||
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
public void setImportMetadata(AnnotationMetadata importMetadata) {
|
||||||
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
|
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
|
||||||
this.attributes = AnnotationAttributes.fromMap(map);
|
this.attributes = AnnotationAttributes.fromMap(map);
|
||||||
Assert.notNull(this.attributes, "@EnableMBeanExport is not present on " +
|
Assert.notNull(this.attributes,
|
||||||
"importing class " + importMetadata.getClassName());
|
"@EnableMBeanExport is not present on importing class " + importMetadata.getClassName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
public void setEnvironment(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) {
|
||||||
this.beanFactory = beanFactory;
|
this.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Bean(name=MBEAN_EXPORTER_BEAN_NAME)
|
@Bean(name=MBEAN_EXPORTER_BEAN_NAME)
|
||||||
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||||
public AnnotationMBeanExporter mbeanExporter() {
|
public AnnotationMBeanExporter mbeanExporter() {
|
||||||
|
|
@ -82,6 +90,9 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||||
private void setupDomain(AnnotationMBeanExporter exporter) {
|
private void setupDomain(AnnotationMBeanExporter exporter) {
|
||||||
String defaultDomain = this.attributes.getString("defaultDomain");
|
String defaultDomain = this.attributes.getString("defaultDomain");
|
||||||
if (StringUtils.hasText(defaultDomain)) {
|
if (StringUtils.hasText(defaultDomain)) {
|
||||||
|
if (this.environment != null) {
|
||||||
|
defaultDomain = this.environment.resolvePlaceholders(defaultDomain);
|
||||||
|
}
|
||||||
exporter.setDefaultDomain(defaultDomain);
|
exporter.setDefaultDomain(defaultDomain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -89,11 +100,14 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||||
private void setupServer(AnnotationMBeanExporter exporter) {
|
private void setupServer(AnnotationMBeanExporter exporter) {
|
||||||
String server = this.attributes.getString("server");
|
String server = this.attributes.getString("server");
|
||||||
if (StringUtils.hasText(server)) {
|
if (StringUtils.hasText(server)) {
|
||||||
|
if (this.environment != null) {
|
||||||
|
server = this.environment.resolvePlaceholders(server);
|
||||||
|
}
|
||||||
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
|
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
SpecificPlatform specificPlatform = SpecificPlatform.get();
|
SpecificPlatform specificPlatform = SpecificPlatform.get();
|
||||||
if(specificPlatform != null) {
|
if (specificPlatform != null) {
|
||||||
exporter.setServer(specificPlatform.getMBeanServer());
|
exporter.setServer(specificPlatform.getMBeanServer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +149,8 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||||
server = getMBeanServerFactory().getObject();
|
server = getMBeanServerFactory().getObject();
|
||||||
Assert.isInstanceOf(MBeanServer.class, server);
|
Assert.isInstanceOf(MBeanServer.class, server);
|
||||||
return (MBeanServer) server;
|
return (MBeanServer) server;
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
throw new IllegalStateException(ex);
|
throw new IllegalStateException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -145,11 +160,12 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
|
||||||
public static SpecificPlatform get() {
|
public static SpecificPlatform get() {
|
||||||
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
|
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
|
||||||
for (SpecificPlatform environment : values()) {
|
for (SpecificPlatform environment : values()) {
|
||||||
if(ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
|
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
|
||||||
return environment;
|
return environment;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2013 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -34,6 +34,7 @@ import org.springframework.jmx.export.TestDynamicMBean;
|
||||||
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
import org.springframework.jmx.support.MBeanServerFactoryBean;
|
||||||
import org.springframework.jmx.support.ObjectNameManager;
|
import org.springframework.jmx.support.ObjectNameManager;
|
||||||
import org.springframework.jmx.support.RegistrationPolicy;
|
import org.springframework.jmx.support.RegistrationPolicy;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
@ -61,6 +62,26 @@ public class EnableMBeanExportConfigurationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPlaceholderBased() throws Exception {
|
||||||
|
MockEnvironment env = new MockEnvironment();
|
||||||
|
env.setProperty("serverName", "server");
|
||||||
|
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||||
|
ctx.setEnvironment(env);
|
||||||
|
ctx.register(PlaceholderBasedConfiguration.class);
|
||||||
|
ctx.refresh();
|
||||||
|
try {
|
||||||
|
MBeanServer server = (MBeanServer) ctx.getBean("server");
|
||||||
|
ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4");
|
||||||
|
assertNotNull(server.getObjectInstance(oname));
|
||||||
|
String name = (String) server.getAttribute(oname, "Name");
|
||||||
|
assertEquals("Invalid name returned", "TEST", name);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
ctx.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLazyAssembling() throws Exception {
|
public void testLazyAssembling() throws Exception {
|
||||||
System.setProperty("domain", "bean");
|
System.setProperty("domain", "bean");
|
||||||
|
|
@ -110,6 +131,7 @@ public class EnableMBeanExportConfigurationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMBeanExport(server = "server")
|
@EnableMBeanExport(server = "server")
|
||||||
static class LazyNamingConfiguration {
|
static class LazyNamingConfiguration {
|
||||||
|
|
@ -129,6 +151,27 @@ public class EnableMBeanExportConfigurationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableMBeanExport(server = "${serverName}")
|
||||||
|
static class PlaceholderBasedConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MBeanServerFactoryBean server() throws Exception {
|
||||||
|
return new MBeanServerFactoryBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Lazy
|
||||||
|
public AnnotationTestBean testBean() {
|
||||||
|
AnnotationTestBean bean = new AnnotationTestBean();
|
||||||
|
bean.setName("TEST");
|
||||||
|
bean.setAge(100);
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMBeanExport(server="server", registration=RegistrationPolicy.REPLACE_EXISTING)
|
@EnableMBeanExport(server="server", registration=RegistrationPolicy.REPLACE_EXISTING)
|
||||||
static class LazyAssemblingConfiguration {
|
static class LazyAssemblingConfiguration {
|
||||||
|
|
@ -178,6 +221,7 @@ public class EnableMBeanExportConfigurationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(excludeFilters = @ComponentScan.Filter(value=Configuration.class))
|
@ComponentScan(excludeFilters = @ComponentScan.Filter(value=Configuration.class))
|
||||||
@EnableMBeanExport(server = "server")
|
@EnableMBeanExport(server = "server")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue