lazyj
Class ExtProperties

java.lang.Object
  extended by java.util.Observable
      extended by lazyj.ExtProperties
All Implemented Interfaces:
java.util.Observer

public final class ExtProperties
extends java.util.Observable
implements java.util.Observer

Utility class to process enhanced .properties file.

Such a file can have a special "include" key, which is a list of comma-separated file names (without the .properties extension) relative to the same base folder.

Each value can contain constructions like:

This object can monitor an underlying file for modifications and automatically reload the contents of the file when it detects changes.

Since:
2006-09-29
Author:
costing

Constructor Summary
ExtProperties()
          Default constructor, creates a configuration dictionary that has no file backing.
ExtProperties(java.io.InputStream is)
          Load the configuration from an InputStream (such as the one returned by ClassLoader.getResourceAsStream(String)
ExtProperties(java.util.Properties inherit)
          Copy constructor
ExtProperties(java.lang.String sBaseConfigDir, java.lang.String sConfigFilename)
          Load the contents of a .properties file from the sConfDir path.
ExtProperties(java.lang.String sBaseConfigDir, java.lang.String sConfigFilename, ExtProperties superProperties)
          Load the contents of a .properties file from the sConfDir path.
 
Method Summary
 void clearCache()
          Method to clear the cache in order to force the evaluation of the keys once again.
 boolean getb(java.lang.String sKey, boolean bDefault)
          Parse an option to return the boolean value.
 java.lang.String getConfigFileName()
          Get the name of the file that was loaded.
 double getd(java.lang.String sKey, double dDefault)
          Get the double value for a key.
 int geti(java.lang.String sKey, int iDefault)
          Get the integer value for a key.
 long getl(java.lang.String sKey, long lDefault)
          Get the long value for a key.
 java.util.Properties getProperties()
          Get a Properties view of this object.
 java.lang.String gets(java.lang.String sKey)
          Get the String value for a given key.
 java.lang.String gets(java.lang.String sKey, java.lang.String sDefault)
          Get the String value for a given key, returning the given default value if the key is not defined.
 java.lang.String gets(java.lang.String sKey, java.lang.String sDefault, boolean bProcessQueries)
          Get the String value for a given key, returning the given default value if the key is not defined.
 void makeReadOnly()
          Make this dictionary read-only, to prevent writes from other pieces of code that see this object.
 java.lang.String parseOption(java.lang.String sKey, java.lang.String sValue, java.lang.String sDefault, boolean bProcessQueries)
          Parse a value to include other keys
 void reload()
          Re-read the same configuration file.
 void set(java.lang.String sKey, java.lang.String sValue)
          Modify an entry of the dictionary.
 void setAutoReload(long lReload)
          Make this object check for changes in the base file and reload the contents when it changes.
 java.lang.String toString()
          Get some debugging info for this object
 java.util.Vector<java.lang.String> toVector(java.lang.String sKey)
          Split a value by "," and return a Vector of String parts.
 void update(java.util.Observable o, java.lang.Object arg)
          Implementation of the Observer interface.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExtProperties

public ExtProperties()
Default constructor, creates a configuration dictionary that has no file backing.


ExtProperties

public ExtProperties(java.io.InputStream is)
              throws java.io.IOException
Load the configuration from an InputStream (such as the one returned by ClassLoader.getResourceAsStream(String)

Parameters:
is - input stream to read from
Throws:
java.io.IOException - in case of problems while reading from the stream

ExtProperties

public ExtProperties(java.lang.String sBaseConfigDir,
                     java.lang.String sConfigFilename)
Load the contents of a .properties file from the sConfDir path.

Parameters:
sBaseConfigDir - base folder for the configuration files
sConfigFilename - file to load, without the ".properties" extension, that will be automatically added

ExtProperties

public ExtProperties(java.lang.String sBaseConfigDir,
                     java.lang.String sConfigFilename,
                     ExtProperties superProperties)
Load the contents of a .properties file from the sConfDir path. If the pSuper parameter is not null then the values from the given ExtProperties are inherited. Any key defined in this class will override any inherited key.

Parameters:
sBaseConfigDir - base folder for all the configuration files
sConfigFilename - file to load (without the ".properties" extension)
superProperties - other configuration to use as default (the keys from the current file will override the ones given as defaults)

ExtProperties

public ExtProperties(java.util.Properties inherit)
Copy constructor

Parameters:
inherit - key-value pairs to copy
Method Detail

setAutoReload

public void setAutoReload(long lReload)
Make this object check for changes in the base file and reload the contents when it changes.

Parameters:
lReload -

getConfigFileName

public java.lang.String getConfigFileName()
Get the name of the file that was loaded. It will return the full path to the file name, eg. the base folder + file name + ".properties".

Returns:
the file name that was loaded

reload

public void reload()
Re-read the same configuration file. Also clears the cache of parsed keys.


makeReadOnly

public void makeReadOnly()
Make this dictionary read-only, to prevent writes from other pieces of code that see this object.


parseOption

public java.lang.String parseOption(java.lang.String sKey,
                                    java.lang.String sValue,
                                    java.lang.String sDefault,
                                    boolean bProcessQueries)
Parse a value to include other keys

Parameters:
sKey - the key that is parsed
sValue - original value
sDefault - default value, in case of an error in parsing
bProcessQueries - whether or not to execute database queries / cache lookups
Returns:
the processed value

gets

public java.lang.String gets(java.lang.String sKey)
Get the String value for a given key. If the key is not defined then the empty string is returned. Queries will be processed.

Parameters:
sKey - the key to get the value for
Returns:
the value
See Also:
gets(String, String, boolean)

gets

public java.lang.String gets(java.lang.String sKey,
                             java.lang.String sDefault)
Get the String value for a given key, returning the given default value if the key is not defined. Queries will be processed.

Parameters:
sKey - the key to get the value for
sDefault - default value to return in case the key is not defined
Returns:
the value
See Also:
gets(String, String, boolean)

gets

public java.lang.String gets(java.lang.String sKey,
                             java.lang.String sDefault,
                             boolean bProcessQueries)
Get the String value for a given key, returning the given default value if the key is not defined. The value that is returned is also stored in a cache so that future requests to the same key will return the value from the cache. This also means that if the key is not defined then the given default value will be cached and returned the next time this function is called. null values are not cached.

Parameters:
sKey - the key to get the value for
sDefault - default value to return in case the key is not defined.
bProcessQueries - flag to process or not process the database/memory cache queries
Returns:
value for this key

getb

public boolean getb(java.lang.String sKey,
                    boolean bDefault)
Parse an option to return the boolean value. It returns true if the value starts with t,T,y,Y or 1 and false if the value starts with f,F,n,N or 0. In any other case it returns the given default value.

Parameters:
sKey - the key to get the value for
bDefault - default value
Returns:
a boolean

geti

public int geti(java.lang.String sKey,
                int iDefault)
Get the integer value for a key. Returns the given default value if the key is not defined or the value is not an integer reprezentation.

Parameters:
sKey - the key to get the value for
iDefault - default value
Returns:
an integer

getl

public long getl(java.lang.String sKey,
                 long lDefault)
Get the long value for a key. Returns the given default value if the key is not defined or the value is not a long reprezentation.

Parameters:
sKey - the key to get the value for
lDefault - default value
Returns:
a long

getd

public double getd(java.lang.String sKey,
                   double dDefault)
Get the double value for a key. Returns the given default value if the key is not defined or the value is not a double reprezentation.

Parameters:
sKey - the key to get the value for
dDefault - default value
Returns:
a double

toVector

public java.util.Vector<java.lang.String> toVector(java.lang.String sKey)
Split a value by "," and return a Vector of String parts.

Parameters:
sKey - the key to get the values for
Returns:
a Vector of String parts

clearCache

public void clearCache()
Method to clear the cache in order to force the evaluation of the keys once again.


toString

public java.lang.String toString()
Get some debugging info for this object

Overrides:
toString in class java.lang.Object
Returns:
the original keys dump + cached values

set

public void set(java.lang.String sKey,
                java.lang.String sValue)
Modify an entry of the dictionary.

Parameters:
sKey - key to change
sValue - new value

getProperties

public java.util.Properties getProperties()
Get a Properties view of this object. This will force a parsing of all the defined keys.

Returns:
a Properties view of this object

update

public void update(java.util.Observable o,
                   java.lang.Object arg)
Implementation of the Observer interface. Since this object can monitor the original file for changes, it will be notified by a call to this method when this happens.

Specified by:
update in interface java.util.Observer
Parameters:
o - ignored
arg - ignored