Ensure CompositePropertySources are listed in EnvironmentEndpoint

This commit is contained in:
Dave Syer 2014-06-16 14:45:06 +01:00
parent 8b03834d79
commit 3378ede231
2 changed files with 61 additions and 5 deletions

View File

@ -16,17 +16,23 @@
package org.springframework.boot.actuate.endpoint;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* {@link Endpoint} to expose {@link ConfigurableEnvironment environment} information.
@ -59,25 +65,58 @@ public class EnvironmentEndpoint extends AbstractEndpoint<Map<String, Object>> i
public Map<String, Object> invoke() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("profiles", this.environment.getActiveProfiles());
for (PropertySource<?> source : getPropertySources()) {
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
String sourceName = entry.getKey();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (String name : enumerable.getPropertyNames()) {
map.put(name, sanitize(name, enumerable.getProperty(name)));
}
result.put(source.getName(), map);
result.put(sourceName, map);
}
}
return result;
}
private Iterable<PropertySource<?>> getPropertySources() {
private Map<String, PropertySource<?>> getPropertySources() {
Map<String, PropertySource<?>> map = new LinkedHashMap<String, PropertySource<?>>();
MutablePropertySources sources = null;
if (this.environment != null
&& this.environment instanceof ConfigurableEnvironment) {
return ((ConfigurableEnvironment) this.environment).getPropertySources();
sources = ((ConfigurableEnvironment) this.environment).getPropertySources();
}
else {
sources = new StandardEnvironment().getPropertySources();
}
for (PropertySource<?> source : sources) {
extract("", map, source);
}
return map;
}
private void extract(String root, Map<String, PropertySource<?>> map,
PropertySource<?> source) {
if (source instanceof CompositePropertySource) {
try {
Field field = ReflectionUtils.findField(CompositePropertySource.class,
"propertySources");
field.setAccessible(true);
@SuppressWarnings("unchecked")
Set<PropertySource<?>> nested = (Set<PropertySource<?>>) field
.get(source);
for (PropertySource<?> nest : nested) {
extract(source.getName() + ":", map, nest);
}
}
catch (Exception e) {
// ignore
}
}
else {
map.put(root + source.getName(), source);
}
return new StandardEnvironment().getPropertySources();
}
public Object sanitize(String name, Object object) {

View File

@ -16,12 +16,15 @@
package org.springframework.boot.actuate.endpoint;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals;
@ -44,6 +47,20 @@ public class EnvironmentEndpointTests extends AbstractEndpointTests<EnvironmentE
assertThat(getEndpointBean().invoke().size(), greaterThan(0));
}
@SuppressWarnings("unchecked")
@Test
public void testCompositeSource() throws Exception {
EnvironmentEndpoint report = getEndpointBean();
CompositePropertySource source = new CompositePropertySource("composite");
source.addPropertySource(new MapPropertySource("one", Collections.singletonMap(
"foo", (Object) "bar")));
source.addPropertySource(new MapPropertySource("two", Collections.singletonMap(
"foo", (Object) "spam")));
this.context.getEnvironment().getPropertySources().addFirst(source);
Map<String, Object> env = report.invoke();
assertEquals("bar", ((Map<String, Object>) env.get("composite:one")).get("foo"));
}
@SuppressWarnings("unchecked")
@Test
public void testKeySanitization() throws Exception {