org.opensubsystems.core.util
Class Config

java.lang.Object
  extended by org.opensubsystems.core.util.Config

public class Config
extends java.lang.Object

Class responsible for reading and saving of configuration files.

Version:
$Id: Config.java,v 1.17 2007/01/07 06:14:00 bastafidli Exp $
Author:
Miro Halas
Code reviewer:
Miro Halas
Code reviewed:
1.9 2006/05/21 03:45:04 bastafidli

Field Summary
static java.lang.String CONFIG_FILE_NAME
          Name of the property which can specify the configuration file to use.
static java.lang.String DEFAULT_CONFIG_FILE_NAME
          Default file name of configuration file.
static java.lang.String DEPENDENT_CONFIG_FILE_NAME
          Name of the property which can specify the configuration file that a given configuration file depends on.
protected  java.util.Properties m_prpProperties
          Property file containing settings for this application This property is not transient since it represents values which needs to be stored on a disk.
protected  java.lang.String m_strConfigFile
          Name of the configuration file actually used.
protected  java.lang.String m_strMutex
          Helper mutex to synchronize some methods.
protected  java.lang.String m_strPropertyFileName
          Name of the property file containing application properties.
protected  java.net.URL m_urlDefaultPropertyFile
          If the property file name is not specified by s_strPropertyFileName, it can be specified by this URL as found on the system class path.
static java.lang.String VALUE_NO_FALSE
          This value can be used to signal no or false in the config file but in general we should test on TRUE and if it is not TRUE then it is FALSE.
static java.lang.String VALUE_NO_FALSE_BOOLEAN
          This value can be used to signal no or false in the config file but
static java.lang.String VALUE_YES_TRUE
          This value can be used to signal yes or true in the config file.
static java.lang.String VALUE_YES_TRUE_BOOLEAN
          This value can be used to signal yes or true in the config file.
 
Constructor Summary
Config()
          Constructor to construct accessor to default property file.
Config(java.util.Properties predefinedProperties)
          Public constructor to use specified properties
Config(java.lang.String strPropertyFileName)
          Public constructor to access specified property file.
 
Method Summary
protected  java.io.InputStream findConfigFile(java.lang.String strConfigFileName)
          Find and configuration file.
static boolean getBooleanProperty(java.util.Properties prpSettings, java.lang.String strProperty, boolean bDefaultValue, java.lang.String strDisplayName)
          Retrieve boolean property value and if the value is not specified throw an exception.
static boolean getBooleanProperty(java.util.Properties prpSettings, java.lang.String strProperty, java.lang.String strDisplayName)
          Retrieve boolean property value and if the value is not specified throw an exception.
static java.lang.String getBooleanPropertyAsString(java.util.Properties prpSettings, java.lang.String strProperty, boolean bDefaultValue, java.lang.String strDisplayName)
          Retrieve boolean property value and if the value is not specified throw an exception.
 java.lang.String getFullPropertyFileName()
          Get full location of the property file.
static Config getInstance()
          Get the default config instance.
static int getIntPropertyInRange(java.util.Properties prpSettings, java.lang.String strProperty, int iDefaultValue, java.lang.String strDisplayName, int iMinValue, int iMaxValue)
          Retrieve integer property value of which should existin within a specified range.
 java.util.Properties getProperties()
          Load all properties from default configuration file or return the ones passed into the constructor.
 java.util.Properties getPropertiesSafely()
          Load all properties from default configuration file or return the ones passed into the constructor.
 java.lang.String getPropertyFileName()
          Get the name of the propety file.
static java.lang.String getStringProperty(Config configFile, java.lang.String strProperty, java.lang.String strDisplayName)
          Retrieve string property value and if the value is not specified throw an exception.
static java.lang.String getStringProperty(java.util.Properties prpSettings, java.lang.String strProperty, java.lang.String strDisplayName)
          Retrieve string property value and if the value is not specified or it is empty throw an exception.
static java.lang.String getStringProperty(java.util.Properties prpSettings, java.lang.String strProperty, java.lang.String strDisplayName, boolean bAllowEmpty)
          Retrieve string property value and if the value is not specified throw an exception.
static java.lang.String getStringProperty(java.util.Properties prpSettings, java.lang.String strProperty, java.lang.String strDefaultValue, java.lang.String strDisplayName)
          Retrieve string property value and if the property is not specified or it is specified as an empty value, use the default value instead.
static java.lang.String getStringProperty(java.util.Properties prpSettings, java.lang.String strProperty, java.lang.String strDefaultValue, java.lang.String strDisplayName, boolean bAllowEmpty)
          Retrieve string property value.
static boolean isTrue(java.lang.String strValue)
          Test if specified value is true according to config file rules.
 void save()
          Save the configuration to a file.
static void save(java.io.File fileConfig, java.util.Properties prpSettings)
          Save the configuration to a file.
static void setInstance(Config defaultInstance)
          Set default instance.
 void setPropertyFileName(java.lang.String strPropertyFileName)
          Set the name of the propety file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONFIG_FILE_NAME

public static final java.lang.String CONFIG_FILE_NAME
Name of the property which can specify the configuration file to use.

See Also:
Constant Field Values

DEPENDENT_CONFIG_FILE_NAME

public static final java.lang.String DEPENDENT_CONFIG_FILE_NAME
Name of the property which can specify the configuration file that a given configuration file depends on. If this property is found in the file being loaded, the dependent file is loaded first. Then the properties loaded from the dependent file are replaced with the properties which were redefined in the current file.

See Also:
Constant Field Values

VALUE_YES_TRUE

public static final java.lang.String VALUE_YES_TRUE
This value can be used to signal yes or true in the config file. Use number since this way we can just use equals and not equalsIgnoreCase.

See Also:
Constant Field Values

VALUE_YES_TRUE_BOOLEAN

public static final java.lang.String VALUE_YES_TRUE_BOOLEAN
This value can be used to signal yes or true in the config file.


VALUE_NO_FALSE

public static final java.lang.String VALUE_NO_FALSE
This value can be used to signal no or false in the config file but in general we should test on TRUE and if it is not TRUE then it is FALSE. Use number since this way we can just use equals and not equalsIgnoreCase.

See Also:
Constant Field Values

VALUE_NO_FALSE_BOOLEAN

public static final java.lang.String VALUE_NO_FALSE_BOOLEAN
This value can be used to signal no or false in the config file but


DEFAULT_CONFIG_FILE_NAME

public static final java.lang.String DEFAULT_CONFIG_FILE_NAME
Default file name of configuration file.

See Also:
Constant Field Values

m_strMutex

protected java.lang.String m_strMutex
Helper mutex to synchronize some methods.


m_prpProperties

protected java.util.Properties m_prpProperties
Property file containing settings for this application This property is not transient since it represents values which needs to be stored on a disk.


m_strPropertyFileName

protected java.lang.String m_strPropertyFileName
Name of the property file containing application properties.


m_urlDefaultPropertyFile

protected java.net.URL m_urlDefaultPropertyFile
If the property file name is not specified by s_strPropertyFileName, it can be specified by this URL as found on the system class path.


m_strConfigFile

protected java.lang.String m_strConfigFile
Name of the configuration file actually used.

Constructor Detail

Config

public Config()
Constructor to construct accessor to default property file.


Config

public Config(java.lang.String strPropertyFileName)
Public constructor to access specified property file.

Parameters:
strPropertyFileName - - name of the property file to use.

Config

public Config(java.util.Properties predefinedProperties)
Public constructor to use specified properties

Parameters:
predefinedProperties - - predefined properties which should be used
Method Detail

getInstance

public static Config getInstance()
Get the default config instance.

Returns:
Config - actual instance

setInstance

public static void setInstance(Config defaultInstance)
Set default instance. This instance will be returned by getInstance method until it is changed.

Parameters:
defaultInstance - - new default Config instance
See Also:
getInstance()

getProperties

public java.util.Properties getProperties()
                                   throws java.io.IOException
Load all properties from default configuration file or return the ones passed into the constructor.

Returns:
Properties - set of properties for this application.
Throws:
java.io.IOException - - for example the file was not found

findConfigFile

protected java.io.InputStream findConfigFile(java.lang.String strConfigFileName)
                                      throws java.io.IOException,
                                             java.io.FileNotFoundException
Find and configuration file. The calling class is responsible for closing the input stream

Parameters:
strConfigFileName - - name of the configuration file
Returns:
InputStream - opened config file
Throws:
java.io.IOException - - an error has occured opening configuration file
java.io.FileNotFoundException - - file cannot be found

getPropertiesSafely

public java.util.Properties getPropertiesSafely()
Load all properties from default configuration file or return the ones passed into the constructor. If this file doesn't exists, a new one will be created and empty property list will be returned.

Returns:
Properties - set of properties for this application.

save

public void save()
          throws java.io.IOException,
                 java.io.FileNotFoundException
Save the configuration to a file.

Throws:
java.io.IOException - - there was a problem saving configuration file.
java.io.FileNotFoundException - - file cannot be found

save

public static void save(java.io.File fileConfig,
                        java.util.Properties prpSettings)
                 throws java.io.IOException,
                        java.io.FileNotFoundException
Save the configuration to a file.

Parameters:
fileConfig - - file to save the properties to
prpSettings - - configuration settings to store to the file
Throws:
java.io.IOException - - there was a problem saving configuration file.
java.io.FileNotFoundException - - file cannot be found

getPropertyFileName

public java.lang.String getPropertyFileName()
Get the name of the propety file.

Returns:
String - property file name or null if none set

getFullPropertyFileName

public java.lang.String getFullPropertyFileName()
Get full location of the property file. The file may be located in the file system or in jar file.

Returns:
String - full location of the property file including file name.

setPropertyFileName

public void setPropertyFileName(java.lang.String strPropertyFileName)
Set the name of the propety file.

Parameters:
strPropertyFileName - - the strPropertyFileName to set

isTrue

public static boolean isTrue(java.lang.String strValue)
Test if specified value is true according to config file rules.

Parameters:
strValue - - value to test
Returns:
boolean - true if the value specifies boolean true

getIntPropertyInRange

public static int getIntPropertyInRange(java.util.Properties prpSettings,
                                        java.lang.String strProperty,
                                        int iDefaultValue,
                                        java.lang.String strDisplayName,
                                        int iMinValue,
                                        int iMaxValue)
Retrieve integer property value of which should existin within a specified range.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
iDefaultValue - - default value to use if a valid value is not specified
strDisplayName - - user friendly name of the property
iMinValue - - inclusive minimal value of the range
iMaxValue - - inclusive maximal value of the range
Returns:
int - value of the property or default value if the value is not specified

getStringProperty

public static java.lang.String getStringProperty(Config configFile,
                                                 java.lang.String strProperty,
                                                 java.lang.String strDisplayName)
                                          throws OSSConfigException
Retrieve string property value and if the value is not specified throw an exception.

Parameters:
configFile - - configuration file to retrieve the setting from
strProperty - - name of the property to retrieve
strDisplayName - - user friendly name of the property
Returns:
String - value of the property or default value if the value is not specified
Throws:
OSSConfigException - - value for the requested property is not specified

getStringProperty

public static java.lang.String getStringProperty(java.util.Properties prpSettings,
                                                 java.lang.String strProperty,
                                                 java.lang.String strDisplayName)
                                          throws OSSConfigException
Retrieve string property value and if the value is not specified or it is empty throw an exception.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
strDisplayName - - user friendly name of the property
Returns:
String - value of the property
Throws:
OSSConfigException - - value for the requested property is not specified

getStringProperty

public static java.lang.String getStringProperty(java.util.Properties prpSettings,
                                                 java.lang.String strProperty,
                                                 java.lang.String strDisplayName,
                                                 boolean bAllowEmpty)
                                          throws OSSConfigException
Retrieve string property value and if the value is not specified throw an exception.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
strDisplayName - - user friendly name of the property
bAllowEmpty - - if true then empty value is allowed
Returns:
String - value of the property
Throws:
OSSConfigException - - value for the requested property is not specified

getStringProperty

public static java.lang.String getStringProperty(java.util.Properties prpSettings,
                                                 java.lang.String strProperty,
                                                 java.lang.String strDefaultValue,
                                                 java.lang.String strDisplayName)
Retrieve string property value and if the property is not specified or it is specified as an empty value, use the default value instead.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
strDefaultValue - - default value to use if a valid value is not specified
strDisplayName - - user friendly name of the property
Returns:
String - value of the property or default value if the value is not specified

getStringProperty

public static java.lang.String getStringProperty(java.util.Properties prpSettings,
                                                 java.lang.String strProperty,
                                                 java.lang.String strDefaultValue,
                                                 java.lang.String strDisplayName,
                                                 boolean bAllowEmpty)
Retrieve string property value.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
strDefaultValue - - default value to use if a valid value is not specified
strDisplayName - - user friendly name of the property
bAllowEmpty - - if true then empty value is allowed
Returns:
String - value of the property or default value if the value is not specified

getBooleanProperty

public static boolean getBooleanProperty(java.util.Properties prpSettings,
                                         java.lang.String strProperty,
                                         java.lang.String strDisplayName)
                                  throws OSSConfigException
Retrieve boolean property value and if the value is not specified throw an exception.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
strDisplayName - - user friendly name of the property
Returns:
boolean - value of the property
Throws:
OSSConfigException - - value for the requested property is not specified

getBooleanProperty

public static boolean getBooleanProperty(java.util.Properties prpSettings,
                                         java.lang.String strProperty,
                                         boolean bDefaultValue,
                                         java.lang.String strDisplayName)
Retrieve boolean property value and if the value is not specified throw an exception.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
bDefaultValue - - default value to use if a valid value is not specified
strDisplayName - - user friendly name of the property
Returns:
boolean - value of the property or default value if the value is not specified

getBooleanPropertyAsString

public static java.lang.String getBooleanPropertyAsString(java.util.Properties prpSettings,
                                                          java.lang.String strProperty,
                                                          boolean bDefaultValue,
                                                          java.lang.String strDisplayName)
Retrieve boolean property value and if the value is not specified throw an exception.

Parameters:
prpSettings - - properties to retrieve the setting from
strProperty - - name of the property to retrieve
bDefaultValue - - default value to use if a valid value is not specified
strDisplayName - - user friendly name of the property
Returns:
String - value of the property or default value if the value is not specified


Copyright © 2003 - 2006 OpenSubsystems s.r.o.