Over a million developers have joined DZone.
Platinum Partner

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

· Cloud Zone

The Cloud Zone is brought to you in partnership with Iron.io. Discover how Microservices have transformed the way developers are building and deploying applications in the era of modern cloud infrastructure.

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;
 
  @Override
  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;
  }
 
  @PostConstruct
  public void afterInstantiation() {
    logger.debug(this.toString());
  }
 
  public StandardEnvironment getEnvironment() {
    return environment;
  }
 
  public Properties getSystemEnvironment() {
    return systemEnvironment;
  }
 
  public Map<String,String> getSortedSystemEnvironment() {
    Properties p = systemEnvironment;
    Object[] keys = p.keySet().toArray();
    Arrays.sort(keys);
    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();
    Arrays.sort(keys);
    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, ','));
      Collections.sort(resultList);
      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"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">
 
...
 
  <bean id="myEnvironment"
      class="com.gordondickens.roobees.util.MyEnvironment"
      init-method="afterInstantiation"/>
 
...
 
</beans>

Source Code

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

 

The Cloud Zone is brought to you in partnership with Iron.io. Learn how to build and test their Go programs inside Docker containers.

Topics:

Published at DZone with permission of Gordon Dickens , DZone MVB .

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}