public class ExtendedProperties
extends java.util.Properties
set of properties
.
The following features are added:
ClassLoader
.
In addition, a caching facility is provided if the
factory method
is used, i.e.
the same ExtendedProperties
instance is returned for the same resource.
Modifier and Type | Field and Description |
---|---|
protected static java.util.Map<java.lang.Object,ExtendedProperties> |
cache
Properties cache
|
Constructor and Description |
---|
ExtendedProperties()
Creates an empty property list with no default values.
|
ExtendedProperties(java.util.Properties defaults)
Creates an empty property list with the specified defaults.
|
Modifier and Type | Method and Description |
---|---|
ExtendedProperties |
clone(java.util.Properties defaults)
Creates a shallow copy of this properties with the given defaults.
|
static ExtendedProperties |
createPropertiesFromMap(java.util.Map<java.lang.String,java.lang.String> map)
Return a new instance of
ExtendedProperties loaded from the specified map . |
static ExtendedProperties |
createPropertiesWithClassDefault(java.util.Properties properties,
java.lang.Class<?> clazz)
Creates a new instance of
ExtendedProperties from the given properties . |
int |
fillByMultiProperty(java.lang.String key,
java.lang.Object[] parameters,
java.lang.Class<?>[] parameterTypes)
Fill the specified array with property values converted to appropriate types.
|
boolean |
getBoolProperty(java.lang.String key,
boolean defaultValue)
Returns a boolean value from the given property.
|
java.lang.Class<?> |
getClassProperty(java.lang.String key,
boolean required)
Returns a class from the given property.
|
<E> java.lang.Class<E> |
getClassProperty(java.lang.String key,
boolean required,
java.lang.Class<E> checkClass)
Returns a generic-safe class from the given property.
|
<E> java.lang.reflect.Constructor<E> |
getConstructor(java.lang.Class<E> objectClass,
java.lang.Class<?>... prototype)
Returns a constructor for a class.
|
<E> java.lang.reflect.Constructor<E> |
getConstructor(java.lang.Class<E> checkClass,
java.lang.String classKeyName,
java.lang.Class<?>... prototype)
Returns a constructor for a class.
|
java.lang.reflect.Constructor<?> |
getConstructor(java.lang.String classKeyName,
java.lang.Class<?>... prototype)
Returns a constructor for a class.
|
java.sql.Connection |
getDatabaseConnection(java.lang.String key)
Returns a data source using either JNDI or
DriverManager using the
URL specified in the value of the property key . |
java.lang.reflect.Method |
getFactoryMethod(java.lang.Class<?> factoryClass,
java.lang.Class<?> returnType,
java.lang.String methodName,
java.lang.Class<?>... prototype)
Returns a
className 's factory method with the specified name and prototype. |
java.lang.reflect.Method |
getFactoryMethod(java.lang.Class<?> returnType,
java.lang.String classKeyName,
java.lang.String methodKeyName,
java.lang.Class<?>... prototype)
Returns a factory method for a class.
|
java.lang.reflect.Method |
getFactoryMethod(java.lang.String classKeyName,
java.lang.String methodKeyName,
java.lang.Class<?>... prototype)
Returns a factory method for a class.
|
java.lang.String |
getFormattedProperty(java.lang.String key,
java.lang.String defaultValue,
java.lang.Object... parameters)
Formats a message using the property with the specified key in this property list.
|
java.net.InetAddress |
getInetAddressProperty(java.lang.String key,
boolean required)
Returns an
InetAddress from the given property. |
int |
getIntProperty(java.lang.String key,
int defaultValue)
Returns an integer value from the given property.
|
int |
getIntProperty(java.lang.String key,
int defaultValue,
int minValue,
int maxValue)
Returns an integer value from the given property.
|
java.lang.String[] |
getMultiProperty(java.lang.String key)
Returns an array of property values.
|
java.lang.Object[] |
getMultiProperty(java.lang.String key,
java.lang.Class<?>... parameterTypes)
Returns an array of property values converted to appropriate types.
|
java.lang.String[] |
getMultiProperty(java.lang.String key,
int count)
Returns an array of property values.
|
static ExtendedProperties |
getProperties(java.lang.Class<?> clazz)
Returns a cached instance of ExtendedProperties for the specified class.
|
static ExtendedProperties |
getProperties(java.lang.String file)
Returns a cached instance of ExtendedProperties for the specified file.
|
boolean |
getRequiredBoolProperty(java.lang.String key)
Returns a boolean value from the given property.
|
int |
getRequiredIntProperty(java.lang.String key)
Returns an integer value from the given property.
|
java.lang.String |
getRequiredProperty(java.lang.String key)
Searches for the property with the specified key in this property list.
|
java.lang.Object |
getSerializedObject(java.lang.String fileNameProperty,
boolean required)
Reads a serialized object from the file specified by the given property.
|
java.sql.PreparedStatement |
getSQLStatement(java.sql.Connection connection,
java.lang.String key)
Prepare an SQL statement from the configuration.
|
java.sql.PreparedStatement |
getSQLStatement(java.lang.String connectionKey,
java.lang.String sqlStatementKey)
Prepare an SQL statement from the configuration.
|
boolean |
load(java.lang.Class<?> clazz)
Populate this properties with the data stored in a
clazz 's property file. |
boolean |
load(java.lang.ClassLoader loader,
java.lang.String resourceName)
Populate this properties with the data stored in
resourceName . |
void |
load(java.util.Map<java.lang.String,java.lang.String> map)
Populate this properties with the data from a
Map . |
void |
load(java.util.Properties properties,
java.lang.String prefix)
Populate this properties with the data stored in another
properties . |
void |
load(java.util.Properties properties,
java.lang.String prefix,
java.util.Map<java.lang.String,java.lang.String> variables)
Populate this properties with the data stored in another
properties . |
void |
load(java.util.Properties properties,
java.lang.String prefix,
java.util.regex.Pattern variableRegex,
int variableRegexGroup,
int flagsRegexpGroup,
java.util.Map<java.lang.String,java.lang.String> variables)
Populate this properties with the data stored in another
properties . |
static ExtendedProperties |
restrictProperties(java.util.Properties properties,
java.lang.String prefix)
Return
ExtendedProperties from the specified properties . |
static ExtendedProperties |
restrictProperties(java.util.Properties properties,
java.lang.String prefix,
java.util.Map<java.lang.String,java.lang.String> variables)
Return
ExtendedProperties from the specified properties with variable expansion. |
static ExtendedProperties |
restrictProperties(java.util.Properties properties,
java.lang.String prefix,
java.util.Properties defaultProperties,
java.util.Map<java.lang.String,java.lang.String> variables)
Return
ExtendedProperties from the specified properties with variable expansion. |
getProperty, getProperty, list, list, load, load, loadFromXML, propertyNames, save, setProperty, store, store, storeToXML, storeToXML, stringPropertyNames
protected static final java.util.Map<java.lang.Object,ExtendedProperties> cache
public ExtendedProperties()
public ExtendedProperties(java.util.Properties defaults)
defaults
- the defaultspublic ExtendedProperties clone(java.util.Properties defaults) throws java.lang.IllegalStateException
defaults
- the defaultsjava.lang.IllegalStateException
- if the defaults cannot be setpublic static ExtendedProperties getProperties(java.lang.String file) throws ExtendedPropertiesException
file
- the file to load the properties fromExtendedPropertiesException
- if there was a problem creating propertiespublic static ExtendedProperties getProperties(java.lang.Class<?> clazz) throws ExtendedPropertiesException
clazz
- the class to load the properties forExtendedPropertiesException
- if there was a problem creating propertiespublic static ExtendedProperties restrictProperties(java.util.Properties properties, java.lang.String prefix, java.util.Properties defaultProperties, java.util.Map<java.lang.String,java.lang.String> variables)
ExtendedProperties
from the specified properties
with variable expansion.
Only keys with the given prefix are copied and the prefix is removed from the keys.
A variable substitution
is
performed on all values (no substitution is done on keys).properties
- the properties to copy fromprefix
- the starting prefix on the keysdefaultProperties
- the default properties to use (see Properties.Properties(java.util.Properties)
)variables
- the variable names with their valuesExtendedProperties
populated from properties
public static ExtendedProperties restrictProperties(java.util.Properties properties, java.lang.String prefix, java.util.Map<java.lang.String,java.lang.String> variables)
ExtendedProperties
from the specified properties
with variable expansion.
Only keys with the given prefix are copied and the prefix is removed from the keys.
A variable substitution
is
performed on all values (no substitution is done on keys).properties
- the properties to copy fromprefix
- the starting prefix on the keysvariables
- the variable names with their valuesExtendedProperties
populated from properties
public static ExtendedProperties restrictProperties(java.util.Properties properties, java.lang.String prefix)
ExtendedProperties
from the specified properties
.
Only keys with the given prefix are copied and the prefix is removed from the
keys.properties
- the properties to copy fromprefix
- the starting prefix on the keysExtendedProperties
populated from properties
public static ExtendedProperties createPropertiesFromMap(java.util.Map<java.lang.String,java.lang.String> map)
ExtendedProperties
loaded from the specified map
.map
- the map of key-value pairs to load into the propertiesExtendedProperties
public static ExtendedProperties createPropertiesWithClassDefault(java.util.Properties properties, java.lang.Class<?> clazz) throws java.lang.IllegalArgumentException
ExtendedProperties
from the given properties
.
If a clazz
is given and a property file for that class exists, it
is used as properties default for the new instance.properties
- the properties to load into the new instanceclazz
- the class to load the properties forExtendedProperties
java.lang.IllegalArgumentException
- if there was an error reading the class propertiespublic boolean load(java.lang.Class<?> clazz) throws java.io.IOException, java.lang.IllegalArgumentException
clazz
's property file.
That is, the file with the same package, the same name as the clazz
and the "properties" extension.clazz
- the class to load the properties forjava.io.IOException
- if an error occurred when reading from the property filejava.lang.IllegalArgumentException
- if the property file contains a malformed Unicode escape sequencepublic boolean load(java.lang.ClassLoader loader, java.lang.String resourceName) throws java.io.IOException, java.lang.IllegalArgumentException
resourceName
.loader
- the class loader to use to access the resourceresourceName
- the name of a resource to loadjava.io.IOException
- if an error occurred when reading from the property filejava.lang.IllegalArgumentException
- if the property file contains a malformed unicode escape sequencepublic void load(java.util.Properties properties, java.lang.String prefix) throws java.lang.NullPointerException
properties
.properties
- the properties to loadprefix
- if null, all keys from the properties
are copied; otherwise, only the keys that starts with the prefix
are copied without the prefixjava.lang.NullPointerException
- if the properties
is nullpublic void load(java.util.Properties properties, java.lang.String prefix, java.util.Map<java.lang.String,java.lang.String> variables) throws java.lang.NullPointerException
properties
.
Variable substitution is performed on property values (keys are not substituted).
Variables have <name:default_value>
format, where the :default_value
part is optional (empty string will be placed if an unknown variable is encountered).
If a prefix
is specified, only keys that begins with that prefix are
copied and the prefix is removed in the process.properties
- the propertries to loadprefix
- if null, all keys from the properties
are copied; otherwise, only the keys that starts with the prefix
are copied without the prefixvariables
- the variable names with their valuesjava.lang.NullPointerException
- if the properties
is nullpublic void load(java.util.Properties properties, java.lang.String prefix, java.util.regex.Pattern variableRegex, int variableRegexGroup, int flagsRegexpGroup, java.util.Map<java.lang.String,java.lang.String> variables) throws java.lang.NullPointerException
properties
.
Variable substitution is performed on property values (keys are not substituted).
If a prefix
is specified, only keys that begins with that prefix are
copied and the prefix is removed in the process.properties
- the properties to loadprefix
- if null, all keys from the properties
are copied; otherwise, only the keys that starts with the prefix
are copied without the prefixvariableRegex
- regular expression that matches variablesvariableRegexGroup
- parenthesis group within regular expression
that holds the variable nameflagsRegexpGroup
- parenthesis group within regular expression
that holds flags:
":defaultValue" the default value for a variable that is not present in the variables
map,
"!" the value is requiredvariables
- the variable names with their valuesjava.lang.NullPointerException
- if the properties
is nullpublic void load(java.util.Map<java.lang.String,java.lang.String> map)
Map
.
Note that any existing keys are replaced.map
- the map of key-value pairs to loadpublic java.lang.String getRequiredProperty(java.lang.String key) throws ExtendedPropertiesException
key
a
ExtendedPropertiesException
is thrown instead of returning null.key
- the hashtable keykey
ExtendedPropertiesException
- if the value was not found and default value is nullpublic java.lang.String getFormattedProperty(java.lang.String key, java.lang.String defaultValue, java.lang.Object... parameters) throws java.lang.IllegalArgumentException
key
the defaultValue
is used.
The message is then formatted according to MessageFormat.format(java.lang.Object[], java.lang.StringBuffer, java.text.FieldPosition)
.key
- the hashtable key of the messagedefaultValue
- a default value if the property key
is nullparameters
- an array of objects to be formatted and substitutedjava.lang.IllegalArgumentException
- if a parameter in the parameters
array is not of the type expected by the format element(s) that use itpublic int getRequiredIntProperty(java.lang.String key) throws ExtendedPropertiesException
ExtendedPropertiesException
is thrown.key
- the hashtable keyExtendedPropertiesException
- if the property was not found or its value is not a valid integerpublic int getIntProperty(java.lang.String key, int defaultValue) throws ExtendedPropertiesException
key
- the hashtable keydefaultValue
- a default value if the property key
is nullExtendedPropertiesException
- if the property value is not a valid integerpublic int getIntProperty(java.lang.String key, int defaultValue, int minValue, int maxValue) throws ExtendedPropertiesException
key
- the hashtable keydefaultValue
- a default value if the property key
is nullminValue
- maxValue
- ExtendedPropertiesException
- if the property value is not a valid integer or it is out of rangepublic boolean getRequiredBoolProperty(java.lang.String key) throws ExtendedPropertiesException
ExtendedPropertiesException
is thrown.key
- the hashtable keyExtendedPropertiesException
- if the property value is not a valid integerpublic boolean getBoolProperty(java.lang.String key, boolean defaultValue) throws ExtendedPropertiesException
key
- the hashtable keydefaultValue
- a default value if the property key
is nullExtendedPropertiesException
- if the property value is not a valid integerpublic java.lang.String[] getMultiProperty(java.lang.String key, int count)
count
items and each item
will be filled with value of the property key#
, where #
is an index of the respective item starting from 1. If count
is negative, the properties key#
are tried until the last
one is found.key
- the hashtable key (index number will be appended)count
- the number of items to retrievepublic java.lang.String[] getMultiProperty(java.lang.String key) throws ExtendedPropertiesException
key
.
Each item will be filled with value of the property key#
, where #
is an index of the respective item starting from 1.
If the property key
does not exist, properties key#
are tried until the last one is found.key
- the hashtable key (index number will be appended)ExtendedPropertiesException
- if the property value is not a non-negative integerpublic java.lang.Object[] getMultiProperty(java.lang.String key, java.lang.Class<?>... parameterTypes) throws ExtendedPropertiesException
stringToType
method.
The returned array will have exactly parameterTypes.length
items
and each item will be filled with value of the property key#
, where #
is a zero-based index of the respective item.key
- the hashtable key (index number will be appended)parameterTypes
- the parameter types arrayExtendedPropertiesException
- if the array of values for the property cannot be converted to the specified typespublic int fillByMultiProperty(java.lang.String key, java.lang.Object[] parameters, java.lang.Class<?>[] parameterTypes) throws ExtendedPropertiesException
stringToType
method. Each item of the array will be filled with value of the property key#
, where #
is a zero-based index of the respective item. If the property does not exist, null
value will be placed in the parameters.key
- the hashtable key (index number will be appended)parameters
- the parameter array to fillparameterTypes
- the parameter types arrayExtendedPropertiesException
- if the array of values for the property cannot be converted to the specified typespublic <E> java.lang.Class<E> getClassProperty(java.lang.String key, boolean required, java.lang.Class<E> checkClass) throws ExtendedPropertiesException
E
- the superclass of the returned classkey
- the hashtable keyrequired
- if true the property must exist (and null will be never returned)checkClass
- the superclass of the returned class for the generic checkExtendedPropertiesException
- if the property was not found or the class with the property value cannot be resolvedpublic java.lang.Class<?> getClassProperty(java.lang.String key, boolean required) throws ExtendedPropertiesException
key
- the hashtable keyrequired
- if true the property must exist (and null will be never returned)ExtendedPropertiesException
- if the property was not found or the class with the property value cannot be resolvedpublic java.lang.reflect.Method getFactoryMethod(java.lang.Class<?> returnType, java.lang.String classKeyName, java.lang.String methodKeyName, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
classKeyName
.
The static factory method is then searched with the name from this properties using key
methodKeyName
and the given prototype.returnType
- the superclass (or class itself) of instances returned by the static factory methodclassKeyName
- the key looked up in this properties to get the class namemethodKeyName
- the key looked up in this properties to get the factory method nameprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic java.lang.reflect.Method getFactoryMethod(java.lang.String classKeyName, java.lang.String methodKeyName, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
classKeyName
.
The static factory method is then searched with the name from this properties using key
methodKeyName
and the given prototype.classKeyName
- the key looked up in this properties to get the class namemethodKeyName
- the key looked up in this properties to get the factory method nameprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic java.lang.reflect.Method getFactoryMethod(java.lang.Class<?> factoryClass, java.lang.Class<?> returnType, java.lang.String methodName, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
className
's factory method with the specified name and prototype.
The factory method must be static and must return the specified returnType
.
If the returnType
is null, the factory method must return factoryClass
.factoryClass
- the class the get the factory method forreturnType
- the superclass (or class itself) of instances returned by the static factory methodmethodName
- the factory method nameprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic <E> java.lang.reflect.Constructor<E> getConstructor(java.lang.Class<E> checkClass, java.lang.String classKeyName, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
classKeyName
.
The constructor is then searched with the given prototype.E
- the type of the object for which the constructor is retrievedclassKeyName
- the key looked up in this properties to get the class namecheckClass
- the superclass (or class itself) of instances created by the constructorprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic java.lang.reflect.Constructor<?> getConstructor(java.lang.String classKeyName, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
classKeyName
.
The constructor is then searched with the given prototype.classKeyName
- the key looked up in this properties to get the class nameprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic <E> java.lang.reflect.Constructor<E> getConstructor(java.lang.Class<E> objectClass, java.lang.Class<?>... prototype) throws ExtendedPropertiesException
classKeyName
.
The constructor is then searched with the given prototype.E
- the type of the object for which the constructor is retrievedobjectClass
- the class the get the constructor forprototype
- the factory method's prototypeExtendedPropertiesException
- if there was an error resolving the class or the factory methodpublic java.lang.Object getSerializedObject(java.lang.String fileNameProperty, boolean required) throws ExtendedPropertiesException
fileNameProperty
- the name of the property where the file path is storedrequired
- if true, the property is required to exist (otherwise and exception is thrown)ExtendedPropertiesException
- if the required property does not exist or there was a problem deserializing the objectpublic java.net.InetAddress getInetAddressProperty(java.lang.String key, boolean required) throws ExtendedPropertiesException
InetAddress
from the given property.
The property value can either be a machine name or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.
key
- the hashtable keyrequired
- if true the property must exist (and null will be never returned)InetAddress
from the given propertyExtendedPropertiesException
- if the property was not found or the class with the property value cannot be resolvedpublic java.sql.Connection getDatabaseConnection(java.lang.String key) throws ExtendedPropertiesException, java.sql.SQLException
DriverManager
using the
URL specified in the value of the property key
. If the URL
starts with "java:", JNDI is looked up, otherwise the driver manager is used
(the driver for the URL must be registered in advance).key
- the hashtable keyExtendedPropertiesException
- if the specified key is not found
in the configuration or there is no JNDI data source with the given URLjava.sql.SQLException
- if the database connection cannot be establishedpublic java.sql.PreparedStatement getSQLStatement(java.lang.String connectionKey, java.lang.String sqlStatementKey) throws ExtendedPropertiesException, java.sql.SQLException
getDatabaseConnection(java.lang.String)
.connectionKey
- the hashtable key for the connection URLsqlStatementKey
- the hashtable key for the SQL statementExtendedPropertiesException
- if the specified keys are not found
or there is no valid database connection for the connectionKey
java.sql.SQLException
- if there was an error preparing the SQL statementpublic java.sql.PreparedStatement getSQLStatement(java.sql.Connection connection, java.lang.String key) throws java.sql.SQLException
connection
- the database connection for which to prepare SQL statementkey
- the hashtable key for the SQL statementExtendedPropertiesException
- if the specified key is not foundjava.sql.SQLException
- if the database connection cannot be established