nl.knowledgeplaza.util
Class ConfigurationProperties

java.lang.Object
  extended by nl.knowledgeplaza.util.ConfigurationProperties

public class ConfigurationProperties
extends java.lang.Object

This class is a wrapper for context dependent property collections.

Explanation

This class has a logic where configuration settings can be split up into context dependant property files, for example a database connection configuration which is different on each computer the application runs on.

This class searches for the following files:
- A resource specified explicitely via the "-Dorg.tbee.util.ConfigurationProperties=" commandline option
or though classloaders:
- configuration.properties, this file contains totally context independant settings
- configuration_<systemname>.properties, this file contains settings specifics for this computer
- configuration_<username>.properties, this file contains settings specifics for the user
- configuration_<systemname>_<username>.properties, this file contains settings specifics for the user on this computer
- configuration_<applicationname>.properties, this file contains settings specific for this application
- configuration_<applicationname>_<systemname>.properties, this file contains settings specifics for the application on this computer
- configuration_<applicationname>_<username>.properties, this file contains settings specifics for the application for the user
- configuration_<applicationname>_<systemname>_<username>.properties, this file contains settings specifics for the application for the user on this computer
- configuration#<key1>=<value1>#<key2>=<value2>#<key3>=<value3>#.properties, this file contains a ordered sequence of key=value pairs (look at the log4j debug output to see examples).
If a file is not found, it simply is ignored.
Solving a property is done in reverse order of the list above.
Per default each file gets the prefix "configuration", but it might be preferable to use the applicationname as the prefix (instead of using it as a filename attribute).

If the values are not set explicitly using the constructor, they are filled with defaults:
- application name = system property "application.name".
- system name = system property "system.name".
- user name = system property "user.name".

Using the map based constructor any number of additional key/value pairs can be added, like:
- web application context (servlet.getContextPath())
- application subsections
The order of the pairs is always sorted on key, and any permutation within this sorted order is checked. So for example:
#A=a#
#B=b#
#C=c#
#A=a#B=b#
#A=a#C=c#
#B=b#C=c#
#A=a#B=b#C=c#
but always sorted.
A similar writing style is also possible for conditional properties, for example:
host[system|production]=192.168...
LookAndFeel[user|pete]=metal
homedir[system|production][user|pete]=\tmp\here
For more information see the Properties class in the same package.

The application.name property and the system.name property are not automatically provided by Java, user.name is.

Typically the application name will be set within the application, using either the constructor or the setter.
The system name is provided via the "-Dsystem.name=mycomputer" on the java command line. (Also see next paragraph).
The user name is present automatically.
Note: when the filesystem is case sensistive; all are written in lowercase.

Coming back to the system.name system property. Since this property is not provided by the System class, each start of java must be altered to provide the property via "-D". This is quite a hassle. To make it less of a hassle the System utility class is created. It has a static getSystemProperties and getSystemProperty method which add to the regular system properties those that are defined in a file called "system.properties" in the "java.home" directory.

It is possible to instruct the ConfigurationProperties to check all files if they have changed, and if so it will reperform a read (complete with file determination)! Also after the new properties have been read, change listeners are notified.

It is possible to add a Properties object which is always scanned before all others.

It is possible to chain ConfigurationProperties so different prefixes can be used as one.

Version:
$Revision: 1.13 $
Author:
$Author: toeukpap $

Nested Class Summary
static class ConfigurationProperties.ConfigurationChangedEvent
           
static interface ConfigurationProperties.ConfigurationChangedListener
           
 
Field Summary
static java.lang.String CONFIGURATIONPROPERTY_ID_APPLICATIONNAME
           
static java.lang.String CONFIGURATIONPROPERTY_ID_HOST
           
static java.lang.String CONFIGURATIONPROPERTY_ID_OSNAME
           
static java.lang.String CONFIGURATIONPROPERTY_ID_PORT
           
static java.lang.String CONFIGURATIONPROPERTY_ID_SYSTEMNAME
           
static java.lang.String CONFIGURATIONPROPERTY_ID_USERNAME
           
static java.lang.String SOURCECODE_VERSION
          Standard variable for determining version of a class file.
 
Constructor Summary
ConfigurationProperties()
          get all the settings from the system properties
ConfigurationProperties(java.io.File... files)
          get all the settings from the system properties then override with provided identifiers
ConfigurationProperties(java.util.Map identifiers)
          get all the settings from the system properties then override with provided identifiers
ConfigurationProperties(java.lang.String pApplicationName)
          get all but the parameters from the system properties
ConfigurationProperties(java.lang.String pApplicationName, java.lang.String pSystemName)
          get all but the parameters from the system properties
ConfigurationProperties(java.lang.String pApplicationName, java.lang.String pSystemName, java.lang.String pUserName)
          get all but the parameters from the system properties
 
Method Summary
 void addConfigurationChangedListener(ConfigurationProperties.ConfigurationChangedListener listener)
           
 ConfigurationProperties chain(ConfigurationProperties value)
           
 void determineAndReadPropertyFiles()
          do both
 void determineAndReadPropertyFilesIfNotReadYet()
          Lazy reading
 void determinePropertyFiles()
          search and read the property files
 void fireConfigurationChanged()
           
static ConfigurationProperties get()
          Singleton or thread related
 java.lang.String get(java.lang.Class pContextClass, java.lang.String pPropertyName)
          Deprecated. use get2 for correct behavior or switch to the Configuration class
 java.lang.String get(java.lang.Object pContextObject, java.lang.String pPropertyName)
          Deprecated. use get2 for correct behavior or switch to the Configuration class
 java.lang.String get(java.lang.String pPropertyName)
          find the first definition of a property and return its value
 java.lang.String get2(java.lang.Class pContextClass, java.lang.String pPropertyName)
          convenience method: first check the context (full classname, simple classname) then the pure property name
 java.lang.String get2(java.lang.Object pContextObject, java.lang.String pPropertyName)
          convenience method: first check the context then the pure property name
 java.lang.String get2(java.lang.String pContext, java.lang.String pPropertyName)
          convenience method: first check the context then the pure property name
 java.util.List<java.io.File> getAdditionalPaths()
          AdditionalPaths
 java.lang.String getApplicationName()
          applicationname
 ConfigurationProperties getChain()
          by chaining configuration properties several naming scheme's can be combined
 boolean getCheckForUpdates()
           
 Properties getCollection(java.lang.String prefix)
          get all properties that match a prefix
 Properties getCollection(java.lang.String prefix, Properties properties)
          The group logic on one free set of properties.
 Properties getGroupedCollection(java.lang.String prefix)
          Get all properties that match a prefix and group them.
 java.lang.String getIdentifier(java.lang.String key)
          identifier
 boolean getIdentifierLock()
          IdentifierLock
 java.net.URL getLocation(java.lang.String pPropertyName)
          find the location of the first definition of a property
 java.util.Properties getManualProperties()
          we allow adding one manual properties set
 java.lang.String getOsName()
          osname
 java.lang.String getPrefix()
          the prefix is configurable
 Properties getProperties()
          Flatten all properties into one
 java.lang.String getSystemName()
          systemname
static ConfigurationProperties getThreadRelatedConfigurationProperties()
           
 long getTimerDelay()
           
 java.lang.String getUserName()
          username
static int parseInt(java.lang.String value, int defaultValue, org.apache.log4j.Logger log4j, java.lang.String prefix)
           
 void readPropertyFiles()
          search and read the property files
 void removeConfigurationChangedListener(ConfigurationProperties.ConfigurationChangedListener listener)
           
static ConfigurationProperties removeThreadRelatedConfigurationProperties()
           
 java.lang.String reportUsedIdentifiersAndFiles()
          for feedback
static void set(ConfigurationProperties configurationProperties)
           
 void setAdditionalPaths(java.io.File... value)
           
 void setAdditionalPaths(java.util.List<java.io.File> value)
           
 void setApplicationName(java.lang.String value)
           
 void setChain(ConfigurationProperties value)
           
 void setCheckForUpdates(boolean value)
           
 void setIdentifier(java.lang.String key, java.lang.String value)
           
 void setIdentifierLock(boolean value)
           
 void setManualProperties(java.io.InputStream is)
          create and load properties from an inputstream (e.g.
 void setManualProperties(java.util.Properties value)
           
 void setOsName(java.lang.String value)
           
 void setPrefix(java.lang.String value)
           
 void setSystemName(java.lang.String value)
           
static void setSystemWideApplicationName(java.lang.String name)
          Set the system wide application name used by all ConfigurationProperties in the JVM
static void setThreadRelatedConfigurationProperties(ConfigurationProperties value)
          ThreadRelatedConfigurationProperties This is the CP bound to the current thread It can also be used to preset a CP (for example from a session in a webapp).
 void setTimerDelay(long value)
           
 void setUserName(java.lang.String value)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SOURCECODE_VERSION

public static final java.lang.String SOURCECODE_VERSION
Standard variable for determining version of a class file.

See Also:
Constant Field Values

CONFIGURATIONPROPERTY_ID_HOST

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

CONFIGURATIONPROPERTY_ID_PORT

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

CONFIGURATIONPROPERTY_ID_APPLICATIONNAME

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

CONFIGURATIONPROPERTY_ID_SYSTEMNAME

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

CONFIGURATIONPROPERTY_ID_OSNAME

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

CONFIGURATIONPROPERTY_ID_USERNAME

public static final java.lang.String CONFIGURATIONPROPERTY_ID_USERNAME
See Also:
Constant Field Values
Constructor Detail

ConfigurationProperties

public ConfigurationProperties()
get all the settings from the system properties


ConfigurationProperties

public ConfigurationProperties(java.lang.String pApplicationName)
get all but the parameters from the system properties

Parameters:
pApplicationName - the name of the application

ConfigurationProperties

public ConfigurationProperties(java.lang.String pApplicationName,
                               java.lang.String pSystemName)
get all but the parameters from the system properties

Parameters:
pApplicationName - the name of the application
pSystemName - the name of the system

ConfigurationProperties

public ConfigurationProperties(java.lang.String pApplicationName,
                               java.lang.String pSystemName,
                               java.lang.String pUserName)
get all but the parameters from the system properties

Parameters:
pApplicationName - the name of the application
pSystemName - the name of the system
pUserName - the name of the currently active user

ConfigurationProperties

public ConfigurationProperties(java.util.Map identifiers)
get all the settings from the system properties then override with provided identifiers


ConfigurationProperties

public ConfigurationProperties(java.io.File... files)
get all the settings from the system properties then override with provided identifiers

Method Detail

get

public static ConfigurationProperties get()
Singleton or thread related


set

public static void set(ConfigurationProperties configurationProperties)

setThreadRelatedConfigurationProperties

public static void setThreadRelatedConfigurationProperties(ConfigurationProperties value)
ThreadRelatedConfigurationProperties This is the CP bound to the current thread It can also be used to preset a CP (for example from a session in a webapp).


getThreadRelatedConfigurationProperties

public static ConfigurationProperties getThreadRelatedConfigurationProperties()

removeThreadRelatedConfigurationProperties

public static ConfigurationProperties removeThreadRelatedConfigurationProperties()

getPrefix

public java.lang.String getPrefix()
the prefix is configurable


setPrefix

public void setPrefix(java.lang.String value)

getManualProperties

public java.util.Properties getManualProperties()
we allow adding one manual properties set


setManualProperties

public void setManualProperties(java.util.Properties value)

setManualProperties

public void setManualProperties(java.io.InputStream is)
                         throws java.io.IOException
create and load properties from an inputstream (e.g. via ...getClass().getResourceAsStream(...))

Throws:
java.io.IOException

getChain

public ConfigurationProperties getChain()
by chaining configuration properties several naming scheme's can be combined


setChain

public void setChain(ConfigurationProperties value)

chain

public ConfigurationProperties chain(ConfigurationProperties value)

getAdditionalPaths

public java.util.List<java.io.File> getAdditionalPaths()
AdditionalPaths


setAdditionalPaths

public void setAdditionalPaths(java.util.List<java.io.File> value)

setAdditionalPaths

public void setAdditionalPaths(java.io.File... value)

getIdentifier

public java.lang.String getIdentifier(java.lang.String key)
identifier


setIdentifier

public void setIdentifier(java.lang.String key,
                          java.lang.String value)

getIdentifierLock

public boolean getIdentifierLock()
IdentifierLock


setIdentifierLock

public void setIdentifierLock(boolean value)

getApplicationName

public java.lang.String getApplicationName()
applicationname


setApplicationName

public void setApplicationName(java.lang.String value)

getSystemName

public java.lang.String getSystemName()
systemname


setSystemName

public void setSystemName(java.lang.String value)

getOsName

public java.lang.String getOsName()
osname


setOsName

public void setOsName(java.lang.String value)

getUserName

public java.lang.String getUserName()
username


setUserName

public void setUserName(java.lang.String value)

determinePropertyFiles

public void determinePropertyFiles()
search and read the property files


readPropertyFiles

public void readPropertyFiles()
search and read the property files


determineAndReadPropertyFiles

public void determineAndReadPropertyFiles()
do both


determineAndReadPropertyFilesIfNotReadYet

public void determineAndReadPropertyFilesIfNotReadYet()
Lazy reading


reportUsedIdentifiersAndFiles

public java.lang.String reportUsedIdentifiersAndFiles()
for feedback


get

public java.lang.String get(java.lang.String pPropertyName)
find the first definition of a property and return its value


get

public java.lang.String get(java.lang.Class pContextClass,
                            java.lang.String pPropertyName)
Deprecated. use get2 for correct behavior or switch to the Configuration class

convenience method


get

public java.lang.String get(java.lang.Object pContextObject,
                            java.lang.String pPropertyName)
Deprecated. use get2 for correct behavior or switch to the Configuration class

convenience method


get2

public java.lang.String get2(java.lang.String pContext,
                             java.lang.String pPropertyName)
convenience method: first check the context then the pure property name


get2

public java.lang.String get2(java.lang.Class pContextClass,
                             java.lang.String pPropertyName)
convenience method: first check the context (full classname, simple classname) then the pure property name


get2

public java.lang.String get2(java.lang.Object pContextObject,
                             java.lang.String pPropertyName)
convenience method: first check the context then the pure property name


getLocation

public java.net.URL getLocation(java.lang.String pPropertyName)
find the location of the first definition of a property


getCollection

public Properties getCollection(java.lang.String prefix)
get all properties that match a prefix

Parameters:
prefix -
Returns:

getCollection

public Properties getCollection(java.lang.String prefix,
                                Properties properties)
The group logic on one free set of properties. Similar to Properties.getCollection, only the prefix is stripped

Parameters:
prefix -
properties -
Returns:

getGroupedCollection

public Properties getGroupedCollection(java.lang.String prefix)
Get all properties that match a prefix and group them. For example: my.collection.1.f.1=A1 my.collection.1.f.2=B1 my.collection.1.f.3=C1 my.collection.3.f.1=A3 my.collection.3.f.2=B3 my.collection.3.f.3=C3 Calling this method with prefix my.collection, returns a map with under the key "1" and "3" two maps containing "f.1" with their respective values. So the result is: [1=>[f.1=>A1, f.3=>C1, f.2=>B1], 3=>[f.1=>A3, f.3=>C3, f.2=>B3]] Often de index is not relevant, but having it in a map makes it a bit of a hassle to navigate (pre 1.5) Using " list.addAll(map.values()); " makes it easier.

Parameters:
prefix -
Returns:

parseInt

public static int parseInt(java.lang.String value,
                           int defaultValue,
                           org.apache.log4j.Logger log4j,
                           java.lang.String prefix)

getTimerDelay

public long getTimerDelay()

setTimerDelay

public void setTimerDelay(long value)

getCheckForUpdates

public boolean getCheckForUpdates()

setCheckForUpdates

public void setCheckForUpdates(boolean value)

getProperties

public Properties getProperties()
Flatten all properties into one

Parameters:
prefix -
Returns:

addConfigurationChangedListener

public void addConfigurationChangedListener(ConfigurationProperties.ConfigurationChangedListener listener)

removeConfigurationChangedListener

public void removeConfigurationChangedListener(ConfigurationProperties.ConfigurationChangedListener listener)

fireConfigurationChanged

public void fireConfigurationChanged()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setSystemWideApplicationName

public static void setSystemWideApplicationName(java.lang.String name)
Set the system wide application name used by all ConfigurationProperties in the JVM

Parameters:
name -


Copyright © 2010. All Rights Reserved.