net.sf.xbus.base.core.config
Class Configuration

java.lang.Object
  extended by net.sf.xbus.base.core.config.Configuration

public class Configuration
extends java.lang.Object

This class reads program parameters from different sources and makes them accessible. The entries of the configuration are organized in a three-level hierarchie:

The Configuration implements the Singleton Design-Pattern.

Up to now, only the Java- Properties can be used as a source for the configuration. Other sources like XML-files or a database are prepared.


Field Summary
static java.lang.String MAPPING_DEFAULT
           
static java.lang.String STANDARD_CONFIG
           
static java.lang.String VARIABLE_END
           
static java.lang.String VARIABLE_PREFIX
           
 
Method Summary
 java.util.List getChapters()
          Returns a list of all chapters.
static java.lang.String getClass(java.lang.String type, java.lang.String name)
          Returns an entry of the class table.
static Configuration getInstance()
          Returns an instance of the standard Configuration.
static Configuration getInstance(java.lang.String source)
          Returns a named instance of the Configuration.
 java.util.Map getKeysAndValues(java.lang.String chapter, java.lang.String section)
          Returns a map of all keys and their values for the given chapter and section.
static java.lang.String getMapping(java.lang.String section, java.lang.String key)
          Returns an entry of the mapping table.
static java.lang.String getMappingDefault(java.lang.String section)
           
static java.lang.String getMappingOptional(java.lang.String section, java.lang.String key)
          Returns an entry of the mapping table or null if it doesn't exist.
 java.util.List getSections(java.lang.String chapter)
          Returns a list of all sections for the given chapter
 java.lang.String getValue(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the value of an entry.
 boolean getValueAsBoolean(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the value of an entry as a boolean.
 boolean getValueAsBooleanOptional(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the optional value of an entry as a boolean.
 int getValueAsInt(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the value of an entry as an integer.
 int getValueAsIntOptional(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the optional value of an entry as an integer.
 long getValueAsLongOptional(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the optional value of an entry as an long value.
 java.lang.String getValueOptional(java.lang.String chapter, java.lang.String section, java.lang.String key)
          Returns the value of an entry.
static void refresh()
          Creates an updated instance of the standard Configuration.
static void refresh(java.lang.String source)
          Creates an updated instance of the named Configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STANDARD_CONFIG

public static final java.lang.String STANDARD_CONFIG
See Also:
Constant Field Values

VARIABLE_PREFIX

public static final java.lang.String VARIABLE_PREFIX
See Also:
Constant Field Values

VARIABLE_END

public static final java.lang.String VARIABLE_END
See Also:
Constant Field Values

MAPPING_DEFAULT

public static final java.lang.String MAPPING_DEFAULT
See Also:
Constant Field Values
Method Detail

getInstance

public static Configuration getInstance()
                                 throws XException
Returns an instance of the standard Configuration.

The first call creates a new Configuration and reads all entries. Following calls will return the object, that has been created by the first call.

Returns:
Configuration - instance of the standard Configuration
Throws:
XException - if any error occurs

getInstance

public static Configuration getInstance(java.lang.String source)
                                 throws XException
Returns a named instance of the Configuration.

The first call creates a new Configuration and reads all entries. Following calls will return the object, that has been created by the first call.

Parameters:
source - the source of the properties.
Returns:
Configuration - instance of the standard Configuration
Throws:
XException - if any error occurs

refresh

public static void refresh()
                    throws XException
Creates an updated instance of the standard Configuration.

Throws:
XException - if any error occurs

refresh

public static void refresh(java.lang.String source)
                    throws XException
Creates an updated instance of the named Configuration.

Parameters:
source - the source of the properties
Throws:
XException - if any error occurs

getValue

public java.lang.String getValue(java.lang.String chapter,
                                 java.lang.String section,
                                 java.lang.String key)
                          throws XException
Returns the value of an entry. All three parameters have to contain values.

If the desired entry can not be found, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as String for the given chapter, section and key
Throws:
XException - if the entry in configuration is not exist or any error occurs

getValueAsInt

public int getValueAsInt(java.lang.String chapter,
                         java.lang.String section,
                         java.lang.String key)
                  throws XException
Returns the value of an entry as an integer. All three parameters have to contain values.

If the desired entry can not be found or the value is not an integer, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as int for the given chapter, section and key *
Throws:
XException - if the entry in configuratiuon is not exist or it is not an integer

getValueAsIntOptional

public int getValueAsIntOptional(java.lang.String chapter,
                                 java.lang.String section,
                                 java.lang.String key)
                          throws XException
Returns the optional value of an entry as an integer. All three parameters have to contain values.

If the desired entry is not an integer, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as int for the given chapter, section and key
If the entry in the configuration is not found, 0 is returned.
Throws:
XException - if the entry is not an integer

getValueAsLongOptional

public long getValueAsLongOptional(java.lang.String chapter,
                                   java.lang.String section,
                                   java.lang.String key)
                            throws XException
Returns the optional value of an entry as an long value. All three parameters have to contain values.

If the desired entry is not an long value, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as int for the given chapter, section and key
If the entry in the configuration is not found, 0 is returned.
Throws:
XException - if the entry is not an integer

getValueAsBoolean

public boolean getValueAsBoolean(java.lang.String chapter,
                                 java.lang.String section,
                                 java.lang.String key)
                          throws XException
Returns the value of an entry as a boolean. All three parameters have to contain values.

If the desired entry can not be found or the value is not a boolean, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as boolean for the given chapter, section and key
Throws:
XException - if the entry in the configuration is not exist or it is not a boolean value

getValueAsBooleanOptional

public boolean getValueAsBooleanOptional(java.lang.String chapter,
                                         java.lang.String section,
                                         java.lang.String key)
                                  throws XException
Returns the optional value of an entry as a boolean. All three parameters have to contain values.

If the desired entry can not be found, false is returned. If the value is not a boolean, a XException is thrown.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as boolean for the given chapter, section and key.
If the entry in the configuration is not found, false is returned.
Throws:
XException - if the entry is not a boolean value or any error occurs

getValueOptional

public java.lang.String getValueOptional(java.lang.String chapter,
                                         java.lang.String section,
                                         java.lang.String key)
Returns the value of an entry. All three parameters have to contain values.

If the desired entry is not in the configuration, null will be returned.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
key - The bottom level from the three-level hierarchie hashtable that belongs to these chapter and section
Returns:
Value as string for the given chapter,section and key or null if the desired value is not found in the configuration

getChapters

public java.util.List getChapters()
Returns a list of all chapters.

Returns:
List with chapters
Throws:
XException - if any error occurs

getSections

public java.util.List getSections(java.lang.String chapter)
Returns a list of all sections for the given chapter

Parameters:
chapter - The first level from the three-level hierarchie hashtable
Returns:
List of all sections for the given chapter or null wenn no sections are found
Throws:
XException - if any error occurs

getKeysAndValues

public java.util.Map getKeysAndValues(java.lang.String chapter,
                                      java.lang.String section)
Returns a map of all keys and their values for the given chapter and section.

Parameters:
chapter - The first level from the three-level hierarchie hashtable
section - The second level from the three-level hierarchie hashtable that belongs to this chapter
Returns:
Map of all keys and their values for the given chapter and section or null wenn no keys are found
Throws:
XException - if any error occurs

getClass

public static java.lang.String getClass(java.lang.String type,
                                        java.lang.String name)
                                 throws XException
Returns an entry of the class table.

Parameters:
type - The second level from the three-level hierarchie hashtable that belongs to the chapter: Class
name - The bottom level from the three-level hierarchie hashtable that belongs to the chapter: Class and this section
Returns:
Value as String for the given type and name
Throws:
XException - if the entry is not found or any error occurs
See Also:
getValue(String, String, String)

getMapping

public static java.lang.String getMapping(java.lang.String section,
                                          java.lang.String key)
                                   throws XException
Returns an entry of the mapping table.

Parameters:
section - The second level from the three-level hierarchie hashtable that belongs to the chapter: Mapping
key - The bottom level from the three-level hierarchie hashtable that belongs to the chapter: Mapping and this section
Returns:
Value for the given section and key
Throws:
XException - if the entry is not found or any error occurs
See Also:
getValue(String, String, String)

getMappingOptional

public static java.lang.String getMappingOptional(java.lang.String section,
                                                  java.lang.String key)
                                           throws XException
Returns an entry of the mapping table or null if it doesn't exist.

Parameters:
section - The second level from the three-level hierarchie hashtable that belongs to the chapter: Mapping
key - The bottom level from the three-level hierarchie hashtable that belongs to the chapter: Mapping and this section
Returns:
value for the given section and key
Throws:
XException - if the entry is not found or any error occurs

getMappingDefault

public static java.lang.String getMappingDefault(java.lang.String section)
                                          throws XException
Parameters:
section -
Throws:
XException