org.apache.commons.configuration
Class EnvironmentConfiguration

java.lang.Object
  extended by org.apache.commons.configuration.event.EventSource
      extended by org.apache.commons.configuration.AbstractConfiguration
          extended by org.apache.commons.configuration.EnvironmentConfiguration
All Implemented Interfaces:
Configuration

public class EnvironmentConfiguration
extends AbstractConfiguration

A Configuration implementation that reads the platform specific environment variables. On pre java5 JRE it uses Ant Execute task to read the environment. (in this case ant must be present in classpath). On java >= 5 JRE it uses java.lang.System#getenv() and ant is not required.

This configuration implementation is read-only. It allows read access to the defined OS environment variables, but their values cannot be changed.

Usage of this class is easy: After an instance has been created the get methods provided by the Configuration interface can be used for querying environment variables, e.g.:

 Configuration envConfig = new EnvironmentConfiguration();
 System.out.println("JAVA_HOME=" + envConfig.getString("JAVA_HOME");
 

Since:
1.5
Author:
Nicolas De Loof
See Also:
Execute.getProcEnvironment()

Field Summary
private  java.util.Map environment
          Stores the environment properties.
private static java.lang.String METHOD_NAME
          Constant for the name of the getenv() method.
private static int VERSION_1_5
          Constant for the Java version 1.5.
 
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
END_TOKEN, EVENT_ADD_PROPERTY, EVENT_CLEAR, EVENT_CLEAR_PROPERTY, EVENT_READ_PROPERTY, EVENT_SET_PROPERTY, START_TOKEN
 
Constructor Summary
EnvironmentConfiguration()
          Constructor.
 
Method Summary
protected  void addPropertyDirect(java.lang.String key, java.lang.Object value)
          Adds a property to this configuration.
 void clear()
          Removes all properties from this configuration.
 void clearProperty(java.lang.String key)
          Removes a property from this configuration.
 boolean containsKey(java.lang.String key)
          Check if the configuration contains the specified key.
(package private)  void extractProperties14()
          Extracts environment properties on a JRE < 1.5.
(package private)  void extractProperties15()
          Extracts environment properties on a JR >= 1.5.
(package private)  void extractPropertiesFromCollection(java.util.Collection env)
          An internally used method for processing a collection with environment entries.
 java.util.Iterator getKeys()
          Get the list of the keys contained in the configuration.
 java.lang.Object getProperty(java.lang.String key)
          Gets a property from the configuration.
 boolean isEmpty()
          Check if the configuration is empty.
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
addErrorLogListener, addProperty, append, clearPropertyDirect, copy, createInterpolator, getBigDecimal, getBigDecimal, getBigInteger, getBigInteger, getBoolean, getBoolean, getBoolean, getByte, getByte, getByte, getDefaultListDelimiter, getDelimiter, getDouble, getDouble, getDouble, getFloat, getFloat, getFloat, getInt, getInt, getInteger, getInterpolator, getKeys, getList, getList, getListDelimiter, getLogger, getLong, getLong, getLong, getProperties, getProperties, getShort, getShort, getShort, getString, getString, getStringArray, getSubstitutor, interpolate, interpolate, interpolatedConfiguration, interpolateHelper, isDelimiterParsingDisabled, isThrowExceptionOnMissing, resolveContainerStore, setDefaultListDelimiter, setDelimiter, setDelimiterParsingDisabled, setListDelimiter, setLogger, setProperty, setThrowExceptionOnMissing, subset
 
Methods inherited from class org.apache.commons.configuration.event.EventSource
addConfigurationListener, addErrorListener, clearConfigurationListeners, clearErrorListeners, clone, createErrorEvent, createEvent, fireError, fireEvent, getConfigurationListeners, getErrorListeners, isDetailEvents, removeConfigurationListener, removeErrorListener, setDetailEvents
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

METHOD_NAME

private static final java.lang.String METHOD_NAME
Constant for the name of the getenv() method.

See Also:
Constant Field Values

VERSION_1_5

private static final int VERSION_1_5
Constant for the Java version 1.5.

See Also:
Constant Field Values

environment

private java.util.Map environment
Stores the environment properties.

Constructor Detail

EnvironmentConfiguration

public EnvironmentConfiguration()
Constructor.

Method Detail

addPropertyDirect

protected void addPropertyDirect(java.lang.String key,
                                 java.lang.Object value)
Adds a property to this configuration. Because this configuration is read-only, this operation is not allowed and will cause an exception.

Specified by:
addPropertyDirect in class AbstractConfiguration
Parameters:
key - the key of the property to be added
value - the property value

containsKey

public boolean containsKey(java.lang.String key)
Check if the configuration contains the specified key.

Parameters:
key - the key whose presence in this configuration is to be tested
Returns:
true if the configuration contains a value for this key, false otherwise
See Also:
Configuration.containsKey(java.lang.String)

getKeys

public java.util.Iterator getKeys()
Get the list of the keys contained in the configuration. The returned iterator can be used to obtain all defined keys. Note that the exact behavior of the iterator's remove() method is specific to a concrete implementation. It may remove the corresponding property from the configuration, but this is not guaranteed. In any case it is no replacement for calling Configuration.clearProperty(String) for this property. So it is highly recommended to avoid using the iterator's remove() method.

Returns:
An Iterator.
See Also:
Configuration.getKeys()

getProperty

public java.lang.Object getProperty(java.lang.String key)
Gets a property from the configuration. This is the most basic get method for retrieving values of properties. In a typical implementation of the Configuration interface the other get methods (that return specific data types) will internally make use of this method. On this level variable substitution is not yet performed. The returned object is an internal representation of the property value for the passed in key. It is owned by the Configuration object. So a caller should not modify this object. It cannot be guaranteed that this object will stay constant over time (i.e. further update operations on the configuration may change its internal state).

Parameters:
key - property to retrieve
Returns:
the value to which this configuration maps the specified key, or null if the configuration contains no mapping for this key.
See Also:
Configuration.getProperty(java.lang.String)

isEmpty

public boolean isEmpty()
Check if the configuration is empty.

Returns:
true if the configuration contains no property, false otherwise.
See Also:
Configuration.isEmpty()

clearProperty

public void clearProperty(java.lang.String key)
Removes a property from this configuration. Because this configuration is read-only, this operation is not allowed and will cause an exception.

Specified by:
clearProperty in interface Configuration
Overrides:
clearProperty in class AbstractConfiguration
Parameters:
key - the key of the property to be removed

clear

public void clear()
Removes all properties from this configuration. Because this configuration is read-only, this operation is not allowed and will cause an exception.

Specified by:
clear in interface Configuration
Overrides:
clear in class AbstractConfiguration

extractProperties14

void extractProperties14()
Extracts environment properties on a JRE < 1.5. This implementation uses ant for this purpose.


extractPropertiesFromCollection

void extractPropertiesFromCollection(java.util.Collection env)
An internally used method for processing a collection with environment entries. The collection must contain strings like property=value. Such a collection is returned by ant.

Parameters:
env - the collection with the properties

extractProperties15

void extractProperties15()
Extracts environment properties on a JR >= 1.5. From this Java version on, there is an official way of doing this. However because the code should compile on lower Java versions, too, we have to invoke the method using reflection.