|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.Dictionary<K,V>
java.util.Hashtable<java.lang.Object,java.lang.Object>
java.util.Properties
messif.utility.ExtendedProperties
public class ExtendedProperties
This class provides getter extension for the basic 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.
Field Summary | |
---|---|
protected static java.util.Map<java.lang.Object,ExtendedProperties> |
cache
Properties cache |
Fields inherited from class java.util.Properties |
---|
defaults |
Constructor Summary | |
---|---|
ExtendedProperties()
Creates an empty property list with no default values. |
|
ExtendedProperties(java.util.Properties defaults)
Creates an empty property list with the specified defaults. |
Method Summary | ||
---|---|---|
ExtendedProperties |
clone(java.util.Properties defaults)
Creates a shallow copy of this properties with the given defaults. |
|
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. |
|
|
getClassProperty(java.lang.String key,
boolean required,
java.lang.Class<E> checkClass)
Returns a generics-safe class from the given property. |
|
|
getConstructor(java.lang.Class<E> objectClass,
java.lang.Class<?>... prototype)
Returns a constructor for a class. |
|
|
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 inet address 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.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 defaultValueRegexGroup,
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. |
Methods inherited from class java.util.Properties |
---|
getProperty, getProperty, list, list, load, load, loadFromXML, propertyNames, save, setProperty, store, store, storeToXML, storeToXML, stringPropertyNames |
Methods inherited from class java.util.Hashtable |
---|
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, rehash, remove, size, toString, values |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final java.util.Map<java.lang.Object,ExtendedProperties> cache
Constructor Detail |
---|
public ExtendedProperties()
public ExtendedProperties(java.util.Properties defaults)
defaults
- the defaultsMethod Detail |
---|
public ExtendedProperties clone(java.util.Properties defaults) throws java.lang.IllegalStateException
defaults
- the defaults
java.lang.IllegalStateException
- if the defaults cannot be setpublic static ExtendedProperties getProperties(java.lang.String file) throws ExtendedPropertiesException
file
- the file to load the properties from
ExtendedPropertiesException
- if there was a problem creating propertiespublic static ExtendedProperties getProperties(java.lang.Class<?> clazz) throws ExtendedPropertiesException
clazz
- the class to load the properties for
ExtendedPropertiesException
- 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 values
ExtendedProperties
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 values
ExtendedProperties
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 keys
ExtendedProperties
populated from properties
public 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 for
java.io.IOException
- if an error occurred when reading from the property file
java.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 load
java.io.IOException
- if an error occurred when reading from the property file
java.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 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 prefix
java.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 values
java.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 defaultValueRegexGroup, 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 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 prefixvariableRegex
- regular expression that matches variablesvariableRegexGroup
- parenthesis group within regular expression
that holds the variable namedefaultValueRegexGroup
- parenthesis group within regular expression
that holds the default value for a variable that is not present
in the variables
mapvariables
- the variable names with their values
java.lang.NullPointerException
- if the properties
is nullpublic java.lang.String getRequiredProperty(java.lang.String key) throws ExtendedPropertiesException
key
a
ExtendedPropertiesException
is thrown instead of returning null.
key
- the hashtable key
key
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 substituted
java.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 key
ExtendedPropertiesException
- 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 null
ExtendedPropertiesException
- if the property's 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's 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 key
ExtendedPropertiesException
- if the property's 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 null
ExtendedPropertiesException
- if the property's 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.
key
- the hashtable key (index number will be appended)count
- the number of items to retrieve
public java.lang.String[] getMultiProperty(java.lang.String key) throws ExtendedPropertiesException
key
.
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)
ExtendedPropertiesException
- if the property's 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 array
ExtendedPropertiesException
- 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 array
ExtendedPropertiesException
- 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 generics check
ExtendedPropertiesException
- if the property was not found or the class with the property's 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's 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 prototype
ExtendedPropertiesException
- 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 prototype
ExtendedPropertiesException
- 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 prototype
ExtendedPropertiesException
- 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 prototype
ExtendedPropertiesException
- 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 prototype
ExtendedPropertiesException
- 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 prototype
ExtendedPropertiesException
- 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
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)
ExtendedPropertiesException
- if the property was not found or the class with the property's 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 key
ExtendedPropertiesException
- if the specified key is not found
in the configuration or there is no JNDI data source with the given URL
java.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 statement
ExtendedPropertiesException
- 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 statement
ExtendedPropertiesException
- if the specified key is not found
java.sql.SQLException
- if the database connection cannot be established
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |