@EnableMBeanExport supports placeholders for its attributes now

Issue: SPR-11105
This commit is contained in:
Juergen Hoeller 2013-11-22 23:18:47 +01:00
parent ad402dcb4a
commit 009e362709
2 changed files with 73 additions and 13 deletions

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -17,15 +17,15 @@
package org.springframework.context.annotation;
import java.util.Map;
import javax.management.MBeanServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.jmx.export.annotation.AnnotationMBeanExporter;
import org.springframework.jmx.support.RegistrationPolicy;
@ -38,8 +38,8 @@ import org.springframework.util.StringUtils;
/**
* {@code @Configuration} class that registers a {@link AnnotationMBeanExporter} bean.
*
* <p>This configuration class is automatically imported when using the @{@link
* EnableMBeanExport} annotation. See its Javadoc for complete usage details.
* <p>This configuration class is automatically imported when using the
* @{@link EnableMBeanExport} annotation. See its javadoc for complete usage details.
*
* @author Phillip Webb
* @author Chris Beams
@ -47,12 +47,14 @@ import org.springframework.util.StringUtils;
* @see EnableMBeanExport
*/
@Configuration
public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
public class MBeanExportConfiguration implements ImportAware, EnvironmentAware, BeanFactoryAware {
private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter";
private AnnotationAttributes attributes;
private Environment environment;
private BeanFactory beanFactory;
@ -60,15 +62,21 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> map = importMetadata.getAnnotationAttributes(EnableMBeanExport.class.getName());
this.attributes = AnnotationAttributes.fromMap(map);
Assert.notNull(this.attributes, "@EnableMBeanExport is not present on " +
"importing class " + importMetadata.getClassName());
Assert.notNull(this.attributes,
"@EnableMBeanExport is not present on importing class " + importMetadata.getClassName());
}
@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;
}
@Bean(name=MBEAN_EXPORTER_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AnnotationMBeanExporter mbeanExporter() {
@ -82,6 +90,9 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
private void setupDomain(AnnotationMBeanExporter exporter) {
String defaultDomain = this.attributes.getString("defaultDomain");
if (StringUtils.hasText(defaultDomain)) {
if (this.environment != null) {
defaultDomain = this.environment.resolvePlaceholders(defaultDomain);
}
exporter.setDefaultDomain(defaultDomain);
}
}
@ -89,11 +100,14 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
private void setupServer(AnnotationMBeanExporter exporter) {
String server = this.attributes.getString("server");
if (StringUtils.hasText(server)) {
if (this.environment != null) {
server = this.environment.resolvePlaceholders(server);
}
exporter.setServer(this.beanFactory.getBean(server, MBeanServer.class));
}
else {
SpecificPlatform specificPlatform = SpecificPlatform.get();
if(specificPlatform != null) {
if (specificPlatform != null) {
exporter.setServer(specificPlatform.getMBeanServer());
}
}
@ -135,7 +149,8 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
server = getMBeanServerFactory().getObject();
Assert.isInstanceOf(MBeanServer.class, server);
return (MBeanServer) server;
} catch (Exception ex) {
}
catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@ -145,11 +160,12 @@ public class MBeanExportConfiguration implements ImportAware, BeanFactoryAware {
public static SpecificPlatform get() {
ClassLoader classLoader = MBeanExportConfiguration.class.getClassLoader();
for (SpecificPlatform environment : values()) {
if(ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
if (ClassUtils.isPresent(environment.identifyingClass, classLoader)) {
return environment;
}
}
return null;
}
}
}

View File

@ -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");
* 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.ObjectNameManager;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.mock.env.MockEnvironment;
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
public void testLazyAssembling() throws Exception {
System.setProperty("domain", "bean");
@ -110,6 +131,7 @@ public class EnableMBeanExportConfigurationTests {
}
}
@Configuration
@EnableMBeanExport(server = "server")
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
@EnableMBeanExport(server="server", registration=RegistrationPolicy.REPLACE_EXISTING)
static class LazyAssemblingConfiguration {
@ -178,6 +221,7 @@ public class EnableMBeanExportConfigurationTests {
}
}
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(value=Configuration.class))
@EnableMBeanExport(server = "server")