com.univocity.api.config
Class PropertyBasedConfiguration

java.lang.Object
  extended by com.univocity.api.config.PropertyBasedConfiguration

public class PropertyBasedConfiguration
extends Object

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:

Author:
uniVocity Software Pty Ltd - dev@univocity.com

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

properties

protected final Properties properties
Constructor Detail

PropertyBasedConfiguration

public PropertyBasedConfiguration(InputStream inputProperties)
                           throws IllegalConfigurationException
Creates a configuration instance from an InputStream

Parameters:
inputProperties - an input with properties.
Throws:
IllegalConfigurationException - if the input can't be read

PropertyBasedConfiguration

public PropertyBasedConfiguration(Reader inputProperties)
                           throws IllegalConfigurationException
Creates a configuration instance from a Reader

Parameters:
inputProperties - an input with properties.
Throws:
IllegalConfigurationException - if the input can't be read

PropertyBasedConfiguration

public PropertyBasedConfiguration(File inputProperties)
                           throws IllegalConfigurationException
Creates a configuration instance from a File

Parameters:
inputProperties - an input with properties.
Throws:
IllegalConfigurationException - if the input can't be read

PropertyBasedConfiguration

public PropertyBasedConfiguration(String... configurationPaths)
                           throws IllegalConfigurationException
Creates a configuration instance from a list of paths to files containing properties. Once a file is found it will be loaded and the remainder of these paths will be ignored. Each path will be attempted to be read twice: first as an absolute path (i.e. as a file of the filesystem) and then as a relative path (i.e. as a resource of the application). If no files are found in either attempts the next path in the list will be tried, and so on.

Parameters:
configurationPaths - the sequence of path of configuration files that this class will attempt to load.
Throws:
IllegalConfigurationException - if none of the given paths indicate a file or resource with properties.
Method Detail

toString

public final String toString()
Overrides:
toString in class Object

getPropertiesDescription

protected String getPropertiesDescription()
Describes the sort of configuration managed by this class.

Returns:
a description of the configuration

replaceVariables

protected final String replaceVariables(String s,
                                        String variable,
                                        String value)
Replaces a variable inside '${' and '}' within a String with a value.

Parameters:
s - a string that may contain the given variable
variable - the variable name, present in the script within '${' and '}'
value - the value that should be used to replace the variable
Returns:
the transformed string, with values in place of the given variables.

listVariables

protected final List<String> listVariables(String s)
Parses a string to find variables between '${' and '}'

Parameters:
s - the input string
Returns:
the list of variables found

setSystemProperty

public final void setSystemProperty(String property)
Sets a given property of the configuration as a system property. Existing existing system properties are not overridden. Use setSystemProperty(String, boolean) to override existing system properties.

Parameters:
property - the property contained in the configuration that should become a system property

setSystemProperty

public final void setSystemProperty(String property,
                                    boolean override)
Sets a given property of the configuration as a system property.

Parameters:
property - the property contained in the configuration that should become a system property
override - flag indicating whether to override any value already associated with the given system property.

getProperty

public final String getProperty(String property,
                                String defaultValue)
Returns the value associated with a property in the configuration

Parameters:
property - the property name
defaultValue - a default value to return in case the property is not defined in the configuration
Returns:
the property value, if present in the configuration, or the default value in case the property doesn't exist.

getProperty

public final String getProperty(String property)
                         throws IllegalConfigurationException
Returns the value associated with a property in the configuration

Parameters:
property - the property name
Returns:
the property value
Throws:
IllegalConfigurationException - if the property is not present in the configuration.

getProperty

public final String getProperty(String property,
                                String... keyValuePairs)
                         throws IllegalConfigurationException
Returns the value associated with a property in the configuration, replacing variables between '!{' and '}'. If property 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/"

Parameters:
property - the property name
keyValuePairs - a list of key an value pairs with values for variables between '!{' and '}' that might be part of the property value.
Returns:
the property value with the variables replaced.
Throws:
IllegalConfigurationException - if the property is not present in the configuration.

normalizeFilePath

public final String normalizeFilePath(String filePath)
Replaces pairs of backslashes in a file path to a single forward slash .

Parameters:
filePath - a path to a file
Returns:
the path with forward slashes only

getValidatedFile

public final File getValidatedFile(String pathToFile,
                                   boolean mandatory,
                                   boolean validateRead,
                                   boolean validateWrite,
                                   boolean create)
                            throws IllegalConfigurationException
Given a path to a file, returns an instance of File for that path, ensuring the physical file matches a given criteria (e.g. it must exist, be readable, writable, etc)

Parameters:
pathToFile - path to the desired file
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, 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.
Returns:
the validated file represented by the given path.
Throws:
IllegalConfigurationException - if a validation fails

getValidatedDirectory

public final File getValidatedDirectory(String pathToDir,
                                        boolean mandatory,
                                        boolean validateRead,
                                        boolean validateWrite,
                                        boolean create)
                                 throws IllegalConfigurationException
Given a path to a directory, returns an instance of File for that path, ensuring the physical directory matches a given criteria (e.g. it must exist, be readable, writable, etc)

Parameters:
pathToDir - path to the desired directory
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, 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.
Returns:
the validated directory represented by the given path.
Throws:
IllegalConfigurationException - if a validation fails

getDirectory

public final File getDirectory(String property,
                               boolean validateRead,
                               boolean validateWrite,
                               boolean create,
                               String... keyValuePairs)
                        throws IllegalConfigurationException
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. it must exist, be readable, writable, etc). An IllegalConfigurationException will be thrown if the property has no valid value associated.

Parameters:
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/"
Returns:
the validated directory represented by the given path.
Throws:
IllegalConfigurationException - if a validation fails

getDirectory

public final File getDirectory(String property,
                               boolean mandatory,
                               boolean validateRead,
                               boolean validateWrite,
                               boolean create,
                               String... keyValuePairs)
                        throws IllegalConfigurationException
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. it must exist, be readable, writable, etc).

Parameters:
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, otherwise
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/"
Returns:
the validated directory represented by the given path.
Throws:
IllegalConfigurationException - if a validation fails

getDirectory

public final File getDirectory(String property,
                               File defaultDir,
                               boolean validateRead,
                               boolean validateWrite,
                               String... keyValuePairs)
                        throws IllegalConfigurationException
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. it must exist, be readable, writable, etc).

Parameters:
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/"
Returns:
the validated directory represented by the given path, or the default directory if the property is empty.
Throws:
IllegalConfigurationException - if a validation fails

getFile

public final File getFile(String property,
                          boolean validateRead,
                          boolean validateWrite,
                          boolean create,
                          String... keyValuePairs)
                   throws IllegalConfigurationException
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. it must exist, be readable, writable, etc).

Parameters:
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"
Returns:
the validated file represented by the given path, or the default file if the property is empty.
Throws:
IllegalConfigurationException - if a validation fails

getFile

public final File getFile(String property,
                          boolean mandatory,
                          boolean validateRead,
                          boolean validateWrite,
                          boolean create,
                          String... keyValuePairs)
                   throws IllegalConfigurationException
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. it must exist, be readable, writable, etc).

Parameters:
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, otherwise
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"
Returns:
the validated file represented by the given path, or the default file if the property is empty.
Throws:
IllegalConfigurationException - if a validation fails

getFile

public final File getFile(String property,
                          File defaultFile,
                          boolean validateRead,
                          boolean validateWrite,
                          String... keyValuePairs)
                   throws IllegalConfigurationException
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. it must exist, be readable, writable, etc).

Parameters:
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"
Returns:
the validated file represented by the given path, or the default file if the property is empty.
Throws:
IllegalConfigurationException - if a validation fails

getInteger

public final Integer getInteger(String property)
Returns the Integer value associated with a property in the configuration

Parameters:
property - the property name
Returns:
the property value, or null if no value is provided.
Throws:
IllegalConfigurationException - if the property is not present in the configuration.

getInteger

public final Integer getInteger(String property,
                                Integer defaultValue)
Returns the Integer value associated with a property in the configuration

Parameters:
property - the property name
defaultValue - a default value to return in case the property is not present in the configuration
Returns:
the property value, or the default value if the property is not present in the configuration.

getList

public final List<String> getList(String property)
Returns a List of values associated with a property in the configuration. Assumes the values are separated by comma.

Parameters:
property - the property name
Returns:
the list of values associated with the given property value
Throws:
IllegalConfigurationException - if the property is not present in the configuration.

getList

public final List<String> getList(String property,
                                  String separator)
Returns a List of values associated with a property in the configuration

Parameters:
property - the property name
separator - the separator that delimits individual values associated with the property.
Returns:
the list of values associated with the given property value
Throws:
IllegalConfigurationException - if the property is not present in the configuration.

getBoolean

public final boolean getBoolean(String property,
                                boolean defaultValue)
Returns the boolean value associated with a property in the configuration

Parameters:
property - the property name
defaultValue - a default value to return in case the property is not present in the configuration
Returns:
the property value, or the default value if the property is not present in the configuration.

getBoolean

public final boolean getBoolean(String property)
Returns the boolean value associated with a property in the configuration

Parameters:
property - the property name
Returns:
the property value, or false if no value is provided.
Throws:
IllegalConfigurationException - if the property is not present in the configuration.


Copyright © 2018 uniVocity Software Pty Ltd. All rights reserved.