|
xmlBlaster 2.2.0 client 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 | |
---|---|
static int |
DEFAULT_VERBOSE
|
static int |
MAX_NEST
|
int |
verbose
Set the verbosity when loading properties (outputs with System.out). |
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. |
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. |
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. |
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. |
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 |
java.lang.String |
replaceVariableNoException(java.lang.String key,
java.lang.String value)
Replace 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! |
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 |
---|
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
public final java.util.Properties getProperties()
public void setApplet(java.lang.Object applet)
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
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)
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 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |