org.sandev.basics.util
Class EnvGrabber

java.lang.Object
  extended by org.sandev.basics.util.EnvGrabber

public class EnvGrabber
extends java.lang.Object

Legacy utility to return the value of an environment variable. If you are running on Java 5 or later, then you should probably use System.getEnv to retrieve what you need. Prior to Java 5, there was no reasonable way of getting this information via the standard API, so various people wrote workarounds that made a call to the environment and then parsed up the results. Since environment calls were expensive (they forked a new JVM), we wrote this utility to make this call once and then hold onto the results for reference.

The core of this code was originally nicked from http://www.rgagnon.com/javadetails/java-0150.html. Our thanks to Real Gagnon for posting this and making it available.


Field Summary
protected static java.util.Properties envVars
          The single reference copy.
 
Constructor Summary
EnvGrabber()
           
 
Method Summary
static void dumpProperties(java.util.Properties props)
          Go through all the property keys and dump out the corresponding values WITHOUT TRUNCATING.
static void envDump()
          This method takes its best shot at dumping out a whole bunch of properties to stdout.
static java.lang.String getEnvVar(java.lang.String envVarName)
          Given the name of an environment variable, return its value.
protected static java.util.Properties getEnvVars()
          Code adapted from www.rgagnon.com
protected static java.util.Properties getEnvVarsFromFile()
          Attempt to read the env vars from a local file.
static java.lang.String safeGetEnvVar(java.lang.String envVarName)
          Catches any thrown IOException and simply returns null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

envVars

protected static java.util.Properties envVars
The single reference copy.

Constructor Detail

EnvGrabber

public EnvGrabber()
Method Detail

getEnvVars

protected static java.util.Properties getEnvVars()
                                          throws java.io.IOException
Code adapted from www.rgagnon.com

Throws:
java.io.IOException

getEnvVarsFromFile

protected static java.util.Properties getEnvVarsFromFile()
                                                  throws java.io.IOException
Attempt to read the env vars from a local file. The idea here is that if we are on a server where memory is too tight to launch another JVM (which Runtime.exec requires), then you can get onto that server and just dump the env vars yourself ahead of time. If all you are looking for is things like the hostname or other things that don't change much, then that may be sufficient.

So for example, if you see from the log that getEnvVars is failing on startup, then you might login (probably as root) and do something like:

      cd /opt/tomcat5
      env > SANDEnvVarDump.out
      chgrp tomcat SANDEnvVarDump.out
      chown tomcat SANDEnvVarDump.out
 

Throws:
java.io.IOException

getEnvVar

public static java.lang.String getEnvVar(java.lang.String envVarName)
                                  throws java.io.IOException
Given the name of an environment variable, return its value. This throws an IOException if anything goes wrong.

Throws:
java.io.IOException

safeGetEnvVar

public static java.lang.String safeGetEnvVar(java.lang.String envVarName)
Catches any thrown IOException and simply returns null.


envDump

public static void envDump()
This method takes its best shot at dumping out a whole bunch of properties to stdout. Useful when you are trying to figure out what's around. This method does not make use of the properties list method since that truncates the displayed values (typically right where you wanted to see it).


dumpProperties

public static void dumpProperties(java.util.Properties props)
Go through all the property keys and dump out the corresponding values WITHOUT TRUNCATING.