|
Arch4J 1.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.arch4j.property.XMLPropertyManager
This class manages the reading, writing, and parsing of an XML-based property file. It also provides backward compatibility to the old-style property scheme by supporting the old API.
When no property domains are used, the simplest format for the XML-based property file looks like this:
<?xml version="1.0" encoding="UTF-8" ?> <xml-properties> <property key="someKey" value="Some Value" type="string" /> <property key="some.boolean.key" value="true" type="boolean" /> <property key="long.key.multiple" value="12023453454" type="long" /> <property key="long.key.multiple" value="-987654321012" type="long" /> </xml-properties>
In this case, a default domain is used. This file is functionally equivalent to:
<?xml version="1.0" encoding="UTF-8" ?> <xml-properties> <domain name="$DEFAULT$"> <property key="someKey" value="Some Value" type="string" /> <property key="some.boolean.key" value="true" type="boolean" /> <property key="long.key.multiple" value="12023453454" type="long" /> <property key="long.key.multiple" value="-987654321012" type="long" /> </domain> </xml-properties>
Properties can be defined with the following list of data types:
boolean char double float int long string
In order to remain backward compatible, split your properties up into domains, specifically, the ones that other components within Arch4J use. For example, here are a few of the known domains:
<?xml version="1.0" encoding="UTF-8" ?> <xml-properties> <domain name="dataaccess"> <property key="defaultConnection" value="myMainDatabase" type="string" /> <property key="myMainDatabase.driver" value="com.mysql.jdbc.Driver" type="string" /> <property key="myMainDatabase.url" value="jdbc:mysql:///testdata" type="string" /> <property key="myMainDatabase.username" value="root" type="string" /> <property key="myMainDatabase.password" value="" type="string" /> <property key="myMainDatabase.maxConnections" value="10" type="string" /> <property key="myMainDatabase.useArch4JPool" value="false" type="string" /> </domain> <domain name="logging"> <property key="framework.class" value="org.arch4j.logging.log4j.Log4JLoggingFramework" type="string" /> <property key="framework.log4j" value="log4j.properties" type="string" /> </domain> </xml-properties>
One drawback to maintaining backward compatibility is that properties in the older domains must be defined with the type of string, since that is all that is available via the Java Properties API.
While it may not be well-formed XML, the parser will allow properties to be both inside and outside the domain tag. Those properties defined outside of the domain tag will be considered to be in the default domain. If a new property is created by using one of the getXXX() methods in this class that takes a default value, then the property data will be written back out to disk. Default properties will be put into the canonical default domain tag.
For new properties that are part of an application and not part of an existing Arch4J domain, application developers can either define their own set of domains or use the default domain. The new format allows developers to create arrays of values for a single property key. When using a Java properties file, the keys have to be unique, as in
primary.colors.1=red primary.colors.2=yellow primary.colors.3=blueand then some contrived method needs to be developed to group by the .1, .2, .3 numbers.
The same property can be defined in XML as
<property key="primary.colors" value="red" type="string" /> <property key="primary.colors" value="yellow" type="string" /> <property key="primary.colors" value="blue" type="string" />and retrieved via the method
getStringArray("primary.colors")
.
Field Summary |
Fields inherited from interface org.arch4j.property.PropertyConstants |
ADD_STRING, BOOLEAN_CHAR, BOOLEAN_NAME, CHAR_CHAR, CHAR_NAME, DEFAULT_DOMAIN_NAME, DOMAIN_CHAR, DOMAIN_TAG_NAME, DOUBLE_CHAR, DOUBLE_NAME, EMPTY_STRING, FALSE_STRING, FLOAT_CHAR, FLOAT_NAME, INT_CHAR, INT_NAME, KEY_CHAR, KEY_TAG_NAME, LONG_CHAR, LONG_NAME, NAME_CHAR, NAME_TAG_NAME, PROPERTY_CHAR, PROPERTY_TAG_NAME, STRING_CHAR, STRING_NAME, TOP_LEVEL_CHAR, TOP_LEVEL_TAG_NAME, TRUE_STRING, TYPE_CHAR, TYPE_TAG_NAME, VALUE_CHAR, VALUE_TAG_NAME, XML_DECLARATION |
Constructor Summary | |
XMLPropertyManager(File propertyFile)
Constructs a XMLPropertyManager , which manages the given property file. |
Method Summary | |
boolean |
getBoolean(String propertyName)
Returns the property value for the given property key as a boolean. |
boolean |
getBoolean(String propertyName,
boolean defaultValue)
Returns the property value for the given property key as a boolean. |
boolean |
getBoolean(String domainName,
String propertyName)
Returns the property value for the given property key as a boolean within the given domain. |
boolean |
getBoolean(String domainName,
String propertyName,
boolean defaultValue)
Returns the property value for the given property key as a boolean within the given domain. |
boolean[] |
getBooleanArray(String propertyName)
Returns an array of boolean property values for the given property key. |
boolean[] |
getBooleanArray(String propertyName,
boolean defaultValue)
Returns an array of boolean property values for the given property key. |
boolean[] |
getBooleanArray(String domainName,
String propertyName)
Returns an array of boolean property values for the given property key within the given domain. |
boolean[] |
getBooleanArray(String domainName,
String propertyName,
boolean defaultValue)
Returns an array of boolean property values for the given property key within the given domain. |
char |
getChar(String propertyName)
Returns the property value for the given property key as a char. |
char |
getChar(String propertyName,
char defaultValue)
Returns the property value for the given property key as a char. |
char |
getChar(String domainName,
String propertyName)
Returns the property value for the given property key as a char within the given domain. |
char |
getChar(String domainName,
String propertyName,
char defaultValue)
Returns the property value for the given property key as a char within the given domain. |
char[] |
getCharArray(String propertyName)
Returns an array of char property values for the given property key. |
char[] |
getCharArray(String propertyName,
char defaultValue)
Returns an array of char property values for the given property key. |
char[] |
getCharArray(String domainName,
String propertyName)
Returns an array of char property values for the given property key within the given domain. |
char[] |
getCharArray(String domainName,
String propertyName,
char defaultValue)
Returns an array of char property values for the given property key within the given domain. |
static String |
getDefaultDomainName()
Returns the string used as the default, global domain name. |
double |
getDouble(String propertyName)
Returns the property value for the given property key as a double. |
double |
getDouble(String propertyName,
double defaultValue)
Returns the property value for the given property key as a double. |
double |
getDouble(String domainName,
String propertyName)
Returns the property value for the given property key as a double within the given domain. |
double |
getDouble(String domainName,
String propertyName,
double defaultValue)
Returns the property value for the given property key as a double within the given domain. |
double[] |
getDoubleArray(String propertyName)
Returns an array of double property values for the given property key. |
double[] |
getDoubleArray(String propertyName,
double defaultValue)
Returns an array of double property values for the given property key. |
double[] |
getDoubleArray(String domainName,
String propertyName)
Returns an array of double property values for the given property key within the given domain. |
double[] |
getDoubleArray(String domainName,
String propertyName,
double defaultValue)
Returns an array of double property values for the given property key within the given domain. |
float |
getFloat(String propertyName)
Returns the property value for the given property key as a float. |
float |
getFloat(String propertyName,
float defaultValue)
Returns the property value for the given property key as a float. |
float |
getFloat(String domainName,
String propertyName)
Returns the property value for the given property key as a float within the given domain. |
float |
getFloat(String domainName,
String propertyName,
float defaultValue)
Returns the property value for the given property key as a float within the given domain. |
float[] |
getFloatArray(String propertyName)
Returns an array of float property values for the given property key. |
float[] |
getFloatArray(String propertyName,
float defaultValue)
Returns an array of float property values for the given property key. |
float[] |
getFloatArray(String domainName,
String propertyName)
Returns an array of float property values for the given property key within the given domain. |
float[] |
getFloatArray(String domainName,
String propertyName,
float defaultValue)
Returns an array of float property values for the given property key within the given domain. |
int |
getInt(String propertyName)
Returns the property value for the given property key as an int. |
int |
getInt(String propertyName,
int defaultValue)
Returns the property value for the given property key as an int. |
int |
getInt(String domainName,
String propertyName)
Returns the property value for the given property key as an int within the given domain. |
int |
getInt(String domainName,
String propertyName,
int defaultValue)
Returns the property value for the given property key as an int within the given domain. |
int[] |
getIntArray(String propertyName)
Returns an array of int property values for the given property key. |
int[] |
getIntArray(String propertyName,
int defaultValue)
Returns an array of int property values for the given property key. |
int[] |
getIntArray(String domainName,
String propertyName)
Returns an array of int property values for the given property key within the given domain. |
int[] |
getIntArray(String domainName,
String propertyName,
int defaultValue)
Returns an array of int property values for the given property key within the given domain. |
long |
getLong(String propertyName)
Returns the property value for the given property key as a long. |
long |
getLong(String propertyName,
long defaultValue)
Returns the property value for the given property key as a long. |
long |
getLong(String domainName,
String propertyName)
Returns the property value for the given property key as a long within the given domain. |
long |
getLong(String domainName,
String propertyName,
long defaultValue)
Returns the property value for the given property key as a long within the given domain. |
long[] |
getLongArray(String propertyName)
Returns an array of long property values for the given property key. |
long[] |
getLongArray(String propertyName,
long defaultValue)
Returns an array of long property values for the given property key. |
long[] |
getLongArray(String domainName,
String propertyName)
Returns an array of long property values for the given property key within the given domain. |
long[] |
getLongArray(String domainName,
String propertyName,
long defaultValue)
Returns an array of long property values for the given property key within the given domain. |
Properties |
getProperties(String forDomain)
Returns all the Properties associated with a given domain. |
String |
getProperty(String forDomain,
String propertyName)
Searches for the property with the specified property name within the given domain. |
String |
getProperty(String forDomain,
String propertyName,
String defaultValue)
Searches for the property with the specified property name within the given domain. |
File |
getPropertyDirectory()
Returns the property directory where property files are stored. |
String |
getString(String propertyName)
Returns the property value for the given property key as a String. |
String |
getString(String propertyName,
String defaultValue)
Returns the property value for the given property key as a String. |
String |
getString(String domainName,
String propertyName,
String defaultValue)
Returns the property value for the given property key as a String within the given domain. |
String[] |
getStringArray(String propertyName)
Returns an array of String property values for the given property key. |
String[] |
getStringArray(String propertyName,
String defaultValue)
Returns an array of String property values for the given property key. |
String[] |
getStringArray(String domainName,
String propertyName,
String defaultValue)
Returns an array of String property values for the given property key within the given domain. |
void |
load()
Parse and load the properties. |
void |
setProperty(String forDomain,
String propertyName,
String propertyValue)
This method sets the given property for a given domain Each time setProperty is called it writes to the file and refreshes the cache If the entry exists the value is changed otherwise a new value is added. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public XMLPropertyManager(File propertyFile)
XMLPropertyManager
, which manages the given property file.
propertyFile
- The XML property file to manage.Method Detail |
public void load()
public static String getDefaultDomainName()
public boolean getBoolean(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public boolean getBoolean(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public boolean getBoolean(String propertyName, boolean defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public boolean getBoolean(String domainName, String propertyName, boolean defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public boolean[] getBooleanArray(String propertyName)
propertyName
- The key to search for.
public boolean[] getBooleanArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public boolean[] getBooleanArray(String propertyName, boolean defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public boolean[] getBooleanArray(String domainName, String propertyName, boolean defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public char getChar(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public char getChar(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public char getChar(String propertyName, char defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public char getChar(String domainName, String propertyName, char defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public char[] getCharArray(String propertyName)
propertyName
- The key to search for.
public char[] getCharArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public char[] getCharArray(String propertyName, char defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public char[] getCharArray(String domainName, String propertyName, char defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public double getDouble(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public double getDouble(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public double getDouble(String propertyName, double defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public double getDouble(String domainName, String propertyName, double defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public double[] getDoubleArray(String propertyName)
propertyName
- The key to search for.
public double[] getDoubleArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public double[] getDoubleArray(String propertyName, double defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public double[] getDoubleArray(String domainName, String propertyName, double defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public float getFloat(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public float getFloat(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public float getFloat(String propertyName, float defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public float getFloat(String domainName, String propertyName, float defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public float[] getFloatArray(String propertyName)
propertyName
- The key to search for.
public float[] getFloatArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public float[] getFloatArray(String propertyName, float defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public float[] getFloatArray(String domainName, String propertyName, float defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public int getInt(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public int getInt(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public int getInt(String propertyName, int defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public int getInt(String domainName, String propertyName, int defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public int[] getIntArray(String propertyName)
propertyName
- The key to search for.
public int[] getIntArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public int[] getIntArray(String propertyName, int defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public int[] getIntArray(String domainName, String propertyName, int defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public long getLong(String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public long getLong(String domainName, String propertyName)
IllegalArgumentException
if the property is not found,
since primitives have no default value.
This method can be used to check the existence of a property without actually
adding a new value. Simply try to get the value and catch the exception.
domainName
- The domain name.propertyName
- The key to search for.
IllegalArgumentException
- if the property was not found.public long getLong(String propertyName, long defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public long getLong(String domainName, String propertyName, long defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public long[] getLongArray(String propertyName)
propertyName
- The key to search for.
public long[] getLongArray(String domainName, String propertyName)
domainName
- The domain name.propertyName
- The key to search for.
public long[] getLongArray(String propertyName, long defaultValue)
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public long[] getLongArray(String domainName, String propertyName, long defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public String getString(String propertyName)
null
if the property is not found.
propertyName
- The key to search for.
null
.public String getString(String propertyName, String defaultValue)
propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public String getString(String domainName, String propertyName, String defaultValue)
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property key is not found.
public String[] getStringArray(String propertyName)
propertyName
- The key to search for.
public String[] getStringArray(String propertyName, String defaultValue)
null
,
a null
will be returned.
propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public String[] getStringArray(String domainName, String propertyName, String defaultValue)
null
,
a null
will be returned.
domainName
- The domain name.propertyName
- The key to search for.defaultValue
- The value to include in the returned array if the property key is not found.
public File getPropertyDirectory()
getPropertyDirectory
in interface PropertyManager
File
.public Properties getProperties(String forDomain)
Properties
associated with a given domain.
If there is more than one value for a given property key, the first one in the array is retrieved.
getProperties
in interface PropertyManager
forDomain
- The domain name.
Properties
object.public String getProperty(String forDomain, String propertyName)
null
if the
property is not found.
getProperty
in interface PropertyManager
forDomain
- The domain name.propertyName
- The key to search for.
public void setProperty(String forDomain, String propertyName, String propertyValue)
setProperty
in interface PropertyManager
forDomain
- The domain name.propertyName
- The key to search for.propertyValue
- The new value for the property (name) key or null.public String getProperty(String forDomain, String propertyName, String defaultValue)
getProperty
in interface PropertyManager
forDomain
- The domain name.propertyName
- The key to search for.defaultValue
- The value to return if the property (name) key is not found.
|
Arch4J 1.1 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |