Allows /configprops endpoint to show nested properties

Including maps and lists. Beans with no metadata (in
/META-INF/*spring-configuration-metadata.json) are just serialized
as they come (so might have problems like cycles). Serialization
errors are caught and rendered as an "error" for that bean. Any
problems can be fixed by preparing metadata and specifying which
properties are to be rendered that way.

Fixes gh-1746, fixes gh-1921
pull/1958/head
Dave Syer 10 years ago
parent fa64f432b7
commit 6ad626de49

2
.gitignore vendored

@ -9,6 +9,7 @@
.project
.settings
.metadata
.factorypath
bin
build
lib/
@ -29,3 +30,4 @@ overridedb.*
*.iws
.idea
*.jar
.DS_Store

@ -16,21 +16,32 @@
package org.springframework.boot.actuate.endpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.BeansException;
import org.springframework.boot.context.properties.ConfigurationBeanFactoryMetaData;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
@ -64,12 +75,17 @@ public class ConfigurationPropertiesReportEndpoint extends
private static final String CGLIB_FILTER_ID = "cglibFilter";
private static final Log logger = LogFactory
.getLog(ConfigurationPropertiesReportEndpoint.class);
private final Sanitizer sanitizer = new Sanitizer();
private ApplicationContext context;
private ConfigurationBeanFactoryMetaData beanFactoryMetaData;
private ConfigurationPropertiesMetaData metadata = new ConfigurationPropertiesMetaData();
public ConfigurationPropertiesReportEndpoint() {
super("configprops");
}
@ -97,7 +113,6 @@ public class ConfigurationPropertiesReportEndpoint extends
* Extract beans annotated {@link ConfigurationProperties} and serialize into
* {@link Map}.
*/
@SuppressWarnings("unchecked")
protected Map<String, Object> extract(ApplicationContext context) {
Map<String, Object> result = new HashMap<String, Object>();
@ -116,8 +131,9 @@ public class ConfigurationPropertiesReportEndpoint extends
String beanName = entry.getKey();
Object bean = entry.getValue();
Map<String, Object> root = new HashMap<String, Object>();
root.put("prefix", extractPrefix(context, beanName, bean));
root.put("properties", sanitize(mapper.convertValue(bean, Map.class)));
String prefix = extractPrefix(context, beanName, bean);
root.put("prefix", prefix);
root.put("properties", sanitize(safeSerialize(mapper, bean, prefix)));
result.put(beanName, root);
}
@ -128,12 +144,31 @@ public class ConfigurationPropertiesReportEndpoint extends
return result;
}
/**
* Cautiously serialize the bean to a map (returning a map with an error message
* instead of throwing an exception if there is a problem).
*/
private Map<String, Object> safeSerialize(ObjectMapper mapper, Object bean,
String prefix) {
try {
@SuppressWarnings("unchecked")
Map<String, Object> result = new HashMap<String, Object>(mapper.convertValue(
this.metadata.extractMap(bean, prefix), Map.class));
return result;
}
catch (Exception e) {
return new HashMap<String, Object>(Collections.<String, Object> singletonMap(
"error", "Cannot serialize '" + prefix + "'"));
}
}
/**
* Configure Jackson's {@link ObjectMapper} to be used to serialize the
* {@link ConfigurationProperties} objects into a {@link Map} structure.
*/
protected void configureObjectMapper(ObjectMapper mapper) {
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
applyCglibFilters(mapper);
applySerializationModifier(mapper);
}
@ -148,7 +183,7 @@ public class ConfigurationPropertiesReportEndpoint extends
}
/**
* Configure PropertyFiler to make sure Jackson doesn't process CGLIB generated bean
* Configure PropertyFilter to make sure Jackson doesn't process CGLIB generated bean
* properties.
*/
private void applyCglibFilters(ObjectMapper mapper) {
@ -239,23 +274,180 @@ public class ConfigurationPropertiesReportEndpoint extends
protected static class GenericSerializerModifier extends BeanSerializerModifier {
private ConversionService conversionService = new DefaultConversionService();
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
List<BeanPropertyWriter> result = new ArrayList<BeanPropertyWriter>();
for (BeanPropertyWriter writer : beanProperties) {
boolean readable = isReadable(beanDesc, writer);
if (readable) {
result.add(writer);
}
}
return result;
}
private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer) {
String parenType = beanDesc.getType().getTypeName();
String type = writer.getType().getTypeName();
AnnotatedMethod setter = beanDesc.findMethod(
"set" + StringUtils.capitalize(writer.getName()),
new Class<?>[] { writer.getPropertyType() });
if (setter != null
&& this.conversionService.canConvert(String.class,
writer.getPropertyType())) {
result.add(writer);
// If there's a setter, we assume it's OK to report on the value,
// similarly, if there's no setter but the package names match, we assume
// that its a nested class used solely for binding to config props, so it
// should be kosher. This filter is not used if there is JSON metadata for
// the property, so it's mainly for user-defined beans.
boolean readable = setter != null
|| ClassUtils.getPackageName(parenType).equals(
ClassUtils.getPackageName(type));
return readable;
}
}
return result;
/**
* Convenience class for grabbing and caching valid property names from
* /META-INF/spring-configuration-metadata.json so that metadata that is known to be
* valid can be used to pull the correct nested properties out of beans that might
* otherwise be tricky (contain cycles or other unserializable properties).
*/
protected static class ConfigurationPropertiesMetaData {
private Map<String, Set<String>> matched = new HashMap<String, Set<String>>();
private Set<String> keys = null;
public boolean matches(String prefix) {
if (this.matched.containsKey(prefix)) {
return matchesInternal(prefix);
}
synchronized (this.matched) {
if (this.matched.containsKey(prefix)) {
return matchesInternal(prefix);
}
this.matched.put(prefix, findKeys(prefix));
}
return matchesInternal(prefix);
}
private boolean matchesInternal(String prefix) {
if (this.matched.get(prefix) != null) {
return true;
}
else {
return false;
}
}
private Set<String> findKeys(String prefix) {
HashSet<String> set = new HashSet<String>();
try {
if (this.keys == null) {
this.keys = new HashSet<String>();
ObjectMapper mapper = new ObjectMapper();
Resource[] resources = new PathMatchingResourcePatternResolver()
.getResources("classpath*:/META-INF/*spring-configuration-metadata.json");
for (Resource resource : resources) {
addKeys(mapper, resource);
}
}
}
catch (IOException e) {
logger.warn("Could not deserialize config properties metadata", e);
}
for (String key : this.keys) {
if (key.length() > prefix.length()
&& key.startsWith(prefix)
&& ".".equals(key.substring(prefix.length(), prefix.length() + 1))) {
set.add(key.substring(prefix.length() + 1));
}
}
if (set.isEmpty()) {
return null;
}
return set;
}
private void addKeys(ObjectMapper mapper, Resource resource) throws IOException,
JsonParseException, JsonMappingException {
@SuppressWarnings("unchecked")
Map<String, Object> map = mapper.readValue(resource.getInputStream(),
Map.class);
@SuppressWarnings("unchecked")
Collection<Map<String, Object>> metadata = (Collection<Map<String, Object>>) map
.get("properties");
for (Map<String, Object> value : metadata) {
try {
if (value.containsKey("type")) {
this.keys.add((String) value.get("name"));
}
}
catch (Exception e) {
logger.warn("Could not parse config properties metadata", e);
}
}
}
public Object extractMap(Object bean, String prefix) {
if (!matches(prefix)) {
return bean;
}
Map<String, Object> map = new HashMap<String, Object>();
for (String key : this.matched.get(prefix)) {
addProperty(bean, key, map);
}
return map;
}
private void addProperty(Object bean, String key, Map<String, Object> map) {
String prefix = key.contains(".") ? StringUtils.split(key, ".")[0] : key;
String suffix = key.length() > prefix.length() ? key.substring(prefix
.length() + 1) : null;
String property = prefix;
if (bean instanceof Map) {
@SuppressWarnings("unchecked")
Map<String, Object> value = (Map<String, Object>) bean;
bean = new MapHolder(value);
property = "map[" + property + "]";
}
BeanWrapperImpl wrapper = new BeanWrapperImpl(bean);
try {
Object value = wrapper.getPropertyValue(property);
if (value instanceof Map) {
Map<String, Object> nested = new HashMap<String, Object>();
map.put(prefix, nested);
if (suffix != null) {
addProperty(value, suffix, nested);
}
}
else {
map.put(prefix, value);
}
}
catch (Exception e) {
// Probably just lives on a different bean (it happens)
logger.debug("Could not parse config properties metadata '" + key + "': "
+ e.getMessage());
}
}
protected static class MapHolder {
Map<String, Object> map = new HashMap<String, Object>();
public MapHolder(Map<String, Object> bean) {
this.map.putAll(bean);
}
public Map<String, Object> getMap() {
return this.map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
}

@ -16,6 +16,8 @@
package org.springframework.boot.actuate.endpoint;
import java.net.InetAddress;
import java.util.List;
import java.util.Map;
import org.junit.After;
@ -28,6 +30,7 @@ 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.context.annotation.Import;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -51,7 +54,46 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
@Test
@SuppressWarnings("unchecked")
public void testNaming() throws Exception {
this.context.register(Config.class);
this.context.register(FooConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
assertEquals("foo", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(2, map.size());
assertEquals("foo", map.get("name"));
}
@Test
@SuppressWarnings("unchecked")
public void testNestedNaming() throws Exception {
this.context.register(FooConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.bar.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(2, map.size());
assertEquals("foo", ((Map<String, Object>) map.get("bar")).get("name"));
}
@Test
@SuppressWarnings("unchecked")
public void testCycle() throws Exception {
this.context.register(CycleConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
@ -65,12 +107,133 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
.get("properties");
assertNotNull(map);
assertEquals(1, map.size());
assertEquals("Cannot serialize 'foo'", map.get("error"));
}
@Test
@SuppressWarnings("unchecked")
public void testCycleAvoidedThroughManualMetadata() throws Exception {
this.context.register(MetadataCycleConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "bar.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
assertEquals("bar", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(1, map.size());
assertEquals("foo", map.get("name"));
}
@Test
@SuppressWarnings("unchecked")
public void testMap() throws Exception {
this.context.register(MapConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.map.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
assertEquals("foo", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(3, map.size());
assertEquals("foo", ((Map<String, Object>) map.get("map")).get("name"));
}
@Test
@SuppressWarnings("unchecked")
public void testEmptyMapIsNotAdded() throws Exception {
this.context.register(MapConfig.class);
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
System.err.println(nestedProperties);
assertEquals("foo", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(2, map.size());
assertEquals(null, (map.get("map")));
}
@Test
@SuppressWarnings("unchecked")
public void testList() throws Exception {
this.context.register(ListConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.list[0]:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
assertEquals("foo", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(3, map.size());
assertEquals("foo", ((List<String>) map.get("list")).get(0));
}
@Test
@SuppressWarnings("unchecked")
public void testMapWithMetadata() throws Exception {
this.context.register(MetadataMapConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "spam.map.name:foo");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
assertEquals("spam", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(2, map.size());
assertEquals("foo", ((Map<String, Object>) map.get("map")).get("name"));
}
@Test
@SuppressWarnings("unchecked")
public void testInetAddress() throws Exception {
this.context.register(AddressedConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, "foo.address:192.168.1.10");
this.context.refresh();
ConfigurationPropertiesReportEndpoint report = this.context
.getBean(ConfigurationPropertiesReportEndpoint.class);
Map<String, Object> properties = report.invoke();
Map<String, Object> nestedProperties = (Map<String, Object>) properties
.get("foo");
assertNotNull(nestedProperties);
System.err.println(nestedProperties);
assertEquals("foo", nestedProperties.get("prefix"));
Map<String, Object> map = (Map<String, Object>) nestedProperties
.get("properties");
assertNotNull(map);
assertEquals(3, map.size());
assertEquals("192.168.1.10", map.get("address"));
}
@Configuration
@EnableConfigurationProperties
public static class Config {
public static class Base {
@Bean
public ConfigurationPropertiesReportEndpoint endpoint(
@ -80,6 +243,12 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
return endpoint;
}
}
@Configuration
@Import(Base.class)
public static class FooConfig {
@Bean
@ConfigurationProperties(prefix = "foo")
public Foo foo() {
@ -88,10 +257,104 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
}
@Configuration
@Import(Base.class)
public static class CycleConfig {
@Bean
@ConfigurationProperties(prefix = "foo")
public Cycle foo() {
return new Cycle();
}
}
@Configuration
@Import(Base.class)
public static class MetadataCycleConfig {
@Bean
@ConfigurationProperties(prefix = "bar")
public Cycle foo() {
return new Cycle();
}
}
@Configuration
@Import(Base.class)
public static class MapConfig {
@Bean
@ConfigurationProperties(prefix = "foo")
public MapHolder foo() {
return new MapHolder();
}
}
@Configuration
@Import(Base.class)
public static class ListConfig {
@Bean
@ConfigurationProperties(prefix = "foo")
public ListHolder foo() {
return new ListHolder();
}
}
@Configuration
@Import(Base.class)
public static class MetadataMapConfig {
@Bean
@ConfigurationProperties(prefix = "spam")
public MapHolder foo() {
return new MapHolder();
}
}
@Configuration
@Import(Base.class)
public static class AddressedConfig {
@Bean
@ConfigurationProperties(prefix = "foo")
public Addressed foo() {
return new Addressed();
}
}
public static class Foo {
private String name = "654321";
public static class Bar {
private String name = "123456";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
private Bar bar = new Bar();
public Bar getBar() {
return this.bar;
}
public void setBar(Bar bar) {
this.bar = bar;
}
public String getName() {
return this.name;
}
@ -107,4 +370,61 @@ public class ConfigurationPropertiesReportEndpointSerializationTests {
}
public static class Cycle extends Foo {
private Foo self;
public Cycle() {
this.self = this;
}
public Foo getSelf() {
return this.self;
}
public void setSelf(Foo self) {
this.self = self;
}
}
public static class MapHolder extends Foo {
private Map<String, Object> map;
public Map<String, Object> getMap() {
return this.map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
}
public static class ListHolder extends Foo {
private List<String> list;
public List<String> getList() {
return this.list;
}
public void setList(List<String> list) {
this.list = list;
}
}
public static class Addressed extends Foo {
private InetAddress address;
public InetAddress getAddress() {
return this.address;
}
public void setAddress(InetAddress address) {
this.address = address;
}
}
}

@ -0,0 +1,10 @@
{"properties": [
{
"name": "bar.name",
"dataType": "java.lang.String"
},
{
"name": "spam.map.name",
"dataType": "java.lang.String"
} ]}

@ -221,6 +221,18 @@ public class SampleActuatorApplicationTests {
((String) body.get("context")).startsWith("application"));
}
@Test
public void testConfigProps() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = new TestRestTemplate("user", getPassword())
.getForEntity("http://localhost:" + this.port + "/configprops", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body,
body.containsKey("spring.datasource.CONFIGURATION_PROPERTIES"));
}
private String getPassword() {
return this.security.getUser().getPassword();
}

@ -17,7 +17,7 @@
<url>http://www.spring.io</url>
</organization>
<properties>
<java.version>1.6</java.version>
<java.version>1.7</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't clash with Spring ${} placeholders -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

Loading…
Cancel
Save