com.tenduke.utils
Class Configuration

java.lang.Object
  extended by com.tenduke.utils.PropertyBasedConfigurationProvider
      extended by com.tenduke.utils.Configuration
All Implemented Interfaces:
ConfigurationProvider

public final class Configuration
extends PropertyBasedConfigurationProvider

Configuration is a singleton class that provides configuration initialization and access to configuration parameters. The singleton instance of the class is intended to represent application main configuration. PropertyBasedConfigurationProvider, which this class extends, can be used for other contexts where configuration needs to be handled using similar interface.

Configuration uses internally Properties, and by default configuration is stored in file where Properties are represented in XML format defined by java.util.Properties.

Configuration allows user to define the file from which configuration is loaded. Configuration file path is resolved in the following order:

  1. java.lang.System.getProperty("10duke.configuration.path"), i.e. JVM parameter named "10duke.configuration.path"
  2. ./conf/conf.xml, i.e. default configuration path relative to process execution directory

Use instance() method to access the singleton Configuration. Methods of this instance (PropertyBasedConfigurationProvider.getString(String), PropertyBasedConfigurationProvider.getInt(String), PropertyBasedConfigurationProvider.getBoolean(String) etc.) provide typed access to the configuration parameters.

Table of configuration keys defined by this class.

Key nameDescriptionDefault value
system.default.locale System default locale. If the key is undefined or empty, then the default locale is obtained with java.util.Locale.getDefault() method. java.util.Locale.getDefault()
default.tmp.dir Directory for temporary files. ./tmp

JVM system properties used in this class are described in the following table:

System propertyDescriptionDefault value
10duke.configuration.main Main configuration file url. If relative url is given, it is resolved as being relative to JVM process execution directory. ./conf/conf.xml


Field Summary
static java.net.URI DEFAULT_CONFIGURATION_MAIN
          The default main configuration name URI, if the JVM system property KEY_CONFIGURATION_MAIN does not exist or contains an invalid value.
static java.lang.String DEFAULT_TEMP_DIR
          Default temporary directory used if no value for DEFAULT_TEMP_DIRECTORY_CONFIGURATION_KEY is found.
static java.lang.String DEFAULT_TEMP_DIRECTORY_CONFIGURATION_KEY
          Configuration key for default temporary directory.
static java.lang.String KEY_CONFIGURATION_MAIN
          Name of a JVM system property whose value defines the main configuration name.
 
Fields inherited from class com.tenduke.utils.PropertyBasedConfigurationProvider
PROPERTIES_DOC_TYPE, VARIABLES_URL_PARAMETER_NAME
 
Method Summary
static java.net.URI getConfigurationUrl()
          Gets url of the primary configuration file.
static java.net.URI getConfigurationUrlWithoutFilename()
          Gets url of the primary configuration file without file name part.
 java.util.Locale getSystemDefaultLocale()
          Get system default locale.
 java.lang.String getTemporaryDirectory(java.lang.String name)
          Gets a named temporary directory as a string.
 void initializeConfiguration()
           Initializes configuration.
 void initializeConfiguration(java.io.File confFile)
           Initializes configuration from the specified file.
 void initializeConfiguration(java.lang.String configPath)
           Initializes the main configuration from the given path.
 void initializeConfiguration(java.net.URI configUri)
           Initializes the main configuration from the given URI.
 void initializeConfiguration(java.net.URL urlConfig)
           Initializes configuration from the specified URL.
static Configuration instance()
          Singleton instance of the Configuration class.
 void logLocationInformation()
           Log useful information about paths.
protected  void parseConfiguration(java.io.InputStream inputStream, java.net.URI baseUrl)
          Parses the main configuration and (re)initializes ConsoleLogWriter settings.
protected  void parseConfiguration(java.lang.String configPath)
          Parses the main configuration and (re)initializes ConsoleLogWriter settings.
protected  void parseConfiguration(java.net.URI configUri)
          Parses the main configuration and (re)initializes ConsoleLogWriter settings.
protected  void parseConfiguration(java.net.URL urlConfig)
          Parses the main configuration and (re)initializes ConsoleLogWriter settings.
static java.net.URI resolveAndNormalize(java.lang.String relative)
           Given a relative url String, resolves that relative to the location of primary configuration (see getConfigurationUrl()).
static java.net.URI resolveAndNormalize(java.net.URI relative)
           Given a URI, resolves that relative to the location of primary configuration (see getConfigurationUrl()).
static void setConfigurationUrl(java.net.URI configurationUrl)
          Sets url locating the primary configuration file.
 
Methods inherited from class com.tenduke.utils.PropertyBasedConfigurationProvider
getBoolean, getByte, getConfigurationParsed, getConfigUri, getDouble, getFloat, getInt, getLong, getPath, getPath, getProperties, getShort, getString, getStrings, getUnderlyingProperties, getUrlConfig, getUUID, getValue, getValue, getValueInputStream, getValueOutputStream, getVariables, getVariablesUrl, getXmlElement, loadPropertiesFromXml, loadPropertiesFromXml, loadPropertiesFromXml, loadPropertiesFromXml, newConfiguration, newConfiguration, newConfiguration, newConfiguration, newConfiguration, newConfigurationFromProperties, newEmptyConfiguration, refreshConfiguration, setConfigurationParsed, setConfigUri, setUnderlyingProperties, setUrlConfig, setValue, storePersistently, validateAndFixDocType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CONFIGURATION_MAIN

public static final java.net.URI DEFAULT_CONFIGURATION_MAIN
The default main configuration name URI, if the JVM system property KEY_CONFIGURATION_MAIN does not exist or contains an invalid value. The default main URI is conf.xml


DEFAULT_TEMP_DIR

public static final java.lang.String DEFAULT_TEMP_DIR
Default temporary directory used if no value for DEFAULT_TEMP_DIRECTORY_CONFIGURATION_KEY is found.

See Also:
Constant Field Values

DEFAULT_TEMP_DIRECTORY_CONFIGURATION_KEY

public static final java.lang.String DEFAULT_TEMP_DIRECTORY_CONFIGURATION_KEY
Configuration key for default temporary directory. The key is "default.tmp.dir". If no value for this key is defined, "./tmp" is used.

See Also:
Constant Field Values

KEY_CONFIGURATION_MAIN

public static final java.lang.String KEY_CONFIGURATION_MAIN
Name of a JVM system property whose value defines the main configuration name. The property name is 10duke.configuration.main

See Also:
Constant Field Values
Method Detail

getConfigurationUrl

public static java.net.URI getConfigurationUrl()
                                        throws java.net.URISyntaxException
Gets url of the primary configuration file. If the primary configuration url has not been set before, then a JVM system property KEY_CONFIGURATION_MAIN is looked for. It the property does not exist, then DEFAULT_CONFIGURATION_MAIN is used as the primary configuration url.

Returns:
Url used to locate the primary configuration file. For technical reasons the returned value is of URI type, but it should always locate, not only identify, the configuration file.
Throws:
java.net.URISyntaxException - If the JVM system property cannot be converted to URI

getConfigurationUrlWithoutFilename

public static java.net.URI getConfigurationUrlWithoutFilename()
                                                       throws java.net.URISyntaxException
Gets url of the primary configuration file without file name part. The returned URI object can be thought to represent directory of the primary configuration file.

Returns:
URI object that contains getConfigurationUrl() without filename part.
Throws:
java.net.URISyntaxException - Thrown if configured primary configuration url is invalid, or if resolved url without filename is invalid.

getSystemDefaultLocale

public java.util.Locale getSystemDefaultLocale()
Get system default locale. System default locale can be configured using "system.default.locale" configuration parameter. If this configuration parameter is not set, Locale.getDefault() is used.

Returns:
Default locale of the system.

getTemporaryDirectory

public java.lang.String getTemporaryDirectory(java.lang.String name)
Gets a named temporary directory as a string. If named temporary directory configuration entry is not defined, default temporary directory is returned. Default temporary directory can be configured using "default.tmp.dir" configuration parameter. If this configuration parameter is not set, "./tmp" that is relative to application execution directory is used.

Specified by:
getTemporaryDirectory in interface ConfigurationProvider
Overrides:
getTemporaryDirectory in class PropertyBasedConfigurationProvider
Parameters:
name - Configuration entry name.
Returns:
Configured temporary directory value, or default temporary directory value if specific directory not configured.

initializeConfiguration

public void initializeConfiguration()
                             throws java.net.URISyntaxException,
                                    java.io.IOException

Initializes configuration.

After calling this thread safe method the configuration parameters can be accessed via methods of Configuration.instance().

Throws:
java.net.URISyntaxException - If the JVM system property Configuration.KEY_CONFIGURATION_MAIN is defined, but contains an invalid URI
java.io.IOException - If I/O error occurred or if a scheme defined by an URI is not supported

initializeConfiguration

public void initializeConfiguration(java.io.File confFile)
                             throws java.io.IOException,
                                    java.net.URISyntaxException

Initializes configuration from the specified file.

After calling this thread safe method the configuration parameters can be accessed via methods of Configuration.instance().

Parameters:
confFile - The configuration file.
Throws:
java.io.IOException - If I/O error occurred
java.net.URISyntaxException - Thrown if path of the given confFile can not be converted to URI.

initializeConfiguration

public void initializeConfiguration(java.lang.String configPath)
                             throws java.io.IOException,
                                    java.net.URISyntaxException

Initializes the main configuration from the given path.

After calling this thread safe method the configuration parameters can be accessed via methods of Configuration.instance().

Parameters:
configPath - Path to the main configuration
Throws:
java.io.IOException - if I/O error occurred or if the scheme defined by configPath is not supported
java.net.URISyntaxException - If configPath is not valid URI

initializeConfiguration

public void initializeConfiguration(java.net.URI configUri)
                             throws java.io.IOException

Initializes the main configuration from the given URI.

After calling this thread safe method the configuration parameters can be accessed via methods of Configuration.instance().

Parameters:
configUri - URI to the main configuration
Throws:
java.io.IOException - If I/O error occurred or if the scheme defined by configPath is not supported

initializeConfiguration

public void initializeConfiguration(java.net.URL urlConfig)
                             throws java.io.IOException,
                                    java.net.URISyntaxException

Initializes configuration from the specified URL.

After calling this thread safe method the configuration parameters can be accessed via methods of Configuration.instance().

Parameters:
urlConfig - The configuration URL.
Throws:
java.io.IOException - If an error occurred when opening a stream from the URL
java.net.URISyntaxException - Thrown if the given URL can not be converted to URI.

instance

public static Configuration instance()
Singleton instance of the Configuration class.

Returns:
Singleton instance of the Configuration class.

logLocationInformation

public void logLocationInformation()

Log useful information about paths.


parseConfiguration

protected void parseConfiguration(java.io.InputStream inputStream,
                                  java.net.URI baseUrl)
                           throws java.io.IOException
Parses the main configuration and (re)initializes ConsoleLogWriter settings.

Overrides:
parseConfiguration in class PropertyBasedConfigurationProvider
Parameters:
inputStream - Input stream to the main configuration
baseUrl - Base url used to resolve relative urls of documents included by the document read using the inputStream.
Throws:
java.io.IOException - If I/O error occurred

parseConfiguration

protected void parseConfiguration(java.lang.String configPath)
                           throws java.io.IOException,
                                  java.net.URISyntaxException
Parses the main configuration and (re)initializes ConsoleLogWriter settings.

Overrides:
parseConfiguration in class PropertyBasedConfigurationProvider
Parameters:
configPath - Path to the main configuration
Throws:
java.io.IOException - If I/O error occurred
java.net.URISyntaxException - If configPath is not valid URI

parseConfiguration

protected void parseConfiguration(java.net.URI configUri)
                           throws java.io.IOException
Parses the main configuration and (re)initializes ConsoleLogWriter settings.

Overrides:
parseConfiguration in class PropertyBasedConfigurationProvider
Parameters:
configUri - URI to the main configuration
Throws:
java.io.IOException - If I/O error occurred

parseConfiguration

protected void parseConfiguration(java.net.URL urlConfig)
                           throws java.io.IOException,
                                  java.net.URISyntaxException
Parses the main configuration and (re)initializes ConsoleLogWriter settings.

Overrides:
parseConfiguration in class PropertyBasedConfigurationProvider
Parameters:
urlConfig - URL to the main configuration
Throws:
java.io.IOException - If I/O error occurred
java.net.URISyntaxException - Thrown if the given URL can not be converted to URI.

resolveAndNormalize

public static java.net.URI resolveAndNormalize(java.lang.String relative)
                                        throws java.net.URISyntaxException

Given a relative url String, resolves that relative to the location of primary configuration (see getConfigurationUrl()). This method uses the resolve(URI uri) method from Java URI class. Using an absolute URI in relative resolves to absolute URI.

Parameters:
relative - URI relative to the primary configuration location (see getConfigurationUrl()).
Returns:
Resolved URI
Throws:
java.net.URISyntaxException - If the configuration url was not correly set up or if the relative string represents an invalid URI

resolveAndNormalize

public static java.net.URI resolveAndNormalize(java.net.URI relative)
                                        throws java.net.URISyntaxException

Given a URI, resolves that relative to the location of primary configuration (see getConfigurationUrl()).

This method uses the resolve(URI uri) method from Java URI class except that the root URI scheme is ignored when doing the resolving. This allows using scheme and relative path in the primary configuration url, e.g., resolving a URI myConf.xml against primary configuration url file:conf/conf.xml resolves to file:conf/myConf.xml. By URI standard the file:conf/conf.xml is opaque and thus myConf.xml would be the resolving result if the scheme would not be ignored.

Using an absolute URI, i.e. URI with scheme, in relative returns the original relative (but new object).

Parameters:
relative - URI relative to the primary configuration location (see getConfigurationUrl()).
Returns:
Resolved URI
Throws:
java.net.URISyntaxException - If the primary configuration url was not correctly set up

setConfigurationUrl

public static void setConfigurationUrl(java.net.URI configurationUrl)
Sets url locating the primary configuration file.

Parameters:
configurationUrl - Primary configuration url as a URI. URI is used for techical reasons. However, the given URI should always locate, not only identify, the configuration file.