Fix issue with new format of VCAP_APPLICATION
This commit is contained in:
@@ -37,22 +37,28 @@ public class SimpleJsonParser implements JsonParser {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> parseMap(String json) {
|
||||
if (json.startsWith("{")) {
|
||||
return parseMapInternal(json);
|
||||
}
|
||||
else if (json.trim().equals("")) {
|
||||
return new HashMap<String, Object>();
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("{")) {
|
||||
return parseMapInternal(json);
|
||||
}
|
||||
else if (json.equals("")) {
|
||||
return new HashMap<String, Object>();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Object> parseList(String json) {
|
||||
if (json.startsWith("[")) {
|
||||
return parseListInternal(json);
|
||||
}
|
||||
else if (json.trim().equals("")) {
|
||||
return new ArrayList<Object>();
|
||||
if (json != null) {
|
||||
json = json.trim();
|
||||
if (json.startsWith("[")) {
|
||||
return parseListInternal(json);
|
||||
}
|
||||
else if (json.trim().equals("")) {
|
||||
return new ArrayList<Object>();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -18,11 +18,14 @@ package org.springframework.boot.context.initializer;
|
||||
|
||||
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;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.config.JsonParser;
|
||||
import org.springframework.boot.config.JsonParserFactory;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
@@ -83,6 +86,9 @@ import org.springframework.util.StringUtils;
|
||||
public class VcapApplicationContextInitializer implements
|
||||
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
|
||||
|
||||
private static final Log logger = LogFactory
|
||||
.getLog(VcapApplicationContextInitializer.class);
|
||||
|
||||
private static final String VCAP_APPLICATION = "VCAP_APPLICATION";
|
||||
|
||||
private static final String VCAP_SERVICES = "VCAP_SERVICES";
|
||||
@@ -136,30 +142,54 @@ public class VcapApplicationContextInitializer implements
|
||||
}
|
||||
|
||||
private Properties getPropertiesFromApplication(Environment environment) {
|
||||
Map<String, Object> map = this.parser.parseMap(environment.getProperty(
|
||||
VCAP_APPLICATION, "{}"));
|
||||
Properties properties = new Properties();
|
||||
properties.putAll(map);
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
logger.error("Could not parse VCAP_APPLICATION", e);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
private Properties getPropertiesFromServices(Environment environment) {
|
||||
Map<String, Object> map = this.parser.parseMap(environment.getProperty(
|
||||
VCAP_SERVICES, "{}"));
|
||||
Properties properties = new Properties();
|
||||
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");
|
||||
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);
|
||||
}
|
||||
}
|
||||
flatten(properties, service, key);
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
logger.error("Could not parse VCAP_APPLICATION", e);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.boot.context.initializer;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.TestUtils;
|
||||
import org.springframework.boot.context.initializer.VcapApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
@@ -45,6 +44,25 @@ public class VcapApplicationContextInitializerTests {
|
||||
.getProperty("vcap.application.instance_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnparseableApplicationProperties() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestUtils.addEnviroment(context, "VCAP_APPLICATION:");
|
||||
this.initializer.initialize(context);
|
||||
assertEquals(null, context.getEnvironment().getProperty("vcap"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullApplicationProperties() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
TestUtils
|
||||
.addEnviroment(
|
||||
context,
|
||||
"VCAP_APPLICATION:{\"application_users\":null,\"instance_id\":\"bb7935245adf3e650dfb7c58a06e9ece\",\"instance_index\":0,\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\",\"name\":\"foo\",\"uris\":[\"foo.cfapps.io\"],\"started_at\":\"2013-05-29 02:37:59 +0000\",\"started_at_timestamp\":1369795079,\"host\":\"0.0.0.0\",\"port\":61034,\"limits\":{\"mem\":128,\"disk\":1024,\"fds\":16384},\"version\":\"3464e092-1c13-462e-a47c-807c30318a50\",\"name\":\"dsyerenv\",\"uris\":[\"dsyerenv.cfapps.io\"],\"users\":[],\"start\":\"2013-05-29 02:37:59 +0000\",\"state_timestamp\":1369795079}");
|
||||
this.initializer.initialize(context);
|
||||
assertEquals(null, context.getEnvironment().getProperty("vcap"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServiceProperties() {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
Reference in New Issue
Block a user