at.knowcenter.wag.egov.egiz.cfg
Class SettingsReader

java.lang.Object
  extended byat.knowcenter.wag.egov.egiz.cfg.SettingsReader
All Implemented Interfaces:
Serializable

public class SettingsReader
extends Object
implements Serializable

The SettingsReader reads the settings.txt file. The settings.txt is a simple java property file that collects all parameters used in different modules. The SettingsReader provides methods to get the property keys and the corresponding values. The keys could be defined as combinations of single keys. Therefore it is possible to combine differen classes of keys. An example could be:

     
                        #SettingNotFoundException
                        error.code.100=Interner Fehler
                        error.code.101=Die Konfigurationsdatei konnte nicht geladen werden
                        
                        #PDFDocumentException
                        error.code.200=Das Dokument konnte nicht geladen werden
                        
                        #SignatureException
                        error.code.300=Die Signatur ist ung�ltig
                        
                        #NormalizeException
                        error.code.400=Die angegebene Version ist nicht bekannt
                       
                        normalizer.version=V01

      
 
The internal representation of the example above is:
     

                        .error|
                              |.code|
                              |     |.200=Das Dokument konnte nicht geladen werden
                              |     |.100=Interner Fehler
                              |     |.400=Die angegebene Version ist nicht bekannt
                              |     |.101=Die Konfigurationsdatei konnte nicht geladen werden
                              |     |.300=Die Signatur ist ung�ltig
                       .normalizer|
                                  |.version=V01      
      
 

Author:
wlackner
See Also:
Serialized Form

Field Summary
static String CERT_PATH
          The path of the certificated directory.
static String CONFIG_FILE_DEFAULT_NAME
          The name of the default configuration file.
static String CONFIG_PATH
          The path of the configuration directory.
static String RESOURCES_PATH
          The path of the resources repository.
static String TMP_PATH
          The path for temporary files.
 
Method Summary
protected static File assembleTemporaryDirectoryFile()
          Assembles the File of the temporary directory without checking if it really exists.
static void clearTemporaryDirectory()
          Deletes all files in the temporary directory, if it exists.
static void createInstance()
          Reloads the Settings file.
static SettingsReader getInstance()
          This method returns an synchronized instance of this class.
 InputStream getInternalResource(String relativePath)
          Get resource as stream, relative to internal resource path {@value}
 int getIntSetting(String key, int defaultValue)
          If a property value is number (interger) this method extracts the value and convert it to an int.
 ArrayList getKeys(String key)
          This method returns an array of sub keys (children references) of the key.
 PropertyTree getPTree()
          This method returns the PropertyTree representation of the configuration file.
 String getSetting(String key)
          This method returns a property value to the corresponding key.
 String getSetting(String key, String defaultValue)
          This method returns a property value to the corresponding key.
 String getSetting(String primaryKey, String defaultKey, String defaultValue)
          This method returns a property value to the corresponding key.
 Vector getSettingKeys(String keyPrefix)
          This method returns an array of keys in the same hierarchy of the keyPrefix.
static File getTemporaryDirectory()
          Returns the directory where temporary files should be stored.
 String getValueFromKey(String key)
          This method returns a the first value from a key.
static void initialize(String base_dir)
           
static void initialize(String configdir, String tmpdir)
           
static void initializeForCommandLine()
          Initializes the paths of the SettingsReader for commanline usage.
static void initializeForWeb(String base_dir)
          Initializes the paths of the SettingsReader for web application usage.
 String readInternalResourceAsString(String relativePath)
          Reads internal resource as string.
static String relocateFile(String file)
          Relocates the relative file.
 void setSetting(String key, String value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOURCES_PATH

public static String RESOURCES_PATH
The path of the resources repository.

This usually contains sub directories for the templates, the configuration files, etc.


TMP_PATH

public static String TMP_PATH
The path for temporary files.


CONFIG_PATH

public static String CONFIG_PATH
The path of the configuration directory.


CERT_PATH

public static String CERT_PATH
The path of the certificated directory.


CONFIG_FILE_DEFAULT_NAME

public static final String CONFIG_FILE_DEFAULT_NAME
The name of the default configuration file. The definition syntax is the java property config syntax.

See Also:
Constant Field Values
Method Detail

getInstance

public static SettingsReader getInstance()
                                  throws SettingsException
This method returns an synchronized instance of this class. The settings file is read only once using this class. This method returns the instance holding the definitions of the default settings file. Default file: CONFIG_FILE_DEFAULT_NAME: "settings.txt"

Returns:
an instance of the SettingsReader
Throws:
SettingsException - if the default settings file could not be read

createInstance

public static void createInstance()
                           throws SettingsException
Reloads the Settings file.

Subsequent calls to getInstance will return the new settings.

Throws:
SettingsException - f.e.

getSetting

public String getSetting(String key)
                  throws SettingNotFoundException
This method returns a property value to the corresponding key. If the key is not found in the property file a SettingNotFoundException is thrown.

Parameters:
key - get the value for that key in the property file
Returns:
the value of the property key.
Throws:
SettingNotFoundException - ErrorCode: 100

setSetting

public void setSetting(String key,
                       String value)

relocateFile

public static String relocateFile(String file)
Relocates the relative file.

Parameters:
file - The relative file.
Returns:
Returns the usable file.

getSetting

public String getSetting(String key,
                         String defaultValue)
This method returns a property value to the corresponding key. If the key is not found in the property file the input param defaultValue is returned.

Parameters:
key - get the value for that key in the property file
defaultValue - the default value if the key is not found
Returns:
the value of the property key

getSetting

public String getSetting(String primaryKey,
                         String defaultKey,
                         String defaultValue)
This method returns a property value to the corresponding key. If the key is not found in the property file the input param defaultKey is searched. If the default key is not found the input param defaultValue is returned.

Parameters:
primaryKey - get the value for that key in the property file
defaultKey - the default key that should be searched if the primaryKey is not found
defaultValue - the default value if the defaultKey is not found
Returns:
the value of the property key

getSettingKeys

public Vector getSettingKeys(String keyPrefix)
This method returns an array of keys in the same hierarchy of the keyPrefix. The method search all keys in the property file that has the keyPrefix as leading substring. The Object[] collects all sub keys without the keyPrefix.

Parameters:
keyPrefix - to search for sub keys
Returns:
alls keys starting with the keyPrefix

getIntSetting

public int getIntSetting(String key,
                         int defaultValue)
If a property value is number (interger) this method extracts the value and convert it to an int. If the key ist not found or the conversion fails, the defaultValue is returned.

Parameters:
key - get the value for that key in the property file
defaultValue - the default value if the key is not found
Returns:
the int value of the property key

getKeys

public ArrayList getKeys(String key)
This method returns an array of sub keys (children references) of the key. The method is a wrapper calling the method PropertyTree.getKeys(String key).

Parameters:
key - get all sub keys for that key in the property file
Returns:
an list of sub keys (type String)
See Also:
PropertyTree

getValueFromKey

public String getValueFromKey(String key)
This method returns a the first value from a key. This means the method search in the PropertyTree representation of the config file. The PropertyTree class can overload key value paires. But the config file can not overload keys. If a key is defined more than one times the last definition is stored it the property list. The method is a wrapper calling the method PropertyTree.getFirstValue(String key).

Parameters:
key - get the value for that key in the property file
Returns:
the value of the property key
See Also:
PropertyTree

getPTree

public PropertyTree getPTree()
This method returns the PropertyTree representation of the configuration file.

Returns:
Returns the pTree.
See Also:
PropertyTree

readInternalResourceAsString

public String readInternalResourceAsString(String relativePath)
Reads internal resource as string.

Parameters:
relativePath -
Returns:
null in case of error

getInternalResource

public InputStream getInternalResource(String relativePath)
Get resource as stream, relative to internal resource path {@value}

Parameters:
relativePath -
Returns:

assembleTemporaryDirectoryFile

protected static File assembleTemporaryDirectoryFile()
Assembles the File of the temporary directory without checking if it really exists.

See Also:
TempDirHelper.assembleTemporaryDirectoryFile()

getTemporaryDirectory

public static File getTemporaryDirectory()
Returns the directory where temporary files should be stored.

If the directory doesn't exist, it is created.

Returns:
Returns the directory where temporary files should be stored.
See Also:
TempDirHelper.getTemporaryDirectory()

clearTemporaryDirectory

public static void clearTemporaryDirectory()
Deletes all files in the temporary directory, if it exists.

This should be used to clear temporary files when the application shuts down.

See Also:
TempDirHelper.clearTemporaryDirectory()

initialize

public static void initialize(String configdir,
                              String tmpdir)

initialize

public static void initialize(String base_dir)

initializeForWeb

public static void initializeForWeb(String base_dir)
Initializes the paths of the SettingsReader for web application usage.

Parameters:
base_dir - The base directory of this web application. E.g. TOMCAT_HOME/webapps/pdf-as

initializeForCommandLine

public static void initializeForCommandLine()
Initializes the paths of the SettingsReader for commanline usage.



Copyright © 2006-2010. All Rights Reserved.