Gordon Dickens is an instructor, mentor & consultant. Gordon is currently architecting and teaching several official SpringSource courses and actively tweets about open source technology at http://twitter.com/gdickens. Gordon is active within the Spring Framework community focussed on: Spring training, Spring Roo, Spring Integration, Spring Batch and Eclipse Virgo OSGi projects. Gordon is a DZone MVB and is not an employee of DZone and has posted 39 posts at DZone. You can read more from them at their website. View Full User Profile

Use Java API via Spring to Define a Cloud Environment (Heroku, Jelastic, and Cloudbees)

  • submit to reddit

I put together Java sample applications using Spring 3.1. Using Spring’s Environment abstractions, we can gather details about the underlying platform.

See the following sample pages

Environment Bean

package com.gordondickens.roobees.util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import javax.annotation.PostConstruct;
import java.util.*;
public class MyEnvironment {
  private static final Logger logger = LoggerFactory.getLogger(MyEnvironment.class);
  @Value("#{ systemProperties['user.language'] }")
  private String varOne;
  @Value("#{ systemProperties }")
  private Properties systemProperties;
  @Value("#{ systemEnvironment }")
  private Properties systemEnvironment;
  @Value("#{ environment }")
  private StandardEnvironment environment;
  public String toString() {
    return "\n\n********************** MyEnvironment: ["
      + "\n\tsystemProperties=" + formatMe(systemProperties.toString())
      + ", \n\n\tsystemEnvironment=" + formatMe(systemEnvironment.toString())
      + ", \n\n\tenvironment=" + formatMe(environment.toString()) + "]";
  private static String formatMe(final String in) {
    String out = in;
    out = in.replace("{", "{\n\t\t");
    out = out.replace(", ", "\n\t\t");
    return out;
  public void afterInstantiation() {
  public StandardEnvironment getEnvironment() {
    return environment;
  public Properties getSystemEnvironment() {
    return systemEnvironment;
  public Map<String,String> getSortedSystemEnvironment() {
    Properties p = systemEnvironment;
    Object[] keys = p.keySet().toArray();
    Map<String, String> treeMap = new TreeMap<String, String>();
    treeMap.putAll((Map) p);
    return treeMap;
  public Properties getSystemProperties() {
    return systemProperties;
  public Map<String,String> getSortedSystemProperties() {
    Properties p = systemProperties;
    Object[] keys = p.keySet().toArray();
    Map<String, String> treeMap = new TreeMap<String, String>();
    treeMap.putAll((Map) p);
    return treeMap;
  public List<String> getServletConfigInitParams() {
    return getPropertyList("servletConfigInitParams");
  public List<String> getServletContextInitParams() {
    return getPropertyList("servletContextInitParams");
  public List<String> getJndiProperties() {
    return getPropertyList("jndiProperties");
  private List<String> getPropertyList(String propSrcName) {
    if (environment.getPropertySources().contains(propSrcName)) {
      PropertySource ps = environment.getPropertySources().get(propSrcName);
      String results = ReflectionToStringBuilder.toString(ps, ToStringStyle.SHORT_PREFIX_STYLE);
      List<String> resultList = Arrays.asList(StringUtils.split(results, ','));
      return resultList;
    } else
        return null;

Wire in the Bean

If you want the environment information logged, then invoke the afterInstantiation method. It is marked as @PostConstruct, so it will automatically invoke if you use <context:annotation-config/> or <context:component-scan ... /> as long as component-scan includes the package, or package parents of the class.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  <bean id="myEnvironment"

Source Code

See my Roo CloudBees Demo Code – https://github.com/gordonad/gordonad-roo-1.2-cloudbees


Published at DZone with permission of Gordon Dickens, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)