|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.univocity.api.config.PropertyBasedConfiguration
public class PropertyBasedConfiguration
A configuration class based on properties. Properties can contain values that refer to other properties, environment variables, or values provided at runtime. Examples of properties that can be declared in a file:
application.dir=${user.home}/.myApp Property application.dir refers to folder .myApp
under the user's home directory. (user.home here is an environment variable)application.status.dir=${application.dir}/status Here application.status.dir refers to a
status folder under the application directory. Note that property application.dir defined earlier
is used here: its value will be replaced by the evaluated path to the application directory.
application.batch.dir=${application.dir}/batch_!{batch} Here the property uses a batch
variable which is provided at runtime. A client application must call getProperty(String, String...), or
getDirectory(String, boolean, boolean, boolean, boolean, String...) or
getFile(String, boolean, boolean, boolean, boolean, String...) with the string "batch" followed by
a batch number.
logback.configurationFile=config/logback.xml Is a regular property. You can call
setSystemProperty(String) at startup to set the this property as a system property. In this case,
calling setSystemProperty("logback.configurationFile"); at startup will make the logback logger read
from our config/logback.xml file
| Field Summary | |
|---|---|
protected Properties |
properties
|
| Constructor Summary | |
|---|---|
PropertyBasedConfiguration(File inputProperties)
Creates a configuration instance from a File |
|
PropertyBasedConfiguration(InputStream inputProperties)
Creates a configuration instance from an InputStream |
|
PropertyBasedConfiguration(Reader inputProperties)
Creates a configuration instance from a Reader |
|
PropertyBasedConfiguration(String... configurationPaths)
Creates a configuration instance from a list of paths to files containing properties. |
|
| Method Summary | |
|---|---|
boolean |
getBoolean(String property)
Returns the boolean value associated with a property in the configuration |
boolean |
getBoolean(String property,
boolean defaultValue)
Returns the boolean value associated with a property in the configuration |
File |
getDirectory(String property,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a directory, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical directory matches a given criteria (e.g. |
File |
getDirectory(String property,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a directory, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical directory matches a given criteria (e.g. |
File |
getDirectory(String property,
File defaultDir,
boolean validateRead,
boolean validateWrite,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a directory, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical directory matches a given criteria (e.g. |
File |
getFile(String property,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a file, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical file matches a given criteria (e.g. |
File |
getFile(String property,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a file, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical file matches a given criteria (e.g. |
File |
getFile(String property,
File defaultFile,
boolean validateRead,
boolean validateWrite,
String... keyValuePairs)
Given a property of the configuration, reads the property value as a path to a file, replacing any variables between '!{' and '}', and returns an instance of File for that path, ensuring the
physical file matches a given criteria (e.g. |
Integer |
getInteger(String property)
Returns the Integer value associated with a property in the configuration |
Integer |
getInteger(String property,
Integer defaultValue)
Returns the Integer value associated with a property in the configuration |
List<String> |
getList(String property)
Returns a List of values associated with a property in the configuration. |
List<String> |
getList(String property,
String separator)
Returns a List of values associated with a property in the configuration |
protected String |
getPropertiesDescription()
Describes the sort of configuration managed by this class. |
String |
getProperty(String property)
Returns the value associated with a property in the configuration |
String |
getProperty(String property,
String... keyValuePairs)
Returns the value associated with a property in the configuration, replacing variables between '!{' and '}'. |
String |
getProperty(String property,
String defaultValue)
Returns the value associated with a property in the configuration |
File |
getValidatedDirectory(String pathToDir,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create)
Given a path to a directory, returns an instance of File for that path, ensuring the physical
directory matches a given criteria (e.g. |
File |
getValidatedFile(String pathToFile,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create)
Given a path to a file, returns an instance of File for that path, ensuring the physical file
matches a given criteria (e.g. |
protected List<String> |
listVariables(String s)
Parses a string to find variables between '${' and '}' |
String |
normalizeFilePath(String filePath)
Replaces pairs of backslashes in a file path to a single forward slash . |
protected String |
replaceVariables(String s,
String variable,
String value)
Replaces a variable inside '${' and '}' within a String with a value. |
void |
setSystemProperty(String property)
Sets a given property of the configuration as a system property. |
void |
setSystemProperty(String property,
boolean override)
Sets a given property of the configuration as a system property. |
String |
toString()
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected final Properties properties
| Constructor Detail |
|---|
public PropertyBasedConfiguration(InputStream inputProperties)
throws IllegalConfigurationException
InputStream
inputProperties - an input with properties.
IllegalConfigurationException - if the input can't be read
public PropertyBasedConfiguration(Reader inputProperties)
throws IllegalConfigurationException
Reader
inputProperties - an input with properties.
IllegalConfigurationException - if the input can't be read
public PropertyBasedConfiguration(File inputProperties)
throws IllegalConfigurationException
File
inputProperties - an input with properties.
IllegalConfigurationException - if the input can't be read
public PropertyBasedConfiguration(String... configurationPaths)
throws IllegalConfigurationException
configurationPaths - the sequence of path of configuration files that this class will attempt to load.
IllegalConfigurationException - if none of the given paths indicate a file or resource with properties.| Method Detail |
|---|
public final String toString()
toString in class Objectprotected String getPropertiesDescription()
protected final String replaceVariables(String s,
String variable,
String value)
String with a value.
s - a string that may contain the given variablevariable - the variable name, present in the script within '${' and '}'value - the value that should be used to replace the variable
protected final List<String> listVariables(String s)
s - the input string
public final void setSystemProperty(String property)
setSystemProperty(String, boolean) to override existing system properties.
property - the property contained in the configuration that should become a system property
public final void setSystemProperty(String property,
boolean override)
property - the property contained in the configuration that should become a system propertyoverride - flag indicating whether to override any value already associated with the given system property.
public final String getProperty(String property,
String defaultValue)
property - the property namedefaultValue - a default value to return in case the property is not defined in the configuration
public final String getProperty(String property)
throws IllegalConfigurationException
property - the property name
IllegalConfigurationException - if the property is not present in the configuration.
public final String getProperty(String property,
String... keyValuePairs)
throws IllegalConfigurationException
my.property has value /tmp/!{batch}/!{date}/, and you call
getProperty("my.property", "batch", "1234", "date", "2015-DEC-25"); the result will be "/tmp/1234/2015-DEC-25/"
property - the property namekeyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value.
IllegalConfigurationException - if the property is not present in the configuration.public final String normalizeFilePath(String filePath)
filePath - a path to a file
public final File getValidatedFile(String pathToFile,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create)
throws IllegalConfigurationException
File for that path, ensuring the physical file
matches a given criteria (e.g. it must exist, be readable, writable, etc)
pathToFile - path to the desired filemandatory - flag indicating whether the path is mandatory. If a null path is
given, this method will return null of this flag is set to false, otherwise
an IllegalConfigurationException will be thrown.validateRead - flag indicating whether the file must have read permissions.
An IllegalConfigurationException will be thrown if the file is not readable.validateWrite - flag indicating whether the file must have write permissions.
An IllegalConfigurationException will be thrown if the file is not writable.create - A flag indicating whether the file must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the file couldn't be created.
IllegalConfigurationException - if a validation fails
public final File getValidatedDirectory(String pathToDir,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create)
throws IllegalConfigurationException
File for that path, ensuring the physical
directory matches a given criteria (e.g. it must exist, be readable, writable, etc)
pathToDir - path to the desired directorymandatory - flag indicating whether the path is mandatory. If a null path is
given, this method will return null of this flag is set to false, otherwise
an IllegalConfigurationException will be thrown.validateRead - flag indicating whether the directory must have read permissions.
An IllegalConfigurationException will be thrown if the directory is not readable.validateWrite - flag indicating whether the directory must have write permissions.
An IllegalConfigurationException will be thrown if the directory is not writable.create - A flag indicating whether the directory must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the directory couldn't be created.
IllegalConfigurationException - if a validation fails
public final File getDirectory(String property,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical directory matches a given criteria (e.g. it must exist, be readable, writable, etc).
An IllegalConfigurationException will be thrown if the property has no valid value associated.
property - name of a property whose value is expected to contain a path to a directory
an IllegalConfigurationException will be thrown.validateRead - flag indicating whether the directory must have read permissions.
An IllegalConfigurationException will be thrown if the directory is not readable.validateWrite - flag indicating whether the directory must have write permissions.
An IllegalConfigurationException will be thrown if the directory is not writable.create - A flag indicating whether the directory must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the directory couldn't be created.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the directory path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}/, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25/"
IllegalConfigurationException - if a validation fails
public final File getDirectory(String property,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical directory matches a given criteria (e.g. it must exist, be readable, writable, etc).
property - name of a property whose value is expected to contain a path to a directory
an IllegalConfigurationException will be thrown.mandatory - flag indicating whether the path is mandatory. If a null path is
given, this method will return null of this flag is set to false, otherwisevalidateRead - flag indicating whether the directory must have read permissions.
An IllegalConfigurationException will be thrown if the directory is not readable.validateWrite - flag indicating whether the directory must have write permissions.
An IllegalConfigurationException will be thrown if the directory is not writable.create - A flag indicating whether the directory must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the directory couldn't be created.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the directory path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}/, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25/"
IllegalConfigurationException - if a validation fails
public final File getDirectory(String property,
File defaultDir,
boolean validateRead,
boolean validateWrite,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical directory matches a given criteria (e.g. it must exist, be readable, writable, etc).
property - name of a property whose value is expected to contain a path to a directory
an IllegalConfigurationException will be thrown.defaultDir - a default directory to return if the property has no path associated with it.validateRead - flag indicating whether the directory must have read permissions.
An IllegalConfigurationException will be thrown if the directory is not readable.validateWrite - flag indicating whether the directory must have write permissions.
An IllegalConfigurationException will be thrown if the directory is not writable.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the directory path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}/, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25/"
IllegalConfigurationException - if a validation fails
public final File getFile(String property,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical file matches a given criteria (e.g. it must exist, be readable, writable, etc).
property - name of a property whose value is expected to contain a path to a file
an IllegalConfigurationException will be thrown.validateRead - flag indicating whether the file must have read permissions.
An IllegalConfigurationException will be thrown if the file is not readable.validateWrite - flag indicating whether the file must have write permissions.
An IllegalConfigurationException will be thrown if the file is not writable.create - A flag indicating whether the file must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the file couldn't be created.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the file path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}.csv, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25.csv"
IllegalConfigurationException - if a validation fails
public final File getFile(String property,
boolean mandatory,
boolean validateRead,
boolean validateWrite,
boolean create,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical file matches a given criteria (e.g. it must exist, be readable, writable, etc).
property - name of a property whose value is expected to contain a path to a file
an IllegalConfigurationException will be thrown.mandatory - flag indicating whether the path is mandatory. If a null path is
given, this method will return null of this flag is set to false, otherwisevalidateRead - flag indicating whether the file must have read permissions.
An IllegalConfigurationException will be thrown if the file is not readable.validateWrite - flag indicating whether the file must have write permissions.
An IllegalConfigurationException will be thrown if the file is not writable.create - A flag indicating whether the file must be created if it doesn't exist. In case the path
contains a directory that doesn't exist, the parent directory will be created as well.
An IllegalConfigurationException will be thrown if the file couldn't be created.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the file path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}.csv, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25.csv"
IllegalConfigurationException - if a validation fails
public final File getFile(String property,
File defaultFile,
boolean validateRead,
boolean validateWrite,
String... keyValuePairs)
throws IllegalConfigurationException
File for that path, ensuring the
physical file matches a given criteria (e.g. it must exist, be readable, writable, etc).
property - name of a property whose value is expected to contain a path to a file
an IllegalConfigurationException will be thrown.defaultFile - a default file to return if the property has no path associated with it.validateRead - flag indicating whether the file must have read permissions.
An IllegalConfigurationException will be thrown if the file is not readable.validateWrite - flag indicating whether the file must have write permissions.
An IllegalConfigurationException will be thrown if the file is not writable.keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be
part of the property value. Matching variables in the file path will be replaced by
the values given in the key value pairs, e.g. if the property has
value /tmp/!{batch}/!{date}.csv, and the key value pairs are "batch", "1234",
"date", "2015-DEC-25", the result will be "/tmp/1234/2015-DEC-25.csv"
IllegalConfigurationException - if a validation failspublic final Integer getInteger(String property)
Integer value associated with a property in the configuration
property - the property name
null if no value is provided.
IllegalConfigurationException - if the property is not present in the configuration.
public final Integer getInteger(String property,
Integer defaultValue)
Integer value associated with a property in the configuration
property - the property namedefaultValue - a default value to return in case the property is not present in the configuration
public final List<String> getList(String property)
List of values associated with a property in the configuration. Assumes the values are
separated by comma.
property - the property name
IllegalConfigurationException - if the property is not present in the configuration.
public final List<String> getList(String property,
String separator)
List of values associated with a property in the configuration
property - the property nameseparator - the separator that delimits individual values associated with the property.
IllegalConfigurationException - if the property is not present in the configuration.
public final boolean getBoolean(String property,
boolean defaultValue)
boolean value associated with a property in the configuration
property - the property namedefaultValue - a default value to return in case the property is not present in the configuration
public final boolean getBoolean(String property)
boolean value associated with a property in the configuration
property - the property name
false if no value is provided.
IllegalConfigurationException - if the property is not present in the configuration.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||