Package jade.util
Class ExtendedProperties
- java.lang.Object
-
- java.util.Dictionary<K,V>
-
- java.util.Hashtable<Object,Object>
-
- java.util.Properties
-
- jade.util.leap.Properties
-
- jade.util.ExtendedProperties
-
- All Implemented Interfaces:
Serializable,Serializable,Cloneable,Map<Object,Object>
public class ExtendedProperties extends Properties
Provides enhanced property management.
1) Allows specifying property values containing strings of the form ${key}. Such strings are properly replaced with the value of the key variable that may represent another property, a system property or an environment variable.2) Allows specifying boolean properties in the form -key. Such format is equivalent to key=true.
3) Allows importing properties from external property files by means of the special key import. E.g. specifying the property
import = a-property-file-name
results in automatically adding all properties defined in the specified property file4) Allows declaring a property as read-only. In order to do that it is sufficient to end its key with a '!'. For example:
agentClass!=com.hp.agent.FooThe value of the property can be accessed including the '!' in the key or not indifferentlyThis class is designed to be usable in the restrictive J2ME CDC environment.
- Author:
- Dick Cowan - HP Labs
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringIMPORT_KEY-
Fields inherited from class java.util.Properties
defaults
-
-
Constructor Summary
Constructors Constructor Description ExtendedProperties()Construct empty property collection.ExtendedProperties(String[] propesStr)Construct a ExtendedProperties object from an array of stringified properties of the form= .
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddFromReader(Reader reader)Add properties from Reader.voidaddProperties(String[] propsStr)Add to this Properties object the stringified properties included in a given array.protected voidaddProperty(String propStr)Add to this Properties object a stringified property of the form key = value or -keyvoidcopyProperties(ExtendedProperties source)Copy a data from standard Properties.StringdoSubstitutions(String anInputString)Replace all substrings of the form ${xxx} with the property value using the key xxx.StringdoSubstitutions(String anInputString, boolean allowUndefined)Replace all substrings of the form ${xxx} with the property value using the key xxx.ExtendedPropertiesextractSubset(String prefix)Create a new Properties object by coping those properties whose key begins with a particular prefix string.Objectget(Object aKey)Get the object associated with a key.booleangetBooleanProperty(String aKey, boolean aDefaultValue)Extract a string value ("true" or "false") and convert it to a boolean.intgetIntProperty(String aKey, int aDefaultValue)Extract a string value and convert it to an integer.protected StringgetOneLine(Reader reader)Get a logical line.StringgetProperty(String aKey)Override getProperty in base class so all occurances of the form ${key} are replaced by their associated value.StringgetProperty(String aKey, String defaultValue)Perform substitution when a value is fetched.StringgetPropertyIgnoreCase(String aKey)Use this method to fetch a property ignoring case of key.StringgetRawProperty(String aKey)Fetch property value for key which may contain strings of the form ${key}.protected intgetSeparatorIndex(String propStr)Retrieve the position of the first valid key-value separator character ('=' or ':') in a stringified property.voidlist(PrintStream out)List properties to provided PrintStream.voidload(InputStream inStream)Add properties from a specified InputStream.static voidmain(String[] args)For testing.Objectput(Object aKey, Object aValue)Set property value to specified object.protected StringreadLine(Reader aReader)Read one line from the Reader.ObjectrenameKey(String existingKey, String newKey)Change key string associated with existing value.voidsetBooleanProperty(String aKey, boolean aValue)Store a boolean as a string ("true" or "false") with the specified key.intsetIntProperty(String aKey, int aValue)Store an int as a string with the specified key.ObjectsetPropertyIfNot(String aKey, String value)Set property value only if its not set already.EnumerationsortedKeys()Return a sorted enumeration of this properties keys.voidstore(OutputStream out, String header)Writes this property collection to the output stream in a format suitable for loading into a Properties table using the load method.String[]toStringArray()Create a String[] for the properties with one key=value pair per array entry.protected StringvalueFilter(String key, String value)Called by getProperty(key, default) to perform any post processing of the value string.-
Methods inherited from class jade.util.leap.Properties
clone, load, store, toLeapProperties
-
Methods inherited from class java.util.Properties
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, getOrDefault, hashCode, isEmpty, keys, keySet, list, load, loadFromXML, merge, propertyNames, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, save, setProperty, size, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values
-
-
-
-
Field Detail
-
IMPORT_KEY
public static final String IMPORT_KEY
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ExtendedProperties
public ExtendedProperties()
Construct empty property collection.
-
ExtendedProperties
public ExtendedProperties(String[] propesStr)
Construct a ExtendedProperties object from an array of stringified properties of the form= . - Parameters:
propesStr- The applications original arguments.
-
-
Method Detail
-
main
public static void main(String[] args)
For testing. Simply pass command line arguments to constructor then display all key=value pairs using sorted enumeration.
-
load
public void load(InputStream inStream) throws IOException
Add properties from a specified InputStream. Properties will be added to any existing collection.- Overrides:
loadin classProperties- Parameters:
aFileName- The name of the file.- Throws:
IOException- if anything goes wrong.
-
store
public void store(OutputStream out, String header) throws IOException
Writes this property collection to the output stream in a format suitable for loading into a Properties table using the load method.- Overrides:
storein classProperties- Parameters:
out- An output stream.header- A description of the property list - may be null.- Throws:
IOException- if anything goes wrong.
-
sortedKeys
public Enumeration sortedKeys()
Return a sorted enumeration of this properties keys.- Returns:
- Enumeration Sorted enumeration.
-
addProperties
public void addProperties(String[] propsStr)
Add to this Properties object the stringified properties included in a given array.- Parameters:
propsStr- The array of stringified properties. If null, this method does nothing.
-
addProperty
protected void addProperty(String propStr)
Add to this Properties object a stringified property of the form key = value or -key- Parameters:
propStr- The string representation of the property to be parsed
-
getSeparatorIndex
protected int getSeparatorIndex(String propStr)
Retrieve the position of the first valid key-value separator character ('=' or ':') in a stringified property.- Parameters:
propStr- The stringified property.
-
copyProperties
public void copyProperties(ExtendedProperties source)
Copy a data from standard Properties.- Parameters:
source- The properties to copy from.
-
extractSubset
public ExtendedProperties extractSubset(String prefix)
Create a new Properties object by coping those properties whose key begins with a particular prefix string. The prefix is removed from the keys inserted into the extracted Properties object- Parameters:
prefix- The prefix string. Ex: "server."
-
get
public Object get(Object aKey)
Get the object associated with a key. Note that, unlike getProperty(), this method does not perform substitutions of variables of the form ${x} in property values since such values may be non-string objects.
-
put
public Object put(Object aKey, Object aValue)
Set property value to specified object.- Specified by:
putin interfaceMap<Object,Object>- Overrides:
putin classProperties- Parameters:
aKey- The key used to store the data. The key may contain strings of the form ${key} which will be evaluated first.aValue- The object to be stored.- Returns:
- The previous value of the specified key, or null if it did not have one.
-
getProperty
public String getProperty(String aKey)
Override getProperty in base class so all occurances of the form ${key} are replaced by their associated value.- Overrides:
getPropertyin classProperties- Parameters:
aKey- Key for desired property.- Returns:
- The keys value with substitutions done.
-
getProperty
public String getProperty(String aKey, String defaultValue)
Perform substitution when a value is fetched. Traps circular definitions, and calls valueFilter with value prior to returning it.- Overrides:
getPropertyin classProperties- Parameters:
aKey- The property key.defaultValue- Value to return if property not defined. May be null. If non null it will be passes to valueFilter first.- Returns:
- The resultant value - could be null or empty.
- Throws:
PropertiesException- if circular definition.
-
setPropertyIfNot
public Object setPropertyIfNot(String aKey, String value)
Set property value only if its not set already.- Parameters:
aKey- The key used to store the data. The key may contain strings of the form ${key} which will be evaluated first.value- The value to be stored.- Returns:
- Null if store was done, non-null indicates store not done and the returned value in the current properties value.
-
getRawProperty
public String getRawProperty(String aKey)
Fetch property value for key which may contain strings of the form ${key}.- Parameters:
aKey- Key for desired property.- Returns:
- The keys value with no substitutions done.
-
getPropertyIgnoreCase
public String getPropertyIgnoreCase(String aKey)
Use this method to fetch a property ignoring case of key.- Parameters:
aKey- The key of the environment property.- Returns:
- The key's value or null if not found.
-
valueFilter
protected String valueFilter(String key, String value)
Called by getProperty(key, default) to perform any post processing of the value string. By default, this method provides special processing on the value associated with any property whose key name has the string "path" as part of it (ex: "classpath", "sourcepath", "mypath"). When the value for such keys is fetched any occurance of '|' will be converted to a ':' on Unix systems and a ';' on Windows systems. Therefore to increase the direct reuse of your property files, always use a '|' as a separator and always assign a key name which has "path" as part of it.- Parameters:
key- The properties key.value- The properties value.- Returns:
- String New potentially altered value.
-
getIntProperty
public int getIntProperty(String aKey, int aDefaultValue)
Extract a string value and convert it to an integer. If there isn't one or there is a problem with the conversion, return the default value.- Parameters:
aKey- The key which will be used to fetch the attribute.aDefaultValue- Specifies the default value for the int.- Returns:
- int The result.
-
setIntProperty
public int setIntProperty(String aKey, int aValue)
Store an int as a string with the specified key.- Parameters:
aKey- The key which will be used to store the attribute.aValue- The int value.
-
getBooleanProperty
public boolean getBooleanProperty(String aKey, boolean aDefaultValue)
Extract a string value ("true" or "false") and convert it to a boolean. If there isn't one or there is a problem with the conversion, return the default value.- Parameters:
aKey- The key which will be used to fetch the attribute.aDefaultValue- Specifies the default value for the boolean.- Returns:
- boolean The result.
-
setBooleanProperty
public void setBooleanProperty(String aKey, boolean aValue)
Store a boolean as a string ("true" or "false") with the specified key.- Parameters:
aKey- The key which will be used to store the attribute.aValue- The boolean value.
-
renameKey
public Object renameKey(String existingKey, String newKey)
Change key string associated with existing value.- Parameters:
existintKey- The current key.newKey- The new key.- Returns:
- Non null is former value of object associated with new key. Null indicates that either the existing key didn't exist or there was no former value associated with the new key. i.e. null => success.
-
doSubstitutions
public String doSubstitutions(String anInputString)
Replace all substrings of the form ${xxx} with the property value using the key xxx. Calls doSubstitutions(anInputString, false).- Parameters:
anInputString- The input string - may be null.- Returns:
- The resultant line with all substitutions done or null if input string was.
-
doSubstitutions
public String doSubstitutions(String anInputString, boolean allowUndefined)
Replace all substrings of the form ${xxx} with the property value using the key xxx. If the key is all caps then the property is considered to be a system property.- Parameters:
anInputString- The input string - may be null.allowUndefined- If true, undefined strings will remain as is, if false, an exception will be thrown.- Returns:
- The resultant line with all substitutions done or null if input string was.
-
addFromReader
protected void addFromReader(Reader reader) throws IOException
Add properties from Reader. Explicitly handled so as to enable handling of import=directive. Blank lines as well as those beginning with a '#' character (comments) are ignored. - Parameters:
reader- The buffered reader to read from. to catch circular imports.- Throws:
IOException- if anything goes wrong.
-
getOneLine
protected String getOneLine(Reader reader) throws IOException
Get a logical line. Any physical line ending in '\' is considered to continue on the next line.- Parameters:
reader- The input reader to read.- Returns:
- The resultant logical line which may have been constructed from one or more physical lines.
- Throws:
IOException- if anything goes wrong.
-
readLine
protected String readLine(Reader aReader) throws IOException
Read one line from the Reader. A line may be terminated by a single CR or LF, or the pair CR LF.- Parameters:
aReader- The Reader to read characters from.- Returns:
- Next physical line.
- Throws:
IOException- if anything goes wrong.
-
list
public void list(PrintStream out)
List properties to provided PrintStream. Output will be in sorted key sequence. If a value is null, it will appear as "key=".- Overrides:
listin classProperties- Parameters:
out- The print stream.
-
toStringArray
public String[] toStringArray()
Create a String[] for the properties with one key=value pair per array entry. If a value is null, it will appear as "key=".- Returns:
- The resultant String[].
-
-