org.foray.common
Class Configuration

java.lang.Object
  extended by org.foray.common.Configuration
Direct Known Subclasses:
OutputConfig, SessionConfig

public abstract class Configuration
extends Object

Abstract superclass for configuration options. All options are stored in key / value pairs. Protected Convenience methods are available to handle Boolean, Int, String, and List values.

Most accessor methods are protected. The purpose is to force clients to use the subclass accessor methods instead, where more intelligence can be applied with regard to handling null values, etc. Applications that wish to make these available should simply override them with public methods in the subclass.


Field Summary
static byte PRECEDENCE_COMMAND_LINE
          Constant indicating the highest-level precedence, that of an item set on the command-line.
static byte PRECEDENCE_CONFIG_FILE
          Constant indicating the middle-level precedence, that of an item in the configuration file.
static byte PRECEDENCE_DEFAULT
          Constant indicating the lowest precedence, that of the default values.
 
Constructor Summary
protected Configuration()
          Constructor.
 
Method Summary
protected  void addUnvalidatedOption(String key, Object value, int precedenceValue)
          Adds an item to the configuration without giving the subclass an opportunity to validate it.
 String dumpConfiguration()
          Returns the contents of this Configuration as a String, suitable for logging.
protected  Boolean getBooleanValue(String key)
          Convenience method to access boolean values in the configuration.
protected  int getIntValue(String key)
          Convenience methods to access int values in the configuration.
protected  List<?> getListValue(String key)
          Convenience method to access list values in the configuration.
protected abstract  String getName()
          Supplies a name for this configuration.
protected  String getStringValue(String key)
          Convenience method to access strings values in the configuration.
protected  Object getValue(String key)
          Accessor method for a generic property.
protected  Boolean parseBoolean(String key, String value)
          Converts a given configuration item to a Boolean value.
abstract  boolean parseOption(String key, String value, int precedenceValue)
          Attempt to add the configuration option.
protected  String[] parseSpaceDelimitedList(String key, String value)
          Converts a given space-delimited configuration item to an ArrayList.
protected  URL parseURL(String key, String urlString)
          Converts a given configuration item to a URL.
protected  URL parseURLDirectory(String key, String directory)
          Converts a given configuration item to a URL directory, logging a warning if the resulting URL points to a file instead of a directory.
protected  URL parseURLFile(String key, String fileName)
          Converts a given configuration item to a URL, logging a warning if the resulting URL points to a directory instead of a file.
protected  void put(String key, Object value, int precedenceValue)
          Adds or updates a configuration option.
protected abstract  void setDefaults()
          Sets any default configuration values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRECEDENCE_DEFAULT

public static final byte PRECEDENCE_DEFAULT
Constant indicating the lowest precedence, that of the default values.

See Also:
Constant Field Values

PRECEDENCE_CONFIG_FILE

public static final byte PRECEDENCE_CONFIG_FILE
Constant indicating the middle-level precedence, that of an item in the configuration file.

See Also:
Constant Field Values

PRECEDENCE_COMMAND_LINE

public static final byte PRECEDENCE_COMMAND_LINE
Constant indicating the highest-level precedence, that of an item set on the command-line.

See Also:
Constant Field Values
Constructor Detail

Configuration

protected Configuration()
Constructor.

Method Detail

setDefaults

protected abstract void setDefaults()
Sets any default configuration values.


getName

protected abstract String getName()
Supplies a name for this configuration.

Returns:
A name for this configuration, suitable to be used in logging messages.

getValue

protected Object getValue(String key)
Accessor method for a generic property.

Parameters:
key - a string containing the key value for the configuration value
Returns:
Object containing the value; normally you would use one of the convenience methods, which return the correct form, null if the key is not defined.

getStringValue

protected String getStringValue(String key)
Convenience method to access strings values in the configuration.

Parameters:
key - a string containing the key value for the configuration value
Returns:
String a string containing the value, null if the key is not defined.

getIntValue

protected int getIntValue(String key)
Convenience methods to access int values in the configuration.

Parameters:
key - a string containing the key value for the configuration value
Returns:
The int value, or -1 if the key is not defined.

getBooleanValue

protected Boolean getBooleanValue(String key)
Convenience method to access boolean values in the configuration.

Parameters:
key - a string containing the key value for the configuration value
Returns:
The Boolean value, or null if the key is not defined.

getListValue

protected List<?> getListValue(String key)
Convenience method to access list values in the configuration.

Parameters:
key - a string containing the key value for the configuration value
Returns:
The List value, or null if the key is not defined.

put

protected void put(String key,
                   Object value,
                   int precedenceValue)
Adds or updates a configuration option. If the option does not already exist, it is added. If it does exist, its previous precedence value is compared to the precedence asserted by precedenceValue. If precedenceValue is greater than or equal to the previous precedence value, the new value is stored. Otherwise, the old value is retained and the new value is disregarded.

Parameters:
key - A string containing the key value for the configuration value.
value - The object containing the configuration value.
precedenceValue - The precedence with which this option should be treated. Entries with a higher precedence value override entries with a lower value.

parseOption

public abstract boolean parseOption(String key,
                                    String value,
                                    int precedenceValue)
                             throws ConfigurationException
Attempt to add the configuration option.

Parameters:
key - A string containing the key value for the configuration value.
value - The string containing the configuration value.
precedenceValue - The precedence with which this option should be treated. Entries with a higher precedence value override entries with a lower value.
Returns:
True if the option was successfully added, false if it does not belong in this Configuration implementation.
Throws:
ConfigurationException - For an invalid value for the given key.

addUnvalidatedOption

protected void addUnvalidatedOption(String key,
                                    Object value,
                                    int precedenceValue)
Adds an item to the configuration without giving the subclass an opportunity to validate it.

Parameters:
key - A string containing the key value for the configuration value.
value - The object containing the configuration value.
precedenceValue - The precedence with which this option should be treated. Entries with a higher precedence value override entries with a lower value.

parseBoolean

protected Boolean parseBoolean(String key,
                               String value)
                        throws ConfigurationException
Converts a given configuration item to a Boolean value.

Parameters:
key - A string containing the key value for the configuration value.
value - The unparsed boolean value.
Returns:
The parsed Boolean.
Throws:
ConfigurationException - If value is neither "true" nor "false".

parseURLDirectory

protected URL parseURLDirectory(String key,
                                String directory)
                         throws ConfigurationException
Converts a given configuration item to a URL directory, logging a warning if the resulting URL points to a file instead of a directory.

Parameters:
key - A string containing the key value for the configuration value.
directory - The unparsed URL directory value.
Returns:
The parsed URL.
Throws:
ConfigurationException - If directory is malformed, or if it is not a directory.

parseURLFile

protected URL parseURLFile(String key,
                           String fileName)
                    throws ConfigurationException
Converts a given configuration item to a URL, logging a warning if the resulting URL points to a directory instead of a file.

Parameters:
key - A string containing the key value for the configuration value.
fileName - The unparsed URL name.
Returns:
The parsed URL.
Throws:
ConfigurationException - If fileName is malformed, or if it is not a file.

parseURL

protected URL parseURL(String key,
                       String urlString)
                throws ConfigurationException
Converts a given configuration item to a URL.

Parameters:
key - A string containing the key value for the configuration value.
urlString - The unparsed URL name.
Returns:
The parsed URL.
Throws:
ConfigurationException - If urlString is malformed.

parseSpaceDelimitedList

protected String[] parseSpaceDelimitedList(String key,
                                           String value)
Converts a given space-delimited configuration item to an ArrayList.

Parameters:
key - A string containing the key value for the configuration value.
value - The space-delimited input.
Returns:
The parsed List.

dumpConfiguration

public String dumpConfiguration()
Returns the contents of this Configuration as a String, suitable for logging. Usually used for debugging.

Returns:
The contents of this Configuration.


Copyright © 2017. All rights reserved.