org.eodisp.util.configuration
Class ConfigurationImpl

java.lang.Object
  extended by org.eodisp.util.configuration.ConfigurationImpl
All Implemented Interfaces:
Configuration
Direct Known Subclasses:
Log4JConfiguration

public class ConfigurationImpl
extends java.lang.Object
implements Configuration

A basic implementation of the Configuration interface using a file as its backend.

Version:
$Id: ConfigurationImpl.java 3753 2006-09-26 16:11:53Z ibirrer $
Author:
ibirrer

Nested Class Summary
 class ConfigurationImpl.EntryImpl
          A configuration entry that saves a configuration value as a string.
 
Nested classes/interfaces inherited from interface org.eodisp.util.configuration.Configuration
Configuration.Entry
 
Field Summary
(package private)  java.util.Map<java.lang.String,Configuration.Entry> entries
          Holds configuration entries
(package private) static org.apache.log4j.Logger logger
          Log4J logger for this class
(package private)  boolean needsSave
           
 
Constructor Summary
ConfigurationImpl(java.lang.String id, java.lang.String name, java.lang.String description, java.io.File file)
          Creates a new configuration with no entries.
 
Method Summary
 boolean containsEntry(java.lang.String key)
          Returns whether this configuration contains an entry with the given key.
 Configuration.Entry createBooleanEntry(java.lang.String key, boolean defaultValue, java.lang.String entryDescription)
           
 Configuration.Entry createEntry(java.lang.String key, java.lang.String defaultValue, java.lang.String entryDescription)
           
 Configuration.Entry createEnumEntry(java.lang.String key, java.lang.Enum defaultValue, java.lang.String entryDescription)
           
<E extends java.lang.Enum<E>>
Configuration.Entry
createEnumSetEntry(java.lang.String key, java.util.EnumSet<E> defaultEnumSet, java.lang.Class elementType, java.lang.String entryDescription)
           
 Configuration.Entry createFileEntry(java.lang.String key, java.io.File defaultValue, java.lang.String entryDescription)
           
 Configuration.Entry createFilelistEntry(java.lang.String key, java.util.List<java.io.File> defaultFileList, java.lang.String entryDescription)
           
 Configuration.Entry createIntEntry(java.lang.String key, int defaultValue, java.lang.String entryDescription)
           
 Configuration.Entry createLongEntry(java.lang.String key, long defaultValue, java.lang.String entryDescription)
           
 Configuration.Entry[] entries()
          Returns a safe array of all configuration entries of this configuration..
 boolean equals(java.lang.Object obj)
          
 java.lang.String getCode()
          Helper method to generates the setter and getter methods of any configuration class.
 java.lang.String getDescription()
          The description of this configuration.
 java.lang.String getDoc()
           
 Configuration.Entry getEntry(java.lang.String key)
          Returns the entry with the given key.
 java.io.File getFile()
          Returns the location this configuration was last saved to or loaded from.
 java.lang.String getId()
          The id is used as the registration key if the configuration is registered with and application.
 java.lang.String getName()
          Returns the name of the configuration.
 java.lang.String getPropertyFileComment()
           
 int hashCode()
           
 void load()
          Load the configuration from its stored location.
 boolean needsSave()
          Indicates if this configuration needs to be saved.
 void overrideFromCommandLineParameters(com.martiansoftware.jsap.JSAPResult cliOptions)
          Overrides configuration entries using the values given on the command line.
 void putEntry(Configuration.Entry entry)
          Add a configuration entry.
 void save()
          Saves the configuration to a persistent storage.
 java.lang.String toString()
          
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

static org.apache.log4j.Logger logger
Log4J logger for this class


entries

java.util.Map<java.lang.String,Configuration.Entry> entries
Holds configuration entries


needsSave

boolean needsSave
Constructor Detail

ConfigurationImpl

public ConfigurationImpl(java.lang.String id,
                         java.lang.String name,
                         java.lang.String description,
                         java.io.File file)
Creates a new configuration with no entries.

Parameters:
name - the name of the new configuration. Must be unique among other configurations. Make sure the name is human-readable as it is possible that the name is displayed in a GUI.
description - A description of this configuration. Can be used to generate documentation or to proved online help in a GUI.
file - The file this configuration is persisted or loaded from using the load() and save() methods.
Method Detail

putEntry

public void putEntry(Configuration.Entry entry)
Add a configuration entry. The key of the entry itself is going to replaces by the one given here.

Parameters:
key - the key for the new entry.
entry - the configuration entry.

getEntry

public Configuration.Entry getEntry(java.lang.String key)
Returns the entry with the given key.

Specified by:
getEntry in interface Configuration
Parameters:
key - the key of the entry
Returns:
the configuration entry

containsEntry

public boolean containsEntry(java.lang.String key)
Returns whether this configuration contains an entry with the given key. This test can be performed to distinguish between a null and a non-existing entry.

Specified by:
containsEntry in interface Configuration
Parameters:
key - the key of the entry that shall be returned.
Returns:
True, if the entry exists, otherwise false.

load

public void load()
          throws java.io.IOException
Load the configuration from its stored location. Specific implementations of the Configuration class define how (e.g: database, file etc.) and from where (e.g: db table, file location etc..) the configuration is loaded. See the implementations of this interface for details.

Specified by:
load in interface Configuration
Throws:
java.io.IOException - Indicates an error while loading.

save

public void save()
          throws java.io.IOException
Description copied from interface: Configuration
Saves the configuration to a persistent storage. Only entries are saved that for which Configuration.Entry.isSet() returns true.

Specified by:
save in interface Configuration
Throws:
java.io.IOException - Indicates an error while saving.

entries

public Configuration.Entry[] entries()
Returns a safe array of all configuration entries of this configuration..

Specified by:
entries in interface Configuration
Returns:
an array containing all entries of this configuration.

getFile

public java.io.File getFile()
Returns the location this configuration was last saved to or loaded from.

Returns:
the location of the configuration. Returns null if this configuration was never saved or loaded.

overrideFromCommandLineParameters

public void overrideFromCommandLineParameters(com.martiansoftware.jsap.JSAPResult cliOptions)
Overrides configuration entries using the values given on the command line.

Parameters:
cliOptions - the command line options

getPropertyFileComment

public java.lang.String getPropertyFileComment()

toString

public java.lang.String toString()

Overrides:
toString in class java.lang.Object

getDoc

public java.lang.String getDoc()

getCode

public java.lang.String getCode()
Helper method to generates the setter and getter methods of any configuration class.

Returns:
Setter and getter methods (Java code) that can be used as type safe methods in subclasses.

getId

public java.lang.String getId()
Description copied from interface: Configuration
The id is used as the registration key if the configuration is registered with and application. Must be unique.

Specified by:
getId in interface Configuration
Returns:
the unique id of this configuration

getName

public java.lang.String getName()
Returns the name of the configuration. The name can be used in a GUI but needn't to be unique in a JVM.

Specified by:
getName in interface Configuration
Returns:
the name of this configuration

getDescription

public java.lang.String getDescription()
The description of this configuration. Suitable to be shown to a human.

Specified by:
getDescription in interface Configuration
Returns:
the description of this configuration

createEntry

public Configuration.Entry createEntry(java.lang.String key,
                                       java.lang.String defaultValue,
                                       java.lang.String entryDescription)

createIntEntry

public Configuration.Entry createIntEntry(java.lang.String key,
                                          int defaultValue,
                                          java.lang.String entryDescription)

createBooleanEntry

public Configuration.Entry createBooleanEntry(java.lang.String key,
                                              boolean defaultValue,
                                              java.lang.String entryDescription)

createLongEntry

public Configuration.Entry createLongEntry(java.lang.String key,
                                           long defaultValue,
                                           java.lang.String entryDescription)

createFileEntry

public Configuration.Entry createFileEntry(java.lang.String key,
                                           java.io.File defaultValue,
                                           java.lang.String entryDescription)

createEnumEntry

public Configuration.Entry createEnumEntry(java.lang.String key,
                                           java.lang.Enum defaultValue,
                                           java.lang.String entryDescription)

createEnumSetEntry

public <E extends java.lang.Enum<E>> Configuration.Entry createEnumSetEntry(java.lang.String key,
                                                                            java.util.EnumSet<E> defaultEnumSet,
                                                                            java.lang.Class elementType,
                                                                            java.lang.String entryDescription)

createFilelistEntry

public Configuration.Entry createFilelistEntry(java.lang.String key,
                                               java.util.List<java.io.File> defaultFileList,
                                               java.lang.String entryDescription)

equals

public boolean equals(java.lang.Object obj)

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

needsSave

public boolean needsSave()
Description copied from interface: Configuration
Indicates if this configuration needs to be saved. This is true if the configuration in memory is different to the persisted configuration.

Specified by:
needsSave in interface Configuration
Returns:
true if this configuration is different from its persisted form.