From 9fce630c8600833d564b191df381044b90cb09f5 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 28 Oct 2014 08:14:00 +0000 Subject: [PATCH] Extract lists in VCAP_APPLICATION (e.g. uris) Fixes gh-1773 --- .../cloudfoundry/VcapApplicationListener.java | 59 +++++++++---------- .../VcapApplicationListenerTests.java | 17 +++++- 2 files changed, 43 insertions(+), 33 deletions(-) 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 72cff54bfe..6704a7be59 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 @@ -18,7 +18,6 @@ package org.springframework.boot.cloudfoundry; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -71,7 +70,7 @@ import org.springframework.util.StringUtils; * vcap.application.version: 0138c4a6-2a73-416b-aca0-572c09f7ca53 * vcap.application.name: foo * vcap.application.uris[0]: foo.cfapps.io - * + * * vcap.services.mysql.name: mysql * vcap.services.mysql.label: rds-mysql-1.0 * vcap.services.mysql.credentials.name: d04fb13d27d964c62b267bbba1cffb9da @@ -151,19 +150,7 @@ public class VcapApplicationListener implements try { Map map = this.parser.parseMap(environment.getProperty( VCAP_APPLICATION, "{}")); - if (map != null) { - map = new LinkedHashMap(map); - for (String key : map.keySet()) { - Object value = map.get(key); - if (!(value instanceof String)) { - if (value == null) { - value = ""; - } - map.put(key, value.toString()); - } - } - properties.putAll(map); - } + extractPropertiesFromApplication(properties, map); } catch (Exception ex) { logger.error("Could not parse VCAP_APPLICATION", ex); @@ -176,21 +163,7 @@ public class VcapApplicationListener implements try { Map map = this.parser.parseMap(environment.getProperty( VCAP_SERVICES, "{}")); - if (map != null) { - for (Object services : map.values()) { - @SuppressWarnings("unchecked") - List list = (List) services; - for (Object object : list) { - @SuppressWarnings("unchecked") - Map service = (Map) object; - String key = (String) service.get("name"); - if (key == null) { - key = (String) service.get("label"); - } - flatten(properties, service, key); - } - } - } + extractPropertiesFromServices(properties, map); } catch (Exception ex) { logger.error("Could not parse VCAP_SERVICES", ex); @@ -198,6 +171,32 @@ public class VcapApplicationListener implements return properties; } + private void extractPropertiesFromApplication(Properties properties, + Map map) { + if (map != null) { + flatten(properties, map, ""); + } + } + + private void extractPropertiesFromServices(Properties properties, + Map map) { + if (map != null) { + for (Object services : map.values()) { + @SuppressWarnings("unchecked") + List list = (List) services; + for (Object object : list) { + @SuppressWarnings("unchecked") + Map service = (Map) object; + String key = (String) service.get("name"); + if (key == null) { + key = (String) service.get("label"); + } + flatten(properties, service, key); + } + } + } + } + private void flatten(Properties properties, Map input, String path) { for (Entry entry : input.entrySet()) { String key = entry.getKey(); diff --git a/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java index 6cce6e7685..fd7c8794da 100644 --- a/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/cloudfoundry/VcapApplicationListenerTests.java @@ -16,6 +16,9 @@ package org.springframework.boot.cloudfoundry; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + import org.junit.Test; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; @@ -23,9 +26,6 @@ import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - /** * Tests for {@link VcapApplicationListener}. * @@ -49,6 +49,17 @@ public class VcapApplicationListenerTests { .getProperty("vcap.application.instance_id")); } + @Test + public void testApplicationUris() { + EnvironmentTestUtils + .addEnvironment( + this.context, + "VCAP_APPLICATION:{\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"uris\":[\"foo.cfapps.io\"]}"); + this.initializer.onApplicationEvent(this.event); + assertEquals("foo.cfapps.io", + this.context.getEnvironment().getProperty("vcap.application.uris[0]")); + } + @Test public void testUnparseableApplicationProperties() { EnvironmentTestUtils.addEnvironment(this.context, "VCAP_APPLICATION:");