Class Configurations
- java.lang.Object
-
- org.apache.commons.configuration2.builder.fluent.Configurations
-
public class Configurations extends java.lang.Object
A convenience class which simplifies the creation of standard configurations and their builders.Complex initializations of configuration builders can be done in a pretty straight-forward way by making use of the provided fluent API. However, if only default settings are used (and maybe a configuration file to be loaded has to be specified), this approach tends to become a bit verbose. This class was introduced to simplify the creation of configuration objects in such cases. It offers a bunch of methods which allow the creation of some standard configuration classes with default settings passing in only a minimum required parameters.
An an example consider the creation of a
PropertiesConfiguration
object from a file. Using a builder, code like the following one would have to be written:Parameters params = new Parameters(); FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class) .configure(params.fileBased().setFile(new File("config.properties"))); PropertiesConfiguration config = builder.getConfiguration();
With a convenience method of
Configurations
the same can be achieved with the following:Configurations configurations = new Configurations(); PropertiesConfiguration config = configurations.properties(new File("config.properties"));
There are similar methods for constructing builder objects from which configurations can then be obtained.
This class is thread-safe. A single instance can be created by an application and used in a central way to create configuration objects. When an instance is created a
Parameters
instance can be passed in. Otherwise, a default instance is created. In any case, theParameters
instance associated with aConfigurations
object can be used to define default settings for the configurations to be created.- Since:
- 2.0
- See Also:
DefaultParametersManager
-
-
Field Summary
Fields Modifier and Type Field Description private Parameters
parameters
The parameters object associated with this instance.
-
Constructor Summary
Constructors Constructor Description Configurations()
Creates a newConfigurations
instance with default settings.Configurations(Parameters params)
Creates a new instance ofConfigurations
and initializes it with the specifiedParameters
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CombinedConfiguration
combined(java.io.File file)
Creates aCombinedConfiguration
instance from the content of the given file.CombinedConfiguration
combined(java.lang.String path)
Creates aCombinedConfiguration
instance from the content of the file identified by the given path.CombinedConfiguration
combined(java.net.URL url)
Creates aCombinedConfiguration
instance from the content of the given URL.CombinedConfigurationBuilder
combinedBuilder(java.io.File file)
Creates a builder for aCombinedConfiguration
and initializes it with the given file to be loaded.CombinedConfigurationBuilder
combinedBuilder(java.lang.String path)
Creates a builder for aCombinedConfiguration
and initializes it with the given path to the file to be loaded.CombinedConfigurationBuilder
combinedBuilder(java.net.URL url)
Creates a builder for aCombinedConfiguration
and initializes it with the given URL to be loaded.private <T extends FileBasedConfiguration>
FileBasedConfigurationBuilder<T>createFileBasedBuilder(java.lang.Class<T> configClass)
Creates a configured builder for a file-based configuration of the specified type.private <T extends FileBasedConfiguration>
FileBasedConfigurationBuilder<T>createFileBasedBuilder(java.lang.Class<T> configClass, FileBasedBuilderParameters params)
Creates a configured builder for a file-based configuration of the specified type.<T extends FileBasedConfiguration>
TfileBased(java.lang.Class<T> configClass, java.io.File file)
Creates an instance of the specified file-based configuration class from the content of the given file.<T extends FileBasedConfiguration>
TfileBased(java.lang.Class<T> configClass, java.lang.String path)
Creates an instance of the specified file-based configuration class from the content of the file identified by the given path.<T extends FileBasedConfiguration>
TfileBased(java.lang.Class<T> configClass, java.net.URL url)
Creates an instance of the specified file-based configuration class from the content of the given URL.<T extends FileBasedConfiguration>
FileBasedConfigurationBuilder<T>fileBasedBuilder(java.lang.Class<T> configClass, java.io.File file)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the file to be loaded.<T extends FileBasedConfiguration>
FileBasedConfigurationBuilder<T>fileBasedBuilder(java.lang.Class<T> configClass, java.lang.String path)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the path to the file to be loaded.<T extends FileBasedConfiguration>
FileBasedConfigurationBuilder<T>fileBasedBuilder(java.lang.Class<T> configClass, java.net.URL url)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the URL to the file to be loaded.private FileBasedBuilderParameters
fileParams()
Convenience method for creating a parameters object for a file-based configuration.private FileBasedBuilderParameters
fileParams(java.io.File file)
Convenience method for creating a file-based parameters object initialized with the given file.private FileBasedBuilderParameters
fileParams(java.lang.String path)
Convenience method for creating a file-based parameters object initialized with the given file path.private FileBasedBuilderParameters
fileParams(java.net.URL url)
Convenience method for creating a file-based parameters object initialized with the given file.Parameters
getParameters()
Gets theParameters
instance associated with this object.INIConfiguration
ini(java.io.File file)
Creates aINIConfiguration
instance from the content of the given file.INIConfiguration
ini(java.lang.String path)
Creates aINIConfiguration
instance from the content of the file identified by the given path.INIConfiguration
ini(java.net.URL url)
Creates aINIConfiguration
instance from the content of the given URL.FileBasedConfigurationBuilder<INIConfiguration>
iniBuilder(java.io.File file)
Creates a builder for aINIConfiguration
and initializes it with the given file to be loaded.FileBasedConfigurationBuilder<INIConfiguration>
iniBuilder(java.lang.String path)
Creates a builder for aINIConfiguration
and initializes it with the file file identified by the given path.FileBasedConfigurationBuilder<INIConfiguration>
iniBuilder(java.net.URL url)
Creates a builder for aINIConfiguration
and initializes it with the given URL to be loaded.PropertiesConfiguration
properties(java.io.File file)
Creates aPropertiesConfiguration
instance from the content of the given file.PropertiesConfiguration
properties(java.lang.String path)
Creates aPropertiesConfiguration
instance from the content of the file identified by the given path.PropertiesConfiguration
properties(java.net.URL url)
Creates aPropertiesConfiguration
instance from the content of the given URL.FileBasedConfigurationBuilder<PropertiesConfiguration>
propertiesBuilder()
Creates a builder for aPropertiesConfiguration
.FileBasedConfigurationBuilder<PropertiesConfiguration>
propertiesBuilder(java.io.File file)
Creates a builder for aPropertiesConfiguration
and initializes it with the given file to be loaded.FileBasedConfigurationBuilder<PropertiesConfiguration>
propertiesBuilder(java.lang.String path)
Creates a builder for aPropertiesConfiguration
and initializes it with the given path to the file to be loaded.FileBasedConfigurationBuilder<PropertiesConfiguration>
propertiesBuilder(java.net.URL url)
Creates a builder for aPropertiesConfiguration
and initializes it with the given URL to be loaded.FileBasedConfigurationBuilder<PropertiesConfiguration>
propertiesBuilder(PropertiesBuilderParameters parameters)
Creates a builder for aPropertiesConfiguration
and initializes it with the given parameters to be loaded.XMLConfiguration
xml(java.io.File file)
Creates aXMLConfiguration
instance from the content of the given file.XMLConfiguration
xml(java.lang.String path)
Creates aXMLConfiguration
instance from the content of the file identified by the given path.XMLConfiguration
xml(java.net.URL url)
Creates aXMLConfiguration
instance from the content of the given URL.FileBasedConfigurationBuilder<XMLConfiguration>
xmlBuilder(java.io.File file)
Creates a builder for aXMLConfiguration
and initializes it with the given file to be loaded.FileBasedConfigurationBuilder<XMLConfiguration>
xmlBuilder(java.lang.String path)
Creates a builder for aXMLConfiguration
and initializes it with the given path to the file to be loaded.FileBasedConfigurationBuilder<XMLConfiguration>
xmlBuilder(java.net.URL url)
Creates a builder for aXMLConfiguration
and initializes it with the given URL to be loaded.
-
-
-
Field Detail
-
parameters
private final Parameters parameters
The parameters object associated with this instance.
-
-
Constructor Detail
-
Configurations
public Configurations()
Creates a newConfigurations
instance with default settings.
-
Configurations
public Configurations(Parameters params)
Creates a new instance ofConfigurations
and initializes it with the specifiedParameters
object.- Parameters:
params
- theParameters
(may be null, then a default instance is created)
-
-
Method Detail
-
combined
public CombinedConfiguration combined(java.io.File file) throws ConfigurationException
Creates aCombinedConfiguration
instance from the content of the given file. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
file
- the file to be loaded- Returns:
- a
CombinedConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
combined
public CombinedConfiguration combined(java.lang.String path) throws ConfigurationException
Creates aCombinedConfiguration
instance from the content of the file identified by the given path. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
path
- the path to the file to be loaded- Returns:
- a
CombinedConfiguration
object initialized from this URL - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
combined
public CombinedConfiguration combined(java.net.URL url) throws ConfigurationException
Creates aCombinedConfiguration
instance from the content of the given URL. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
url
- the URL to be loaded- Returns:
- a
CombinedConfiguration
object initialized from this URL - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
combinedBuilder
public CombinedConfigurationBuilder combinedBuilder(java.io.File file)
Creates a builder for aCombinedConfiguration
and initializes it with the given file to be loaded.- Parameters:
file
- the file to be loaded- Returns:
- the newly created
CombinedConfigurationBuilder
-
combinedBuilder
public CombinedConfigurationBuilder combinedBuilder(java.lang.String path)
Creates a builder for aCombinedConfiguration
and initializes it with the given path to the file to be loaded.- Parameters:
path
- the path to the file to be loaded- Returns:
- the newly created
CombinedConfigurationBuilder
-
combinedBuilder
public CombinedConfigurationBuilder combinedBuilder(java.net.URL url)
Creates a builder for aCombinedConfiguration
and initializes it with the given URL to be loaded.- Parameters:
url
- the URL to be loaded- Returns:
- the newly created
CombinedConfigurationBuilder
-
createFileBasedBuilder
private <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T> createFileBasedBuilder(java.lang.Class<T> configClass)
Creates a configured builder for a file-based configuration of the specified type.- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration class- Returns:
- the newly created builder
- Since:
- 2.6
-
createFileBasedBuilder
private <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T> createFileBasedBuilder(java.lang.Class<T> configClass, FileBasedBuilderParameters params)
Creates a configured builder for a file-based configuration of the specified type.- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classparams
- the parameters object for configuring the builder- Returns:
- the newly created builder
-
fileBased
public <T extends FileBasedConfiguration> T fileBased(java.lang.Class<T> configClass, java.io.File file) throws ConfigurationException
Creates an instance of the specified file-based configuration class from the content of the given file. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classfile
- the file to be loaded- Returns:
- a
FileBasedConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
fileBased
public <T extends FileBasedConfiguration> T fileBased(java.lang.Class<T> configClass, java.lang.String path) throws ConfigurationException
Creates an instance of the specified file-based configuration class from the content of the file identified by the given path. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classpath
- the path to the file to be loaded- Returns:
- a
FileBasedConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
fileBased
public <T extends FileBasedConfiguration> T fileBased(java.lang.Class<T> configClass, java.net.URL url) throws ConfigurationException
Creates an instance of the specified file-based configuration class from the content of the given URL. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classurl
- the URL to be loaded- Returns:
- a
FileBasedConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
fileBasedBuilder
public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T> fileBasedBuilder(java.lang.Class<T> configClass, java.io.File file)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the file to be loaded.- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classfile
- the file to be loaded- Returns:
- the new
FileBasedConfigurationBuilder
-
fileBasedBuilder
public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T> fileBasedBuilder(java.lang.Class<T> configClass, java.lang.String path)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the path to the file to be loaded.- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classpath
- the path to the file to be loaded- Returns:
- the new
FileBasedConfigurationBuilder
-
fileBasedBuilder
public <T extends FileBasedConfiguration> FileBasedConfigurationBuilder<T> fileBasedBuilder(java.lang.Class<T> configClass, java.net.URL url)
Creates aFileBasedConfigurationBuilder
for the specified configuration class and initializes it with the URL to the file to be loaded.- Type Parameters:
T
- the type of the configuration to be constructed- Parameters:
configClass
- the configuration classurl
- the URL to be loaded- Returns:
- the new
FileBasedConfigurationBuilder
-
fileParams
private FileBasedBuilderParameters fileParams()
Convenience method for creating a parameters object for a file-based configuration.- Returns:
- the newly created parameters object
-
fileParams
private FileBasedBuilderParameters fileParams(java.io.File file)
Convenience method for creating a file-based parameters object initialized with the given file.- Parameters:
file
- the file to be loaded- Returns:
- the initialized parameters object
-
fileParams
private FileBasedBuilderParameters fileParams(java.lang.String path)
Convenience method for creating a file-based parameters object initialized with the given file path.- Parameters:
path
- the path to the file to be loaded- Returns:
- the initialized parameters object
-
fileParams
private FileBasedBuilderParameters fileParams(java.net.URL url)
Convenience method for creating a file-based parameters object initialized with the given file.- Parameters:
url
- the URL to be loaded- Returns:
- the initialized parameters object
-
getParameters
public Parameters getParameters()
Gets theParameters
instance associated with this object.- Returns:
- the associated
Parameters
object
-
ini
public INIConfiguration ini(java.io.File file) throws ConfigurationException
Creates aINIConfiguration
instance from the content of the given file. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
file
- the file to be loaded- Returns:
- a
INIConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
ini
public INIConfiguration ini(java.lang.String path) throws ConfigurationException
Creates aINIConfiguration
instance from the content of the file identified by the given path. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
path
- the path to the file to be loaded- Returns:
- a
INIConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
ini
public INIConfiguration ini(java.net.URL url) throws ConfigurationException
Creates aINIConfiguration
instance from the content of the given URL. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
url
- the URL to be loaded- Returns:
- a
INIConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
iniBuilder
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(java.io.File file)
Creates a builder for aINIConfiguration
and initializes it with the given file to be loaded.- Parameters:
file
- the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
iniBuilder
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(java.lang.String path)
Creates a builder for aINIConfiguration
and initializes it with the file file identified by the given path.- Parameters:
path
- the path to the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
iniBuilder
public FileBasedConfigurationBuilder<INIConfiguration> iniBuilder(java.net.URL url)
Creates a builder for aINIConfiguration
and initializes it with the given URL to be loaded.- Parameters:
url
- the URL to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
properties
public PropertiesConfiguration properties(java.io.File file) throws ConfigurationException
Creates aPropertiesConfiguration
instance from the content of the given file. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
file
- the file to be loaded- Returns:
- a
PropertiesConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
properties
public PropertiesConfiguration properties(java.lang.String path) throws ConfigurationException
Creates aPropertiesConfiguration
instance from the content of the file identified by the given path. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
path
- the path to the file to be loaded- Returns:
- a
PropertiesConfiguration
object initialized from this path - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
properties
public PropertiesConfiguration properties(java.net.URL url) throws ConfigurationException
Creates aPropertiesConfiguration
instance from the content of the given URL. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
url
- the URL to be loaded- Returns:
- a
PropertiesConfiguration
object initialized from this URL - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
propertiesBuilder
public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder()
Creates a builder for aPropertiesConfiguration
.- Returns:
- the newly created
FileBasedConfigurationBuilder
- Since:
- 2.6
-
propertiesBuilder
public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(java.io.File file)
Creates a builder for aPropertiesConfiguration
and initializes it with the given file to be loaded.- Parameters:
file
- the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
propertiesBuilder
public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(PropertiesBuilderParameters parameters)
Creates a builder for aPropertiesConfiguration
and initializes it with the given parameters to be loaded.- Parameters:
parameters
- the parameters to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
- Since:
- 2.6
-
propertiesBuilder
public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(java.lang.String path)
Creates a builder for aPropertiesConfiguration
and initializes it with the given path to the file to be loaded.- Parameters:
path
- the path to the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
propertiesBuilder
public FileBasedConfigurationBuilder<PropertiesConfiguration> propertiesBuilder(java.net.URL url)
Creates a builder for aPropertiesConfiguration
and initializes it with the given URL to be loaded.- Parameters:
url
- the URL to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
xml
public XMLConfiguration xml(java.io.File file) throws ConfigurationException
Creates aXMLConfiguration
instance from the content of the given file. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
file
- the file to be loaded- Returns:
- a
XMLConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
xml
public XMLConfiguration xml(java.lang.String path) throws ConfigurationException
Creates aXMLConfiguration
instance from the content of the file identified by the given path. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
path
- the path to the file to be loaded- Returns:
- a
XMLConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
xml
public XMLConfiguration xml(java.net.URL url) throws ConfigurationException
Creates aXMLConfiguration
instance from the content of the given URL. This is a convenience method which can be used if no builder is needed for managing the configuration object. (Although, behind the scenes a builder is created).- Parameters:
url
- the URL to be loaded- Returns:
- a
XMLConfiguration
object initialized from this file - Throws:
ConfigurationException
- if an error occurred when loading the configuration
-
xmlBuilder
public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(java.io.File file)
Creates a builder for aXMLConfiguration
and initializes it with the given file to be loaded.- Parameters:
file
- the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
xmlBuilder
public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(java.lang.String path)
Creates a builder for aXMLConfiguration
and initializes it with the given path to the file to be loaded.- Parameters:
path
- the path to the file to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
xmlBuilder
public FileBasedConfigurationBuilder<XMLConfiguration> xmlBuilder(java.net.URL url)
Creates a builder for aXMLConfiguration
and initializes it with the given URL to be loaded.- Parameters:
url
- the URL to be loaded- Returns:
- the newly created
FileBasedConfigurationBuilder
-
-