diff --git a/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java index 58fc2b0844e..3ab6b27e331 100644 --- a/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/cloudfoundry/VcapApplicationListener.java @@ -193,19 +193,27 @@ public class VcapApplicationListener implements } } + @SuppressWarnings("unchecked") private void flatten(Properties properties, Map input, String path) { for (Entry entry : input.entrySet()) { - String key = entry.getKey(); - if (StringUtils.hasText(path)) { - if (key.startsWith("[")) { - key = path + key; - } - else { - key = path + "." + key; + String key = getFullKey(path, entry.getKey()); + Object value = entry.getValue(); + if (value instanceof Map) { + // Need a compound key + flatten(properties, (Map) value, key); + } + else if (value instanceof Collection) { + // Need a compound key + Collection collection = (Collection) value; + properties.put(key, + StringUtils.collectionToCommaDelimitedString(collection)); + int count = 0; + for (Object item : collection) { + String itemKey = "[" + (count++) + "]"; + flatten(properties, Collections.singletonMap(itemKey, item), key); } } - Object value = entry.getValue(); - if (value instanceof String) { + else if (value instanceof String) { properties.put(key, value); } else if (value instanceof Number) { @@ -214,28 +222,20 @@ public class VcapApplicationListener implements else if (value instanceof Boolean) { properties.put(key, value.toString()); } - else if (value instanceof Map) { - // Need a compound key - @SuppressWarnings("unchecked") - Map map = (Map) value; - flatten(properties, map, key); - } - else if (value instanceof Collection) { - // Need a compound key - @SuppressWarnings("unchecked") - Collection collection = (Collection) value; - properties.put(key, - StringUtils.collectionToCommaDelimitedString(collection)); - int count = 0; - for (Object object : collection) { - flatten(properties, - Collections.singletonMap("[" + (count++) + "]", object), key); - } - } else { properties.put(key, value == null ? "" : value); } } } + private String getFullKey(String path, String key) { + if (!StringUtils.hasText(path)) { + return key; + } + if (key.startsWith("[")) { + return path + key; + } + return path + "." + key; + } + }