Migrate JolokiaAutoConfiguration to use ConfigurationProperties instead of accessing the environment directly to obtain configuration properties

This commit is contained in:
Christian Dupuis 2014-01-09 17:52:55 +01:00
parent 51f9189923
commit b8b948d366
3 changed files with 30 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 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.
@ -38,8 +38,8 @@ import org.springframework.util.StringUtils;
* @author Christian Dupuis * @author Christian Dupuis
*/ */
@Configuration @Configuration
@ConditionalOnExpression("${endpoints.jmx.enabled:true} && ${spring.jmx.enabled:true}")
@AutoConfigureAfter({ EndpointAutoConfiguration.class }) @AutoConfigureAfter({ EndpointAutoConfiguration.class })
@ConditionalOnExpression("${endpoints.jmx.enabled:true}&&${spring.jmx.enabled:true}")
@EnableConfigurationProperties(EndpointMBeanExportProperties.class) @EnableConfigurationProperties(EndpointMBeanExportProperties.class)
public class EndpointMBeanExportAutoConfiguration { public class EndpointMBeanExportAutoConfiguration {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 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.
@ -16,11 +16,13 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.jolokia.http.AgentServlet; import org.jolokia.http.AgentServlet;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.JolokiaAutoConfiguration.JolokiaProperties;
import org.springframework.boot.actuate.endpoint.mvc.JolokiaMvcEndpoint; import org.springframework.boot.actuate.endpoint.mvc.JolokiaMvcEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
@ -30,10 +32,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for embedding Jolokia, a JMX-HTTP * {@link EnableAutoConfiguration Auto-configuration} for embedding Jolokia, a JMX-HTTP
@ -56,17 +58,14 @@ import org.springframework.core.env.Environment;
@Configuration @Configuration
@ConditionalOnWebApplication @ConditionalOnWebApplication
@ConditionalOnClass({ AgentServlet.class }) @ConditionalOnClass({ AgentServlet.class })
@ConditionalOnExpression("${endpoints.jolokia.enabled:true}")
@AutoConfigureBefore(ManagementSecurityAutoConfiguration.class) @AutoConfigureBefore(ManagementSecurityAutoConfiguration.class)
@AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class) @AutoConfigureAfter(EmbeddedServletContainerAutoConfiguration.class)
@ConditionalOnExpression("${endpoints.jolokia.enabled:true}") @EnableConfigurationProperties(JolokiaProperties.class)
public class JolokiaAutoConfiguration { public class JolokiaAutoConfiguration {
private RelaxedPropertyResolver environment;
@Autowired @Autowired
public void setEnvironment(Environment environment) { JolokiaProperties properties = new JolokiaProperties();
this.environment = new RelaxedPropertyResolver(environment);
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ -77,14 +76,23 @@ public class JolokiaAutoConfiguration {
} }
private Properties getInitParameters() { private Properties getInitParameters() {
Properties properties = new Properties(); Properties initParameters = new Properties();
Map<String, Object> configParameters = this.environment initParameters.putAll(this.properties.getConfig());
.getSubProperties("jolokia.config."); return initParameters;
for (Map.Entry<String, Object> configParameter : configParameters.entrySet()) { }
properties.setProperty(configParameter.getKey(), configParameter.getValue()
.toString()); @ConfigurationProperties(name = "jolokia")
public static class JolokiaProperties {
private Map<String, String> config = new HashMap<String, String>();
public Map<String, String> getConfig() {
return this.config;
}
public void setConfig(Map<String, String> config) {
this.config = config;
} }
return properties;
} }
} }

View File

@ -54,6 +54,8 @@ public class JolokiaAutoConfigurationTests {
@Test @Test
public void agentServletRegisteredWithAppContext() throws Exception { public void agentServletRegisteredWithAppContext() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "jolokia.config[key1]:value1",
"jolokia.config[key2]:value2");
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
@ -65,7 +67,8 @@ public class JolokiaAutoConfigurationTests {
@Test @Test
public void agentDisabled() throws Exception { public void agentDisabled() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "endpoints.jolokia.enabled:false"); EnvironmentTestUtils.addEnvironment(this.context,
"endpoints.jolokia.enabled:false");
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,