From 3378ede2318ac26434cb9630b9c2c0e49d76a4a4 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 16 Jun 2014 14:45:06 +0100 Subject: [PATCH] Ensure CompositePropertySources are listed in EnvironmentEndpoint --- .../actuate/endpoint/EnvironmentEndpoint.java | 49 +++++++++++++++++-- .../endpoint/EnvironmentEndpointTests.java | 17 +++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java index 7de906075b..502bcba6f9 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java @@ -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> i public Map invoke() { Map result = new LinkedHashMap(); result.put("profiles", this.environment.getActiveProfiles()); - for (PropertySource source : getPropertySources()) { + for (Entry> entry : getPropertySources().entrySet()) { + PropertySource source = entry.getValue(); + String sourceName = entry.getKey(); if (source instanceof EnumerablePropertySource) { EnumerablePropertySource enumerable = (EnumerablePropertySource) source; Map map = new LinkedHashMap(); 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> getPropertySources() { + private Map> getPropertySources() { + Map> map = new LinkedHashMap>(); + 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> map, + PropertySource source) { + if (source instanceof CompositePropertySource) { + try { + Field field = ReflectionUtils.findField(CompositePropertySource.class, + "propertySources"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + Set> nested = (Set>) 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) { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java index ccb0b0a04d..172e19ff98 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java @@ -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 env = report.invoke(); + assertEquals("bar", ((Map) env.get("composite:one")).get("foo")); + } + @SuppressWarnings("unchecked") @Test public void testKeySanitization() throws Exception {