Package lazyj
Class ExtProperties
java.lang.Object
java.util.Observable
lazyj.ExtProperties
- All Implemented Interfaces:
java.util.Observer
- Direct Known Subclasses:
DBProperties
,FallbackProperties
public 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:
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:
${other_key}
, to include the value of another key in that place
- Since:
- 2006-09-29
- Author:
- costing
-
Constructor Summary
Constructors Constructor Description 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 byClassLoader.getResourceAsStream(String)
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.ExtProperties(java.lang.String sBaseConfigDir, java.lang.String sConfigFilename, ExtProperties superProperties, boolean ignoreMissingIncludes)
Load the contents of a .properties file from the sConfDir path.ExtProperties(java.util.Properties inherit)
Copy constructor -
Method Summary
Modifier and Type Method Description void
addPropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener listener)
Listen for particular changes in the setvoid
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.boolean
isReadOnly()
Check if the configuration object is read only or can accept modifications to itvoid
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 keysvoid
reload()
Re-read the same configuration file.void
removePropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener listener)
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.int
size()
Get the number of elements in this dictionaryjava.lang.String
toString()
Get some debugging info for this objectjava.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 theObserver
interface.
-
Constructor Details
-
ExtProperties
public ExtProperties()Default constructor, creates a configuration dictionary that has no file backing. -
ExtProperties
public ExtProperties(java.io.InputStream is) throws java.io.IOExceptionLoad the configuration from an InputStream (such as the one returned byClassLoader.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 filessConfigFilename
- 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 filessConfigFilename
- 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.lang.String sBaseConfigDir, java.lang.String sConfigFilename, ExtProperties superProperties, boolean ignoreMissingIncludes)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 filessConfigFilename
- 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)ignoreMissingIncludes
- whether or not to ignore missing include files
-
ExtProperties
public ExtProperties(java.util.Properties inherit)Copy constructor- Parameters:
inherit
- key-value pairs to copy
-
-
Method Details
-
size
public int size()Get the number of elements in this dictionary- Returns:
- number of entries
-
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 parsedsValue
- original valuesDefault
- default value, in case of an error in parsingbProcessQueries
- 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 forsDefault
- 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 forsDefault
- 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 forbDefault
- 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 representation.- Parameters:
sKey
- the key to get the value foriDefault
- 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 forlDefault
- 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 fordDefault
- 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 classjava.lang.Object
- Returns:
- the original keys dump + cached values
-
isReadOnly
public boolean isReadOnly()Check if the configuration object is read only or can accept modifications to it- Returns:
true
if changes are blocked
-
set
public void set(java.lang.String sKey, java.lang.String sValue)Modify an entry of the dictionary.- Parameters:
sKey
- key to changesValue
- 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
-
addPropertyChangeListener
public void addPropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener listener)Listen for particular changes in the set- Parameters:
key
- a particular property key to subscribe to, ornull
to receive all updateslistener
- object to notify
-
removePropertyChangeListener
public void removePropertyChangeListener(java.lang.String key, java.beans.PropertyChangeListener listener)- Parameters:
key
-listener
-
-
update
public void update(java.util.Observable o, java.lang.Object arg)Implementation of theObserver
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 interfacejava.util.Observer
- Parameters:
o
- ignoredarg
- ignored
-