Class BaseConfigurationBuilderProvider
- java.lang.Object
-
- org.apache.commons.configuration2.builder.combined.BaseConfigurationBuilderProvider
-
- All Implemented Interfaces:
ConfigurationBuilderProvider
- Direct Known Subclasses:
CombinedConfigurationBuilderProvider
,FileExtensionConfigurationBuilderProvider
,MultiFileConfigurationBuilderProvider
public class BaseConfigurationBuilderProvider extends java.lang.Object implements ConfigurationBuilderProvider
A fully-functional, reflection-based implementation of the
ConfigurationBuilderProvider
interface which can deal with the default tags defining configuration sources.An instance of this class is initialized with the names of the
ConfigurationBuilder
class used by this provider and the concreteConfiguration
class. TheConfigurationBuilder
class must be derived fromBasicConfigurationBuilder
. When asked for the builder object, an instance of the builder class is created and initialized from the bean declaration associated with the current configuration source.ConfigurationBuilder
objects are configured using parameter objects. When declaring configuration sources in XML it should not be necessary to define the single parameter objects. Rather, simple and complex properties are set in the typical way of a bean declaration (i.e. as attributes of the current XML element or as child elements). This class creates all supported parameter objects (whose names also must be provided at construction time) and takes care that their properties are initialized according to the current bean declaration.The use of reflection to create builder instances allows a generic implementation supporting many concrete builder classes. Another reason for this approach is that builder classes are only loaded if actually needed. Some specialized
Configuration
implementations require specific external dependencies which should not be mandatory for the use ofCombinedConfigurationBuilder
. Because such classes are lazily loaded, an application only has to include the dependencies it actually uses.- Since:
- 2.0
-
-
Constructor Summary
Constructors Constructor Description BaseConfigurationBuilderProvider(java.lang.String bldrCls, java.lang.String reloadBldrCls, java.lang.String configCls, java.util.Collection<java.lang.String> paramCls)
Creates a new instance ofBaseConfigurationBuilderProvider
and initializes all its properties.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
configureBuilder(BasicConfigurationBuilder<? extends Configuration> builder, ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Configures a newly created builder instance with its initialization parameters.protected BasicConfigurationBuilder<? extends Configuration>
createBuilder(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Creates a new, uninitialized instance of the builder class managed by this provider.protected java.util.Collection<BuilderParameters>
createParameterObjects()
Creates a collection of parameter objects to be used for configuring the builder.protected java.lang.String
determineBuilderClass(ConfigurationDeclaration decl)
Determines the name of the class to be used for a new builder instance.protected java.lang.String
determineConfigurationClass(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Determines the name of the configuration class produced by the builder.java.lang.String
getBuilderClass()
Gets the name of the class of the builder created by this provider.ConfigurationBuilder<? extends Configuration>
getConfigurationBuilder(ConfigurationDeclaration decl)
Gets the builder for the configuration source managed by this provider.java.lang.String
getConfigurationClass()
Gets the name of the configuration class created by the builder produced by this provider.java.util.Collection<java.lang.String>
getParameterClasses()
Gets an unmodifiable collection with the names of parameter classes supported by this provider.java.lang.String
getReloadingBuilderClass()
Gets the name of the class of the builder created by this provider if the reload flag is set.protected void
inheritParentBuilderProperties(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Passes all parameter objects to the parentCombinedConfigurationBuilder
so that properties already defined for the parent builder can be added.protected void
initializeParameterObjects(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Initializes the parameter objects with data stored in the current bean declaration.protected boolean
isAllowFailOnInit(ConfigurationDeclaration decl)
Determines the allowFailOnInit flag for the newly created builder based on the givenConfigurationDeclaration
.
-
-
-
Constructor Detail
-
BaseConfigurationBuilderProvider
public BaseConfigurationBuilderProvider(java.lang.String bldrCls, java.lang.String reloadBldrCls, java.lang.String configCls, java.util.Collection<java.lang.String> paramCls)
Creates a new instance ofBaseConfigurationBuilderProvider
and initializes all its properties.- Parameters:
bldrCls
- the name of the builder class (must not be null)reloadBldrCls
- the name of a builder class to be used if reloading support is required (null if reloading is not supported)configCls
- the name of the configuration class (must not be null)paramCls
- a collection with the names of parameters classes- Throws:
java.lang.IllegalArgumentException
- if a required parameter is missing
-
-
Method Detail
-
getBuilderClass
public java.lang.String getBuilderClass()
Gets the name of the class of the builder created by this provider.- Returns:
- the builder class
-
getReloadingBuilderClass
public java.lang.String getReloadingBuilderClass()
Gets the name of the class of the builder created by this provider if the reload flag is set. If this method returns null, reloading builders are not supported by this provider.- Returns:
- the reloading builder class
-
getConfigurationClass
public java.lang.String getConfigurationClass()
Gets the name of the configuration class created by the builder produced by this provider.- Returns:
- the configuration class
-
getParameterClasses
public java.util.Collection<java.lang.String> getParameterClasses()
Gets an unmodifiable collection with the names of parameter classes supported by this provider.- Returns:
- the parameter classes
-
getConfigurationBuilder
public ConfigurationBuilder<? extends Configuration> getConfigurationBuilder(ConfigurationDeclaration decl) throws ConfigurationException
Gets the builder for the configuration source managed by this provider. This method is called during processing of the combined configuration definition file. This implementation delegates to some protected methods to create a new builder instance using reflection and to configure it with parameter values defined by the passed inBeanDeclaration
.- Specified by:
getConfigurationBuilder
in interfaceConfigurationBuilderProvider
- Parameters:
decl
- the bean declaration with initialization parameters for the configuration builder- Returns:
- the
ConfigurationBuilder
object created by this provider - Throws:
ConfigurationException
- if an error occurs
-
isAllowFailOnInit
protected boolean isAllowFailOnInit(ConfigurationDeclaration decl)
Determines the allowFailOnInit flag for the newly created builder based on the givenConfigurationDeclaration
. Some combinations of flags in the declaration say that a configuration source is optional, but an empty instance should be created if its creation fail.- Parameters:
decl
- the currentConfigurationDeclaration
- Returns:
- the value of the allowFailOnInit flag
-
createParameterObjects
protected java.util.Collection<BuilderParameters> createParameterObjects() throws java.lang.Exception
Creates a collection of parameter objects to be used for configuring the builder. This method creates instances of the parameter classes passed to the constructor.- Returns:
- a collection with parameter objects for the builder
- Throws:
java.lang.Exception
- if an error occurs while creating parameter objects via reflection
-
initializeParameterObjects
protected void initializeParameterObjects(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params) throws java.lang.Exception
Initializes the parameter objects with data stored in the current bean declaration. This method is called before the newly created builder instance is configured with the parameter objects. It maps attributes of the bean declaration to properties of parameter objects. In addition, it invokes the parentCombinedConfigurationBuilder
so that the parameters object can inherit properties already defined for this builder.- Parameters:
decl
- the currentConfigurationDeclaration
params
- the collection with (uninitialized) parameter objects- Throws:
java.lang.Exception
- if an error occurs
-
inheritParentBuilderProperties
protected void inheritParentBuilderProperties(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params)
Passes all parameter objects to the parentCombinedConfigurationBuilder
so that properties already defined for the parent builder can be added. This method is called before the parameter objects are initialized from the definition configuration. This way properties from the parent builder are inherited, but can be overridden for child configurations.- Parameters:
decl
- the currentConfigurationDeclaration
params
- the collection with (uninitialized) parameter objects
-
createBuilder
protected BasicConfigurationBuilder<? extends Configuration> createBuilder(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params) throws java.lang.Exception
Creates a new, uninitialized instance of the builder class managed by this provider. This implementation determines the builder class to be used by delegating todetermineBuilderClass()
. It then calls the constructor expecting the configuration class, the map with properties, and theallowFailOnInit flag.- Parameters:
decl
- the currentConfigurationDeclaration
params
- initialization parameters for the new builder object- Returns:
- the newly created builder instance
- Throws:
java.lang.Exception
- if an error occurs
-
configureBuilder
protected void configureBuilder(BasicConfigurationBuilder<? extends Configuration> builder, ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params) throws java.lang.Exception
Configures a newly created builder instance with its initialization parameters. This method is called after a new instance was created using reflection. This implementation passes the parameter objects to the builder'sconfigure()
method.- Parameters:
builder
- the builder to be initializeddecl
- the currentConfigurationDeclaration
params
- the collection with initialization parameter objects- Throws:
java.lang.Exception
- if an error occurs
-
determineBuilderClass
protected java.lang.String determineBuilderClass(ConfigurationDeclaration decl) throws ConfigurationException
Determines the name of the class to be used for a new builder instance. This implementation selects between the normal and the reloading builder class, based on the passed inConfigurationDeclaration
. If a reloading builder is desired, but this provider has no reloading support, an exception is thrown.- Parameters:
decl
- the currentConfigurationDeclaration
- Returns:
- the name of the builder class
- Throws:
ConfigurationException
- if the builder class cannot be determined
-
determineConfigurationClass
protected java.lang.String determineConfigurationClass(ConfigurationDeclaration decl, java.util.Collection<BuilderParameters> params) throws ConfigurationException
Determines the name of the configuration class produced by the builder. This method is called when obtaining the arguments for invoking the constructor of the builder class. This implementation just returns the pre-configured configuration class name. Derived classes may determine this class name dynamically based on the passed in parameters.- Parameters:
decl
- the currentConfigurationDeclaration
params
- the collection with parameter objects- Returns:
- the name of the builder's result configuration class
- Throws:
ConfigurationException
- if an error occurs
-
-