Tuesday, May 13, 2008

Getting environment variable for deployment environment automation

I want to get an environment variable, let's call it "deploy.env" which is set in the OS environment to tell my application where it is, it could be any of the following values: local, dev, int, qa, uat, prod. The purpose of which is to retrieve a particular setting to be used in that environment.

I tried to use System.getProperties() but it didn't give me the environment variable I need. It just can't access it. So I search the web, got some idea and wrote the following utility class. In my environment it returns what I want.


Feel free to use, but you're on your own, standard no-liability disclaimer apply. Always test your own code first before deploying to any server.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Utility class to access the OS environment variables.</p>
* @author Judah Lim (c)2007
* @copyright(disclaimer) USE AS-IS.
* NO WARRANTIES WHATSOEVER, EITHER EXPRESSED OR IMPLIED.
*/
public class EnvarsUtil {
private static Properties envars;
private static Log logger = LogFactory.getLog(EnvarsUtil.class);

public static void main(String[] args) {
System.out.println(EnvarsUtil.getEnvironmentVariable("deploy.env"));
}

/**
* <p>Retrieve an environment variable from current system environment.</p>
* @param envarName The name of the environment variable to retrieve.
* @return The String value of the environment variable, null if not found.
*/
public static String getEnvironmentVariable(String envarName) {
if(envars == null) {
initEnvironmentVars(false);
return envars.getProperty(envarName);
} else {
return envars.getProperty(envarName);
}
}

/**
* <p>Refresh the cached environment variables</p>
*/
public static void refreshEnvironmentVars() {
initEnvironmentVars(true);
}

/**
* <p>Read the environment variables and cahed them.</p>
* @param refresh set to true to refresh the cache, false otherwise.
* @return the environment variables as Properties.
*/
private static void initEnvironmentVars(boolean refresh) {
if (envars != null && !refresh) return;

Process process = null;
try {
process = osProcess();
} catch (InvocationTargetException e) {
logger.error("System environment values can't be retrieved.", e);
}

BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = null;
envars = new Properties();
try {
while ((line = bufferedReader.readLine()) != null) {
int idx = line.indexOf('=');
String key = line.substring(0, idx);
String value = line.substring(idx + 1);
envars.setProperty(key, value);
}
} catch (IOException e) {
logger.error("System environment values can't be processed.", e);
}

// Clean up
process = null;
bufferedReader = null;
line = null;
}

/**
* <p>Run an OS process to get all environment variables.</p>
* @return Process the OS process executed containig the results of execution.
* @throws InvocationTargetException
*/
private static Process osProcess() throws InvocationTargetException {
Process process = null;
Runtime runtime = null;
String osName = null;
try {
runtime = Runtime.getRuntime();
osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("windows 9") > -1) {
process = runtime.exec("command.com /c set");
} else if ((osName.indexOf("nt") > -1)
|| (osName.indexOf("windows 2000") > -1)
|| (osName.indexOf("windows xp") > -1)) {
process = runtime.exec("cmd.exe /c set");
} else {
process = runtime.exec("env");
}
runtime = null;
osName = null;
return process;
} catch (IOException e) {
// Clean up
process = null;
runtime = null;
osName = null;
// Convert to InvocationTargetException
throw new InvocationTargetException(e);
}
}
}

No comments:

Post a Comment