Extract lists in VCAP_APPLICATION (e.g. uris)
Fixes gh-1773
This commit is contained in:
@@ -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<String, Object> map = this.parser.parseMap(environment.getProperty(
|
||||
VCAP_APPLICATION, "{}"));
|
||||
if (map != null) {
|
||||
map = new LinkedHashMap<String, Object>(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<String, Object> map = this.parser.parseMap(environment.getProperty(
|
||||
VCAP_SERVICES, "{}"));
|
||||
if (map != null) {
|
||||
for (Object services : map.values()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object> list = (List<Object>) services;
|
||||
for (Object object : list) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> service = (Map<String, Object>) 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<String, Object> map) {
|
||||
if (map != null) {
|
||||
flatten(properties, map, "");
|
||||
}
|
||||
}
|
||||
|
||||
private void extractPropertiesFromServices(Properties properties,
|
||||
Map<String, Object> map) {
|
||||
if (map != null) {
|
||||
for (Object services : map.values()) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Object> list = (List<Object>) services;
|
||||
for (Object object : list) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> service = (Map<String, Object>) 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<String, Object> input, String path) {
|
||||
for (Entry<String, Object> entry : input.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
|
||||
@@ -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:");
|
||||
|
||||
Reference in New Issue
Block a user