Reputation: 31
I used the following code to read my VCAP_SERVICES
environment variable of Liberty application.
And I am not getting any values, result shows null or "not found".
private void readECaaSEnvVars() {
Map<?, ?> env = System.getenv();
Object vcap = env.get("VCAP_SERVICES");
if (vcap == null) {
System.out.println("No VCAP_SERVICES found");
}
else {
try {
JSONObject obj = new JSONObject(vcap);
String[] names = JSONObject.getNames(obj);
if (names != null) {
for (String name : names) {
if (name.startsWith("DataCache")) {
JSONArray val = obj.getJSONArray(name);
JSONObject serviceAttr = val.getJSONObject(0);
JSONObject credentials = serviceAttr.getJSONObject("credentials");
String username = credentials.getString("username");
String password = credentials.getString("password");
String endpoint=credentials.getString("catalogEndPoint");
String gridName= credentials.getString("gridName");
System.out.println("Found configured username: " + username);
System.out.println("Found configured password: " + password);
System.out.println("Found configured endpoint: " + endpoint);
System.out.println("Found configured gridname: " + gridName);
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 299
Reputation: 3546
Your parsing code is OK.
cf restage <appname>
System.out.println("VCAP_SERVICES: " + System.getenv("VCAP_SERVICES"));
You should also know that by default the the Liberty buildpack generates or updates existing server.xml file configuration stanzas for the Data Cache instance. The bound Data Cache instance can be accessed by the application using JNDI. The cache instance can either be injected into the application with an @Resource annotation, or can be looked up by the application with the javax.naming.InitialContext.
To see your server.xml on Bluemix for Liberty application:
cf files myLibertyApplication app/wlp/usr/servers/defaultServer/server.xml
You should see something like:
<xsBindings>
<xsGrid jndiName="wxs/myCache"
id="myCache"
gridName="${cloud.services.myCache.connection.gridName}"
userName="${cloud.services.myCache.connection.username}"
password="${cloud.services.myCache.connection.password}"
clientDomain="${cloud.services.myCache.name}"/>
</xsBindings>
where your JNDI name is wxs/myCache. This avoids the need for parsing VCAP_SERVICES.
Upvotes: 2