Getting your VCAP_SERVICES variable out of cloudfoundry in Java is tough. This makes it easier.
Java Groovy
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
repo/com/github/scottmotte/0.0.1
src
.gitignore
README.md
build.gradle

README.md

vcapenv

This Java module makes it easy for you to get our your VCAP_SERVICES environment variables on CloudFoundry.

import com.github.scottmotte.Vcapenv;
Vcapenv vcapenv             = new Vcapenv();
String sendgrid_username    = vcapenv.SENDGRID_USERNAME();
String sendgrid_password    = vcapenv.SENDGRID_PASSWORD();

Available Methods

Vcapenv vcapenv = new Vcapenv();
vcapenv.SENDGRID_USERNAME();
vcapenv.SENDGRID_PASSWORD();

Please add more by creating a pull request.

Dig Deeper

You can also, dig down into the VCAP_SERVICES json by doing something like this:

Vcapenv vcapenv = new Vcapenv();
vcapenv.get("sendgrid-n/a").get(0).get("credentials").get("username").toString();

Installation

There are multiple ways to install this library. I recommend using Gradle.

via Gradle (recommended)

Add the following to your build.gradle file in the root of your project.

...

repositories {
  mavenCentral()
  add(new org.apache.ivy.plugins.resolver.URLResolver()) {
    name = 'GitHub'
    addArtifactPattern 'https://github.com/scottmotte/vcapenv/raw/v[revision]/repo/com/github/scottmotte/[revision]/vcapenv-[revision]-jar.jar'
  }
}
dependencies {
  ...
  compile 'com.github.scottmotte:vcapenv:0.0.1'
}

...

Then import the library - in the file appropriate to your Java project.

import com.github.scottmotte.Vcapenv;

via copy/paste

Include the Vcapenv.java library

Copy and paste the Vcapenv.java file into your project. That file is available here: https://github.com/scottmotte/vcapenv/blob/master/src/main/java/com/github/scottmotte/Vcapenv.java

Then import the library - in the file appropriate to your Java project.

import com.github.scottmotte.Vcapenv;

Include the required dependencies

via Maven

I'd like to get this on Maven. Please +1 your support if you'd like to see it on Maven as well.

Testing

First set some test export environment variables.

export VCAP_SERVICES='{"sendgrid-n/a":[{"name":"sendgrid-e5af0","label":"sendgrid-n/a","tags":["smtp"],"plan":"free","credentials":{"username":"orion","hostname":"smtp.sendgrid.net","password":"ofsky"}}]}'

Then run the tests.

gradle test -i