|
xmlBlaster 2.2.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.xmlBlaster.util.property.Property
public class Property
Handling properties for your project, e.g. your property file $HOME/cool.properties and command line properties.
java -DMOMS_NAME=Joan ...
MOMS_NAME=Joanor on the command line:
java org.MyApp -MOMS_NAME Joan
MOMS_RECIPY=Strawberry cake from ${MONS_NAME} is the best.which results to
MOMS_RECIPY=Strawberry cake from Joan is the best.The maximum replacement (nesting) depth is 50. A user may specify the PROJECT_HOME environment variable, the property file will be searched there as well. NOTE: These are often helpful variables
NOTE: If a variable like ${XYZ} is not resolved an exception is thrown, you can use the markup $_{XYZ} to avoid the exception in which case the $_{XYZ} remains as is.
Searching the property file
This class searches the given property file in typical directories (see findPath() method). It is searched for in the following order:
Array support
We look for keys containing [] brackets and collect them into a map
val[A]=AAA val[B]=BBBAccessing the map with the array praefix (here it is "val")
Map map = get("val", (Map)null);returns a Map containing keys { "A", "B" } and values { "AAA", "BBB" }
val[C][1]=cccc -> map entry with key "C:1" and value "cccc"
Very simple array values
Values of properties may have a separator, example:
names=Joe,Jack,AverellAccess the names split to an array:
String[] arr = prop.get("array", new String[0], ","); //arr[0] contains "Joe", arr[1] contains "Jack" and arr[2] contains "Averell"
Listening on property changes
You can listen on added properties, changed properties or on removed properties, the following code
snippet shows how to do it:
I_PropertyChangeListener ll = prop.addPropertyChangeListener(theKey, new I_PropertyChangeListener() { public void propertyChanged(PropertyChangeEvent ev) { System.out.println("propertyChanged: " + ev.toString()); // key = ev.getKey(); // old = ev.getOldValue(); // value = ev.getNewValue(); } } );Note that setting a property with an equal value is not triggering an event. JDK 1.1 or higher only.
testsuite.TestProperty
Nested Class Summary | |
---|---|
class |
Property.FileInfo
|
Field Summary | |
---|---|
private java.lang.Object |
applet
Applet handle or null |
private java.util.HashMap |
changeListenerMap
Map containing as key a property key and as value a Set of listeners (I_PropertyChangeListener) which wants to be notified on changes |
private java.lang.String |
currentPath
current directory, can it change for each instance or is it final in the JVM? |
static int |
DEFAULT_VERBOSE
|
private java.util.Properties |
dummyProperties
|
private static java.lang.String |
javaHome
The java home directory, e.g. |
private static java.lang.String |
javaHomeExt
The java home ext directory, e.g. |
static int |
MAX_NEST
|
private static java.lang.String |
ME
|
private java.lang.String |
projectHomePath
user supplied path in PROJECT_HOME |
private java.util.Properties |
properties
Holding all found properties |
private java.lang.String |
propertyFileName
The supplied file name, e.g. |
private java.util.Map |
propMap
Holding all maps from array properties, e.g. |
private boolean |
replaceVariables
Replace occurrences of ${key} with the value of key |
private boolean |
scanSystemProperties
Scan system environment variables? |
private static java.lang.String |
separator
|
private boolean |
supportArrays
Scan for properties with square brackets [] and handle them as array |
private static java.lang.String |
userHome
The users home directory |
int |
verbose
Set the verbosity when loading properties (outputs with System.out). |
private boolean |
wantsHelp
Was the option '--help' or '-help' or '-h' or '-?' specified? |
Constructor Summary | |
---|---|
Property()
The default constructor. |
|
Property(java.lang.String propertyFileUrl,
boolean scanSystemProperties,
java.lang.Object applet,
boolean replaceVariables)
Construct a property container from supplied property file (URL) and Applet. |
|
Property(java.lang.String fileName,
boolean scanSystemProperties,
java.util.Properties extraProps,
boolean replaceVariables)
Construct a property container from supplied property file and enumeration. |
|
Property(java.lang.String fileName,
boolean scanSystemProperties,
java.lang.String[] args,
boolean replaceVariables)
Construct a property container from supplied property file and args array. |
|
Property(java.lang.String fileName,
boolean scanSystemProperties,
java.lang.String[] args,
boolean replaceVariables,
boolean output)
Construct a property container from supplied property file and args array. |
|
Property(java.lang.String fileName,
boolean scanSystemProperties,
java.lang.String projectname,
boolean replaceVariables)
Construct a property container from supplied property file and enumeration. |
Method Summary | |
---|---|
void |
addArgs2Props(java.util.Properties argsProps)
The same as addArgs2Props(String[] args) but passing the args in a Properties hashtable. |
private void |
addArgs2Props(java.util.Properties props,
java.util.Properties argsProps)
See method addArgs2Props(args) |
void |
addArgs2Props(java.lang.String[] args)
Add key/values, for example from startup command line args to the property variable. |
I_PropertyChangeListener |
addPropertyChangeListener(java.lang.String key,
I_PropertyChangeListener l)
Listen on change or creation events of the given key. |
I_PropertyChangeListener |
addPropertyChangeListener(java.lang.String key,
java.lang.String defaultValue,
I_PropertyChangeListener l)
Listen on change or creation events of the given key |
static java.util.Properties |
argsToProps(java.lang.String[] args)
Scan args, correct and check them and return the result in a Properties hash table |
java.lang.Object |
clone()
We do a deep copy for all properties and listeners |
Property.FileInfo |
findArgsFile(java.util.Properties argsProps)
Find properties file. |
Property.FileInfo |
findFile(java.lang.String fileName,
java.util.Properties argsProps)
Find properties file. |
Property.FileInfo |
findGivenFile(java.lang.String fileName)
Find properties file which was given with the constructor. |
Property.FileInfo |
findPath(java.lang.String fileName)
Look for properties file. |
private void |
fireChangeEvent(java.lang.String key,
java.lang.String oldValue,
java.lang.String newValue)
Notifies registered listeners when a property has changed or is created or is deleted If key or values are null, no event is fired |
private java.lang.String |
get_(java.lang.String key)
|
boolean |
get(java.lang.String key,
boolean defaultVal)
Try to find the given key. |
double |
get(java.lang.String key,
double defaultVal)
Try to find the given key. |
float |
get(java.lang.String key,
float defaultVal)
Try to find the given key. |
int |
get(java.lang.String key,
int defaultVal)
Try to find the given key. |
long |
get(java.lang.String key,
long defaultVal)
Try to find the given key. |
java.util.Map |
get(java.lang.String key,
java.util.Map defaultVal)
Try to find the given key. |
java.lang.String |
get(java.lang.String key,
java.lang.String defaultVal)
Try to find the given key. |
java.lang.String[] |
get(java.lang.String key,
java.lang.String[] defaultVal,
java.lang.String separator)
Try to find the given key. |
private java.util.Map |
getMap_(java.lang.String key)
|
java.util.Properties |
getProperties()
Get the internal handle. |
java.util.Map |
getPropertiesForContextNode(ContextNode parentCtx,
java.lang.String shortKey,
java.lang.String keyForDefault)
Returns a map containing all properties of the specified context. |
java.util.Map |
getPropertiesStartingWith(java.lang.String prefix)
Returns the subset of properties found in the properties starting with the specified prefix. |
private void |
init(java.lang.String fileName_,
boolean scanSystemProperties,
java.lang.String[] args,
boolean replaceVariables,
boolean supportArrays)
Internal Initialize. |
java.util.Properties |
loadProps(Property.FileInfo info,
java.util.Properties argsProps)
Use this method only the first time to initialize everything. |
static void |
main(java.lang.String[] args)
For testing only |
boolean |
propertyExists(java.lang.String key)
Try to find the given key. |
static java.lang.String[] |
propsToArgs(java.util.Map props)
Convert a properties hashtable to a String array with leading "-" in front of the keys |
java.lang.String |
removeProperty(java.lang.String key)
Remove the given property. |
void |
removePropertyChangeListener(java.lang.String key,
I_PropertyChangeListener l)
Listen on change or creation events of the given key |
private java.lang.String |
replaceVariable(java.lang.String key,
java.lang.String value)
Replace dynamic variables, e.g. |
java.lang.String |
replaceVariableNoException(java.lang.String key,
java.lang.String value)
Replace dynamic variables, e.g. |
private void |
replaceVariables()
Replace all dynamic variables, e.g. |
java.lang.String |
replaceVariableWithException(java.lang.String key,
java.lang.String value)
Replace dynamic variables, e.g. |
void |
saveProps()
Saves the property to a file; this also will include the system properties! |
void |
saveProps(java.lang.String fileName)
Saves the property to a file; this also will include the system properties! |
private void |
scanArray(java.lang.String key,
java.lang.String value)
|
private void |
scanArrays()
We look for keys containing [] brackets and collect them into a map |
java.lang.String |
set(java.lang.String key,
java.lang.String value)
Set or overwrite a property, note that currently no variable replacement is implemented for the passed value. |
void |
setApplet(java.lang.Object applet)
|
static boolean |
toBool(java.lang.String token)
Parse a string to boolean. |
java.lang.String |
toXml()
|
java.lang.String |
toXml(java.lang.String extraOffset)
Dump all properties to xml representation. |
boolean |
wantsHelp()
If set to true user wants you to display a usage text. |
Methods inherited from class java.lang.Object |
---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final java.lang.String ME
private static java.lang.String separator
private static java.lang.String userHome
private java.lang.String currentPath
private java.lang.String projectHomePath
private static java.lang.String javaHomeExt
private static java.lang.String javaHome
private java.lang.String propertyFileName
private java.util.Properties properties
private final java.util.Properties dummyProperties
private java.util.Map propMap
private boolean scanSystemProperties
private boolean replaceVariables
private boolean supportArrays
private boolean wantsHelp
private java.lang.Object applet
private java.util.HashMap changeListenerMap
public static final int MAX_NEST
public static final int DEFAULT_VERBOSE
public int verbose
java -Dproperty.verbose 2 java MyApp -property.verbose 2
Constructor Detail |
---|
public Property(java.lang.String fileName, boolean scanSystemProperties, java.lang.String[] args, boolean replaceVariables) throws XmlBlasterException
fileName
- The property file name, e.g. "project.properties"scanSystemProperties
- Scan System.getProperties() as well, you can
add variable to JVM like: java -DmyName=Joe ...args
- A String array with properties, usually from command line, e.g.
java myApp -logging FINE -name JoereplaceVariables
- true: replace occurrences of ${key} with the value of key
XmlBlasterException
public Property(java.lang.String fileName, boolean scanSystemProperties, java.util.Properties extraProps, boolean replaceVariables) throws XmlBlasterException
Properties extraProps = new Properties(); Enumeration e = conf.getInitParameterNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); extraProps.put(name, conf.getInitParameter(name)); }
fileName
- The property file name, e.g. "project.properties"scanSystemProperties
- Scan System.getProperties() as well, you can
add variable to JVM like: java -DmyName=Joe ...args
- A String array with properties, usually from command line, e.g.
java myApp -trace true -name JoereplaceVariables
- true: replace occurrences of ${key} with the value of key
XmlBlasterException
public Property(java.lang.String fileName, boolean scanSystemProperties, java.lang.String projectname, boolean replaceVariables) throws XmlBlasterException
Properties extraProps = new Properties(); Enumeration e = conf.getInitParameterNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); extraProps.put(name, conf.getInitParameter(name)); }
fileName
- The property file name, e.g. "project.properties"scanSystemProperties
- Scan System.getProperties() as well, you can
add variable to JVM like: java -DmyName=Joe ...name
- of project which is to be extracted from user.home/projects.propertiesreplaceVariables
- true: replace occurrences of ${key} with the value of key
XmlBlasterException
public Property(java.lang.String fileName, boolean scanSystemProperties, java.lang.String[] args, boolean replaceVariables, boolean output) throws XmlBlasterException
fileName
- The property file name, e.g. "project.properties"scanSystemProperties
- Scan System.getProperties() as well, you can
add variable to JVM like: java -DmyName=Joe ...args
- A String array with properties, usually from command line, e.g.
java myApp -trace true -name JoereplaceVariables
- true: replace occurrences of ${key} with the value of keyoutput
- false: no info output to System.out is printed. Only successfull operation.
XmlBlasterException
public Property(java.lang.String propertyFileUrl, boolean scanSystemProperties, java.lang.Object applet, boolean replaceVariables) throws XmlBlasterException
propertyFileUrl
- applet.getCodeBase()+"xy.properties" e.g. "http://myHost.com:80/xy.properties"scanSystemProperties
- Scan System.getProperties() as well, you can
add variable to JVM like: java -DmyName=Joe ...applet
- Not yet supported. How to access all applet parameters???replaceVariables
- true: replace occurrences of ${key} with the value of key
XmlBlasterException
public Property()
Method Detail |
---|
public java.util.Map getPropertiesStartingWith(java.lang.String prefix)
prefix
- The prefix to use. If null is passed, then all properties
are returned
public java.util.Map getPropertiesForContextNode(ContextNode parentCtx, java.lang.String shortKey, java.lang.String keyForDefault)
ctx
- shortKey
- keyForDefault,
- can be null in which case it will not return the default value.
public java.lang.Object clone()
clone
in class java.lang.Object
private void init(java.lang.String fileName_, boolean scanSystemProperties, java.lang.String[] args, boolean replaceVariables, boolean supportArrays) throws XmlBlasterException
argsProps
- The checked command line arguments
XmlBlasterException
public final java.util.Properties getProperties()
public void setApplet(java.lang.Object applet)
private java.lang.String get_(java.lang.String key)
private java.util.Map getMap_(java.lang.String key)
public final java.lang.String set(java.lang.String key, java.lang.String value) throws XmlBlasterException
key
- The key for this propertyvalue
- The value for it
XmlBlasterException
public final java.lang.String get(java.lang.String key, java.lang.String defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final java.lang.String[] get(java.lang.String key, java.lang.String[] defaultVal, java.lang.String separator)
key
- the key to look fordefaultVal
- The default value to return if key is not foundseparator
- The separator, typically ","
public final int get(java.lang.String key, int defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final long get(java.lang.String key, long defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final double get(java.lang.String key, double defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final float get(java.lang.String key, float defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final boolean get(java.lang.String key, boolean defaultVal)
key
- the key to look fordefaultVal
- the default value to return if key is not found
public final java.util.Map get(java.lang.String key, java.util.Map defaultVal)
Map map = get(key, (Map)null); if (map != null) { ... }
key
- the key to look fordefaultVal
- the default Map to return if key is not found
public final boolean propertyExists(java.lang.String key)
key
- the parameter key to look for
public final java.lang.String removeProperty(java.lang.String key)
key
- the key to remove
public final boolean wantsHelp()
public final java.util.Properties loadProps(Property.FileInfo info, java.util.Properties argsProps) throws XmlBlasterException
argsProps
- This key/value parameter array is added to the properties object (see addArgs2Props()).
XmlBlasterException
private final void replaceVariables() throws XmlBlasterException
XmlBlasterException
private final void scanArrays() throws XmlBlasterException
val[A]=AAA val[B]=BBB -> get("val", (Map)null); Returns a Map containing keys { "A", "B" } and values { "AAA", "BBB" }Two dimensional arrays are supported as well:
val[C][1]=cccc -> map entry with key "C:1" and value "cccc"
XmlBlasterException
private final void scanArray(java.lang.String key, java.lang.String value) throws XmlBlasterException
XmlBlasterException
scanArrays()
private void fireChangeEvent(java.lang.String key, java.lang.String oldValue, java.lang.String newValue)
private final java.lang.String replaceVariable(java.lang.String key, java.lang.String value) throws XmlBlasterException
key
- For logging only
XmlBlasterException
public final java.lang.String replaceVariableWithException(java.lang.String key, java.lang.String value) throws XmlBlasterException
key
- For logging only
XmlBlasterException
- if a variable ${...} is not found of max nesting depth is reached or matching "}" is missingpublic final java.lang.String replaceVariableNoException(java.lang.String key, java.lang.String value) throws XmlBlasterException
key
- For logging only
XmlBlasterException
- if matching "}" is missingpublic static final boolean toBool(java.lang.String token) throws XmlBlasterException
token
- for example "false"
if
- none of the above strings
XmlBlasterException
public final Property.FileInfo findPath(java.lang.String fileName)
fileName
- e.g. "cool.properties".
public final Property.FileInfo findArgsFile(java.util.Properties argsProps)
args
- Only "-propertyFile /tmp/xy.properties" is evaluated (if given)
public final Property.FileInfo findGivenFile(java.lang.String fileName)
fileName
- e.g. "cool.properties"args
- Only "-propertyFile /tmp/xy.properties" is evaluated (if given)
this has precedence over the given fileName!
public final Property.FileInfo findFile(java.lang.String fileName, java.util.Properties argsProps)
fileName
- e.g. "cool.properties"args
- Only "-propertyFile /tmp/xy.properties" is evaluated (if given)
this has precedence over the given fileName!
public void addArgs2Props(java.lang.String[] args) throws XmlBlasterException
XmlBlasterException
public void addArgs2Props(java.util.Properties argsProps) throws XmlBlasterException
XmlBlasterException
public static java.lang.String[] propsToArgs(java.util.Map props)
private void addArgs2Props(java.util.Properties props, java.util.Properties argsProps) throws XmlBlasterException
XmlBlasterException
public static java.util.Properties argsToProps(java.lang.String[] args) throws XmlBlasterException
XmlBlasterException
public final I_PropertyChangeListener addPropertyChangeListener(java.lang.String key, I_PropertyChangeListener l)
public final I_PropertyChangeListener addPropertyChangeListener(java.lang.String key, java.lang.String defaultValue, I_PropertyChangeListener l)
defaultValue
- If not null an initial event is sent: If the property is known its value is used else the defaultValue is used.
The defaultValue is not stored in this case, only bounced back.
public final void removePropertyChangeListener(java.lang.String key, I_PropertyChangeListener l)
l
- If null all listeners are removedpublic java.lang.String toXml()
public java.lang.String toXml(java.lang.String extraOffset)
public static void main(java.lang.String[] args)
java -Djava.compiler= org.xmlBlaster.util.property.Property -Persistence.Driver myDriver -isCool true -xml "world " java -Djava.compiler= org.xmlBlaster.util.property.Property -dump true -uu "You are the user \${user.name}" -double "\${uu} using \${java.vm.name}" java -Djava.compiler= org.xmlBlaster.util.property.Property -NameList Josua,David,Ken,Abel java -Djava.compiler= org.xmlBlaster.util.property.Property -urlVariant true -url http://localhost/xy.properties java -Djava.compiler= org.xmlBlaster.util.property.Property -hashVariant true java -Djava.compiler= org.xmlBlaster.util.property.Property -dump true -val[A] aaaa -val[B] bbbb -val[C][1] cccc -val[C][2] c2c2
public void saveProps() throws java.io.IOException
java.io.IOException
- Die Beschreibung der Ausnahmebedingung.public void saveProps(java.lang.String fileName) throws java.io.IOException
java.io.IOException
- Die Beschreibung der Ausnahmebedingung.
|
xmlBlaster 2.2.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |