Long story short, I just want to know if someone's already created the handling methods to resolve the effective bluemixContext.isRunningOnBluemix() and dbName, dbServer, dbAppPath calls in Java, or if I should spend a little time with System.getenv()
.
[Update] Thank you to both David and Tony, who have both provided illustrative responses to my original question; I just wish I'd done a slightly better job at asking it the first time.
Ultimately, I was trying to get at more than just the VCAP_SERVICES
var and just wanted access to the bluemixContext object in an app config/utils bean I was writing. I settled on using BluemixContextManager.getInstance()
, but as pointed out I could also use a variable resolver to get the bluemixContext object (which is available in Java, it just needs resolving) or parse the environment variables myself (e.g.- System.getenv("VCAP_SERVICES")
).
I'm going to leave my accepted answer as is, but I've up-voted both David's and Tony's answers. [/Update]
Answer by Eric McCormick (119) | Jul 15, 2015 at 02:46 PM
As much David Amid's answer is perfectly valid, what I was ultimately looking for was to use existing code to achieve the same results within the XPages runtime's Java context. That being said, poking through the Extension Library's code in the GitHub repository yielded the class in question. Now I just need to get a handle on the current instance of it and I'm good to go.
Answer by Tony McGuckin (486) | Jul 15, 2015 at 02:57 PM
Simple really... bluemixContext is a global var already available in the running context for SSJS and Pojo's...
bluemixContext.getVCAP_SERVICES()
I had settled on resolving the instance via BluemixContextManager.getInstance()
.
I was setting up a managed bean, DDE told me bluemixContext couldn't be resolved. Am I missing something obvious about how the global var can be resolved (or would a variable resolver on bluemixContext be preferable to the getInstance call I'm using)?
No, you're good there Eric with the getInstance approach in Java... the example above is for SSJS just to elaborate var's availability.
It's a great and convenient object, unless you're trying to be a Java purist :-P
Thanks for the clarification Tony.
Answer by DavidAmid (644) | Jul 15, 2015 at 02:16 PM
Taken from the Watson Developer Cloud on Git
/**
* Gets the <b>VCAP_SERVICES</b> environment variable and return it
* as a JSONObject.
*
* @return the VCAP_SERVICES as Json
*/
private JSONObject getVcapServices() {
String envServices = System.getenv("VCAP_SERVICES");
if (envServices == null) return null;
JSONObject sysEnv = null;
try {
sysEnv = JSONObject.parse(envServices);
} catch (IOException e) {
// Do nothing, fall through to defaults
logger.log(Level.SEVERE, "Error parsing VCAP_SERVICES: "+e.getMessage(), e);
}
return sysEnv;
}
So, I shouldn't treat it appreciably different because it's within an XPages runtime app. I can live with that :-) Thanks.