org.apache.commons.configuration
Class JNDIConfiguration

java.lang.Object
  extended byorg.apache.commons.configuration.AbstractConfiguration
      extended byorg.apache.commons.configuration.BaseConfiguration
          extended byorg.apache.commons.configuration.JNDIConfiguration
All Implemented Interfaces:
Configuration

public class JNDIConfiguration
extends BaseConfiguration
implements Configuration

This Configuration class allows you to interface with a JNDI datasource.

Version:
$Id: JNDIConfiguration.java,v 1.2 2003/12/24 14:28:22 epugh Exp $
Author:
Eric Pugh

Nested Class Summary
 
Nested classes inherited from class org.apache.commons.configuration.AbstractConfiguration
AbstractConfiguration.Container, AbstractConfiguration.PropertiesTokenizer
 
Field Summary
private  java.util.List clearedProperties
           
private  javax.naming.Context envCtx
           
private static org.apache.commons.logging.Log log
           
private  java.lang.String prefix
           
 
Fields inherited from class org.apache.commons.configuration.BaseConfiguration
 
Fields inherited from class org.apache.commons.configuration.AbstractConfiguration
defaults, END_TOKEN, START_TOKEN
 
Constructor Summary
JNDIConfiguration()
          Creates an empty JNDIConfiguration object which can then be added some other Configuration files
 
Method Summary
 void addProperty(java.lang.String key, java.lang.Object token)
          JNDIConfigurations can not be added to
 void clearProperty(java.lang.String key)
          Clear a property in the configuration.
 boolean containsKey(java.lang.String key)
          check if the configuration contains the key, or the key has been removed.
 java.lang.Boolean getBoolean(java.lang.String key, java.lang.Boolean defaultValue)
          Get a boolean associated with the given configuration key.
 java.lang.Byte getByte(java.lang.String key, java.lang.Byte defaultValue)
          Get a byte associated with the given configuration key.
private  javax.naming.Context getContext()
           
 java.lang.Double getDouble(java.lang.String key, java.lang.Double defaultValue)
          Get a double associated with the given configuration key.
 java.lang.Float getFloat(java.lang.String key, java.lang.Float defaultValue)
          Get a float associated with the given configuration key.
 java.lang.Integer getInteger(java.lang.String key, java.lang.Integer defaultValue)
          Get a int associated with the given configuration key.
 java.util.Iterator getKeys()
          Get the list of the keys contained in the configuration repository.
 java.util.Iterator getKeys(java.lang.String key)
          Get the list of the keys contained in the configuration repository that match a passed in beginning pattern.
 java.util.List getList(java.lang.String key, java.util.List defaultValue)
          Get a List of strings associated with the given configuration key.
 java.lang.Long getLong(java.lang.String key, java.lang.Long defaultValue)
          Get a long associated with the given configuration key.
 java.lang.String getPrefix()
           
 java.util.Properties getProperties(java.lang.String key)
          Get a list of properties associated with the given configuration key.
 java.lang.Object getProperty(java.lang.String key)
          Gets a property from the configuration.
 java.lang.Short getShort(java.lang.String key, java.lang.Short defaultValue)
          Get a short associated with the given configuration key.
private  javax.naming.Context getStartingContextPoint(java.util.List keys, javax.naming.NamingEnumeration enum)
          Because JNDI is based on a tree configuration, we need to filter down the tree, till we find the Context specified by the key to start from.
 java.lang.String getString(java.lang.String key, java.lang.String defaultValue)
          Get a string associated with the given configuration key.
 java.lang.String[] getStringArray(java.lang.String key)
          Get an array of strings associated with the given configuration key.
private  java.lang.Object getValueFromJNDI(java.lang.String key)
           
 boolean isEmpty()
          Check if the configuration is empty.
private  void recursiveGetKeys(java.util.List keys, javax.naming.NamingEnumeration enum, java.lang.String key)
          This method recursive traverse the JNDI tree, looking for Context objects.
 void setPrefix(java.lang.String prefix)
          Sets the prefix.
 void setProperty(java.lang.String key, java.lang.Object value)
          Set a property, this will replace any previously set values.
 Configuration subset(java.lang.String prefix)
          Create an ExtendedProperties object that is a subset of this one.
 
Methods inherited from class org.apache.commons.configuration.BaseConfiguration
addPropertyDirect, getPropertyDirect
 
Methods inherited from class org.apache.commons.configuration.AbstractConfiguration
getBoolean, getBoolean, getByte, getByte, getDouble, getDouble, getFloat, getFloat, getInt, getInt, getList, getLong, getLong, getProperties, getShort, getShort, getString, interpolate, interpolateHelper, processString, testBoolean
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.commons.configuration.Configuration
getBoolean, getBoolean, getByte, getByte, getDouble, getDouble, getFloat, getFloat, getInt, getInt, getList, getLong, getLong, getShort, getShort, getString
 

Field Detail

log

private static org.apache.commons.logging.Log log

prefix

private java.lang.String prefix

envCtx

private javax.naming.Context envCtx

clearedProperties

private java.util.List clearedProperties
Constructor Detail

JNDIConfiguration

public JNDIConfiguration()
Creates an empty JNDIConfiguration object which can then be added some other Configuration files

Method Detail

addProperty

public void addProperty(java.lang.String key,
                        java.lang.Object token)
JNDIConfigurations can not be added to

Specified by:
addProperty in interface Configuration
Overrides:
addProperty in class AbstractConfiguration
Parameters:
key - The Key to add the property to.
token - The Value to add.

recursiveGetKeys

private void recursiveGetKeys(java.util.List keys,
                              javax.naming.NamingEnumeration enum,
                              java.lang.String key)
                       throws javax.naming.NamingException
This method recursive traverse the JNDI tree, looking for Context objects. When it finds them, it traverses them as well. Otherwise it just adds the values to the list of keys found.

Parameters:
keys - All the keys that have been found.
enum - An enumeration of all the elements found at a specific context
key - What key we are building on.
Throws:
javax.naming.NamingException - If JNDI has an issue.

getKeys

public java.util.Iterator getKeys()
Get the list of the keys contained in the configuration repository.

Specified by:
getKeys in interface Configuration
Overrides:
getKeys in class BaseConfiguration
Returns:
An Iterator.

getKeys

public java.util.Iterator getKeys(java.lang.String key)
Get the list of the keys contained in the configuration repository that match a passed in beginning pattern.

Specified by:
getKeys in interface Configuration
Overrides:
getKeys in class AbstractConfiguration
Parameters:
key - the key pattern to match on.
Returns:
An Iterator.

getStartingContextPoint

private javax.naming.Context getStartingContextPoint(java.util.List keys,
                                                     javax.naming.NamingEnumeration enum)
                                              throws javax.naming.NamingException
Because JNDI is based on a tree configuration, we need to filter down the tree, till we find the Context specified by the key to start from. Otherwise return null.

Returns:
The context at that key's location in the JNDI tree, or null if not found
Throws:
javax.naming.NamingException - if JNDI has an issue

getProperties

public java.util.Properties getProperties(java.lang.String key)
Get a list of properties associated with the given configuration key.

Specified by:
getProperties in interface Configuration
Overrides:
getProperties in class AbstractConfiguration
Parameters:
key - The configuration key.
Returns:
The associated properties if key is found.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String/List.
java.lang.IllegalArgumentException - if one of the tokens is malformed (does not contain an equals sign).
See Also:
AbstractConfiguration.getProperties(String, Properties)

isEmpty

public boolean isEmpty()
Description copied from interface: Configuration
Check if the configuration is empty.

Specified by:
isEmpty in interface Configuration
Overrides:
isEmpty in class BaseConfiguration
Returns:
true if Configuration is empty, false otherwise.

getProperty

public java.lang.Object getProperty(java.lang.String key)
Gets a property from the configuration.

Specified by:
getProperty in interface Configuration
Overrides:
getProperty in class AbstractConfiguration
Parameters:
key - property to retrieve
Returns:
value as object. Will return user value if exists, if not then default value if exists, otherwise null

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value)
Set a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key), addProperty(key,value).

Specified by:
setProperty in interface Configuration
Overrides:
setProperty in class AbstractConfiguration
Parameters:
key -
value -

clearProperty

public void clearProperty(java.lang.String key)
Clear a property in the configuration. Just marks it as cleared, doesn't change the underlying JNDI data source.

Specified by:
clearProperty in interface Configuration
Overrides:
clearProperty in class BaseConfiguration
Parameters:
key - the key to remove along with corresponding value.

containsKey

public boolean containsKey(java.lang.String key)
check if the configuration contains the key, or the key has been removed.

Specified by:
containsKey in interface Configuration
Overrides:
containsKey in class BaseConfiguration
Parameters:
key - the configuration key
Returns:
true if Configuration contain given key, false otherwise.

subset

public Configuration subset(java.lang.String prefix)
Create an ExtendedProperties object that is a subset of this one. Take into account duplicate keys by using the setProperty() in ExtendedProperties.

Specified by:
subset in interface Configuration
Overrides:
subset in class AbstractConfiguration
Parameters:
prefix -
Returns:
subset of configuration if there is keys, that match given prefix, or null if there is no such keys.

getBoolean

public java.lang.Boolean getBoolean(java.lang.String key,
                                    java.lang.Boolean defaultValue)
Get a boolean associated with the given configuration key.

Specified by:
getBoolean in interface Configuration
Overrides:
getBoolean in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated boolean if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Boolean.

getByte

public java.lang.Byte getByte(java.lang.String key,
                              java.lang.Byte defaultValue)
Get a byte associated with the given configuration key.

Specified by:
getByte in interface Configuration
Overrides:
getByte in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated byte if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Byte.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getDouble

public java.lang.Double getDouble(java.lang.String key,
                                  java.lang.Double defaultValue)
Get a double associated with the given configuration key.

Specified by:
getDouble in interface Configuration
Overrides:
getDouble in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated double if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Double.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getFloat

public java.lang.Float getFloat(java.lang.String key,
                                java.lang.Float defaultValue)
Get a float associated with the given configuration key.

Specified by:
getFloat in interface Configuration
Overrides:
getFloat in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated float if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Float.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getInteger

public java.lang.Integer getInteger(java.lang.String key,
                                    java.lang.Integer defaultValue)
Get a int associated with the given configuration key.

Specified by:
getInteger in interface Configuration
Overrides:
getInteger in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated int if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Integer.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getLong

public java.lang.Long getLong(java.lang.String key,
                              java.lang.Long defaultValue)
Get a long associated with the given configuration key.

Specified by:
getLong in interface Configuration
Overrides:
getLong in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated long if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Long.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getShort

public java.lang.Short getShort(java.lang.String key,
                                java.lang.Short defaultValue)
Get a short associated with the given configuration key.

Specified by:
getShort in interface Configuration
Overrides:
getShort in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated short if key is found and has valid format, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a Short.
java.lang.NumberFormatException - is thrown if the value mapped by the key has not a valid number format.

getString

public java.lang.String getString(java.lang.String key,
                                  java.lang.String defaultValue)
Get a string associated with the given configuration key.

Specified by:
getString in interface Configuration
Overrides:
getString in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated string if key is found, default value otherwise.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String.

getStringArray

public java.lang.String[] getStringArray(java.lang.String key)
Get an array of strings associated with the given configuration key.

Specified by:
getStringArray in interface Configuration
Overrides:
getStringArray in class AbstractConfiguration
Parameters:
key - The configuration key.
Returns:
The associated string array if key is found.
Throws:
java.lang.ClassCastException - is thrown if the key maps to an object that is not a String/List of Strings.

getList

public java.util.List getList(java.lang.String key,
                              java.util.List defaultValue)
Get a List of strings associated with the given configuration key. Typically this will be just a single item, as you can't have multiple properties with the same name.

Specified by:
getList in interface Configuration
Overrides:
getList in class AbstractConfiguration
Parameters:
key - The configuration key.
defaultValue - The default value.
Returns:
The associated List.

getPrefix

public java.lang.String getPrefix()
Returns:
String

setPrefix

public void setPrefix(java.lang.String prefix)
Sets the prefix.

Parameters:
prefix - The prefix to set

getValueFromJNDI

private java.lang.Object getValueFromJNDI(java.lang.String key)

getContext

private javax.naming.Context getContext()
                                 throws javax.naming.NamingException
Throws:
javax.naming.NamingException