This commit is contained in:
Stephane Nicoll
2016-03-21 15:43:27 +01:00
parent 24f09e28c4
commit 2ae1435916
5 changed files with 149 additions and 16 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.actuate.info;
import org.springframework.boot.bind.PropertySourcesBinder;
import org.springframework.core.env.ConfigurableEnvironment;
/**

View File

@@ -20,8 +20,8 @@ import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import org.springframework.boot.bind.PropertySourcesBinder;
import org.springframework.boot.info.InfoProperties;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
@@ -84,9 +84,7 @@ public abstract class InfoPropertiesInfoContributor<T extends InfoProperties> im
* @return the raw content
*/
protected Map<String, Object> extractContent(PropertySource<?> propertySource) {
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(propertySource);
return new PropertySourcesBinder(propertySources).extractAll("");
return new PropertySourcesBinder(propertySource).extractAll("");
}
/**

View File

@@ -1,86 +0,0 @@
/*
* 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.actuate.info;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySources;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindException;
/**
* Helper extracting info from {@link PropertySources}.
*
* @author Stephane Nicoll
* @since 1.4.0
*/
public class PropertySourcesBinder {
private final PropertySources propertySources;
public PropertySourcesBinder(PropertySources propertySources) {
this.propertySources = propertySources;
}
public PropertySourcesBinder(ConfigurableEnvironment environment) {
this(environment.getPropertySources());
}
public final PropertySources getPropertySources() {
return this.propertySources;
}
/**
* Extract the keys using the specified {@code prefix}. The
* prefix won't be included.
* <p>
* Any key that starts with the {@code prefix} will be included
* @param prefix the prefix to use
* @return the keys matching the prefix
*/
public Map<String, Object> extractAll(String prefix) {
Map<String, Object> content = new LinkedHashMap<String, Object>();
bindTo(prefix, content);
return content;
}
/**
* Bind the specified {@code target} from the environment using the {@code prefix}.
* <p>
* Any key that starts with the {@code prefix} will be bound to the {@code target}.
* @param prefix the prefix to use
* @param target the object to bind to
*/
public void bindTo(String prefix, Object target) {
PropertiesConfigurationFactory<Object> factory = new PropertiesConfigurationFactory<Object>(
target);
if (StringUtils.hasText(prefix)) {
factory.setTargetName(prefix);
}
factory.setPropertySources(this.propertySources);
try {
factory.bindPropertiesToTarget();
}
catch (BindException ex) {
throw new IllegalStateException("Cannot bind to " + target, ex);
}
}
}

View File

@@ -49,12 +49,13 @@ import org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
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.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.validation.BindException;
@@ -262,18 +263,15 @@ public class EndpointAutoConfigurationTests {
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 {
this.content = new LinkedHashMap<String, Object>();
if (location.exists()) {
PropertiesConfigurationFactory<Map<String, Object>> factory = new PropertiesConfigurationFactory<Map<String, Object>>(
this.content);
factory.setTargetName("git");
Properties gitInfoProperties = PropertiesLoaderUtils
.loadProperties(location);
factory.setProperties(gitInfoProperties);
factory.bindPropertiesToTarget();
PropertiesPropertySource gitPropertySource =
new PropertiesPropertySource("git", gitInfoProperties);
this.content = new PropertySourcesBinder(gitPropertySource).extractAll("git");
}
}