Over a million developers have joined DZone.

What is in My Cloud Environment?

DZone's Guide to

What is in My Cloud Environment?

· Java Zone
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

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

My Cloud Presentations



Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.


Published at DZone with permission of Gordon Dickens, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}