Commit 2ae14359 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish

parent 24f09e28
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.info; package org.springframework.boot.actuate.info;
import org.springframework.boot.bind.PropertySourcesBinder;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
/** /**
......
...@@ -20,8 +20,8 @@ import java.util.Collections; ...@@ -20,8 +20,8 @@ import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.springframework.boot.bind.PropertySourcesBinder;
import org.springframework.boot.info.InfoProperties; import org.springframework.boot.info.InfoProperties;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -84,9 +84,7 @@ public abstract class InfoPropertiesInfoContributor<T extends InfoProperties> im ...@@ -84,9 +84,7 @@ public abstract class InfoPropertiesInfoContributor<T extends InfoProperties> im
* @return the raw content * @return the raw content
*/ */
protected Map<String, Object> extractContent(PropertySource<?> propertySource) { protected Map<String, Object> extractContent(PropertySource<?> propertySource) {
MutablePropertySources propertySources = new MutablePropertySources(); return new PropertySourcesBinder(propertySource).extractAll("");
propertySources.addFirst(propertySource);
return new PropertySourcesBinder(propertySources).extractAll("");
} }
/** /**
......
...@@ -49,12 +49,13 @@ import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration; ...@@ -49,12 +49,13 @@ import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties; import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
import org.springframework.boot.bind.PropertiesConfigurationFactory; import org.springframework.boot.bind.PropertySourcesBinder;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
...@@ -262,18 +263,15 @@ public class EndpointAutoConfigurationTests { ...@@ -262,18 +263,15 @@ public class EndpointAutoConfigurationTests {
private static class GitFullInfoContributor implements InfoContributor { private static class GitFullInfoContributor implements InfoContributor {
private final Map<String, Object> content; private Map<String, Object> content = new LinkedHashMap<String, Object>();
GitFullInfoContributor(Resource location) throws BindException, IOException { GitFullInfoContributor(Resource location) throws BindException, IOException {
this.content = new LinkedHashMap<String, Object>();
if (location.exists()) { if (location.exists()) {
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
this.content);
factory.setTargetName("git");
Properties gitInfoProperties = PropertiesLoaderUtils Properties gitInfoProperties = PropertiesLoaderUtils
.loadProperties(location); .loadProperties(location);
factory.setProperties(gitInfoProperties); PropertiesPropertySource gitPropertySource =
factory.bindPropertiesToTarget(); new PropertiesPropertySource("git", gitInfoProperties);
this.content = new PropertySourcesBinder(gitPropertySource).extractAll("git");
} }
} }
......
...@@ -14,13 +14,16 @@ ...@@ -14,13 +14,16 @@
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.boot.actuate.info; package org.springframework.boot.bind;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import org.springframework.boot.bind.PropertiesConfigurationFactory; import org.springframework.core.convert.ConversionService;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources; import org.springframework.core.env.PropertySources;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.BindException; import org.springframework.validation.BindException;
...@@ -33,20 +36,50 @@ import org.springframework.validation.BindException; ...@@ -33,20 +36,50 @@ import org.springframework.validation.BindException;
*/ */
public class PropertySourcesBinder { public class PropertySourcesBinder {
private final PropertySources propertySources; private PropertySources propertySources;
private ConversionService conversionService;
/**
* Create a new instance.
* @param propertySources the {@link PropertySources} to use
*/
public PropertySourcesBinder(PropertySources propertySources) { public PropertySourcesBinder(PropertySources propertySources) {
this.propertySources = propertySources; this.propertySources = propertySources;
} }
/**
* Create a new instance from a single {@link PropertySource}.
* @param propertySource the {@link PropertySource} to use
*/
public PropertySourcesBinder(PropertySource<?> propertySource) {
this(createPropertySources(propertySource));
}
/**
* Create a new instance using the {@link Environment} as the property sources.
* @param environment the environment
*/
public PropertySourcesBinder(ConfigurableEnvironment environment) { public PropertySourcesBinder(ConfigurableEnvironment environment) {
this(environment.getPropertySources()); this(environment.getPropertySources());
} }
public final PropertySources getPropertySources() { public void setPropertySources(PropertySources propertySources) {
this.propertySources = propertySources;
}
public PropertySources getPropertySources() {
return this.propertySources; return this.propertySources;
} }
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
public ConversionService getConversionService() {
return this.conversionService;
}
/** /**
* Extract the keys using the specified {@code prefix}. The * Extract the keys using the specified {@code prefix}. The
* prefix won't be included. * prefix won't be included.
...@@ -74,6 +107,9 @@ public class PropertySourcesBinder { ...@@ -74,6 +107,9 @@ public class PropertySourcesBinder {
if (StringUtils.hasText(prefix)) { if (StringUtils.hasText(prefix)) {
factory.setTargetName(prefix); factory.setTargetName(prefix);
} }
if (this.conversionService != null) {
factory.setConversionService(this.conversionService);
}
factory.setPropertySources(this.propertySources); factory.setPropertySources(this.propertySources);
try { try {
factory.bindPropertiesToTarget(); factory.bindPropertiesToTarget();
...@@ -83,4 +119,10 @@ public class PropertySourcesBinder { ...@@ -83,4 +119,10 @@ public class PropertySourcesBinder {
} }
} }
private static PropertySources createPropertySources(PropertySource<?> propertySource) {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(propertySource);
return propertySources;
}
} }
/*
* Copyright 2012-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.bind;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.testutil.EnvironmentTestUtils;
import org.springframework.core.env.StandardEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link PropertySourcesBinder}.
*
* @author Stephane Nicoll
*/
public class PropertySourcesBinderTests {
private StandardEnvironment env = new StandardEnvironment();
@Test
public void extractAllWithPrefix() {
EnvironmentTestUtils.addEnvironment(this.env, "foo.first=1", "foo.second=2");
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("foo");
assertThat(content.get("first")).isEqualTo("1");
assertThat(content.get("second")).isEqualTo("2");
assertThat(content).hasSize(2);
}
@Test
@SuppressWarnings("unchecked")
public void extractNoPrefix() {
EnvironmentTestUtils.addEnvironment(this.env, "foo.ctx.first=1", "foo.ctx.second=2");
Map<String, Object> content = new PropertySourcesBinder(this.env).extractAll("");
assertThat(content.get("foo")).isInstanceOf(Map.class);
Map<String, Object> foo = (Map<String, Object>) content.get("foo");
assertThat(content.get("foo")).isInstanceOf(Map.class);
Map<String, Object> ctx = (Map<String, Object>) foo.get("ctx");
assertThat(ctx.get("first")).isEqualTo("1");
assertThat(ctx.get("second")).isEqualTo("2");
assertThat(ctx).hasSize(2);
assertThat(foo).hasSize(1);
}
@Test
public void bindToSimplePojo() {
EnvironmentTestUtils.addEnvironment(this.env, "test.name=foo", "test.counter=42");
TestBean bean = new TestBean();
new PropertySourcesBinder(this.env).bindTo("test", bean);
assertThat(bean.getName()).isEqualTo("foo");
assertThat(bean.getCounter()).isEqualTo(42);
}
private static class TestBean {
private String name;
private Integer counter;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCounter() {
return this.counter;
}
public void setCounter(Integer counter) {
this.counter = counter;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment