Class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration>
- java.lang.Object
-
- org.apache.commons.configuration2.builder.BasicConfigurationBuilder<T>
-
- org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder<T>
-
- org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder<T>
-
- Type Parameters:
T
- the concrete type ofConfiguration
objects created by this builder
- All Implemented Interfaces:
ConfigurationBuilder<T>
,EventSource
,ReloadingControllerSupport
public class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration> extends FileBasedConfigurationBuilder<T> implements ReloadingControllerSupport
A specialized
ConfigurationBuilder
implementation which can handle configurations read from aFileHandler
and supports reloading.This builder class exposes a
ReloadingController
object controlling reload operations on the file-based configuration produced as result object. For theFileHandler
defining the location of the configuration a configurableReloadingDetector
is created and associated with the controller. So changes on the source file can be detected. When ever such a change occurs, the result object of this builder is reset. This means that the next timegetConfiguration()
is called a newConfiguration
object is created which is loaded from the modified file.Client code interested in notifications can register a listener at this builder to receive reset events. When such an event is received the new result object can be requested. This way client applications can be sure to work with an up-to-date configuration. It is also possible to register a listener directly at the
ReloadingController
.This builder does not actively trigger the
ReloadingController
to perform a reload check. This has to be done by an external component, e.g. a timer.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private static ReloadingDetectorFactory
DEFAULT_DETECTOR_FACTORY
The default factory for creating reloading detector objects.private ReloadingController
reloadingController
The reloading controller associated with this object.private ReloadingDetector
resultReloadingDetector
The reloading detector which does the actual reload check for the current result object.
-
Constructor Summary
Constructors Constructor Description ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class.ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls, java.util.Map<java.lang.String,java.lang.Object> params)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class and sets initialization parameters.ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls, java.util.Map<java.lang.String,java.lang.Object> params, boolean allowFailOnInit)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class and sets initialization parameters and the allowFailOnInit flag.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ReloadingFileBasedConfigurationBuilder<T>
configure(BuilderParameters... params)
Appends the content of the specifiedBuilderParameters
objects to the current initialization parameters.private ReloadingController
createReloadingController()
Creates theReloadingController
associated with this object.protected ReloadingDetector
createReloadingDetector(FileHandler handler, FileBasedBuilderParametersImpl fbparams)
Creates aReloadingDetector
which monitors the passed inFileHandler
.private ReloadingDetector
createReloadingDetectorForController()
Creates aReloadingDetector
wrapper to be passed to the associatedReloadingController
.private static ReloadingDetectorFactory
fetchDetectorFactory(FileBasedBuilderParametersImpl params)
Returns aReloadingDetectorFactory
either from the passed in parameters or a default factory.ReloadingController
getReloadingController()
Gets theReloadingController
associated with this builder.protected void
initFileHandler(FileHandler handler)
Initializes the new currentFileHandler
.-
Methods inherited from class org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder
getDefaultEncoding, getFileHandler, initResultInstance, isAutoSave, save, setAutoSave, setDefaultEncoding, setParameters
-
Methods inherited from class org.apache.commons.configuration2.builder.BasicConfigurationBuilder
addEventListener, addParameters, connectToReloadingController, copyEventListeners, copyEventListeners, createResult, createResultDeclaration, createResultInstance, fetchBeanHelper, fireBuilderEvent, getConfiguration, getParameters, getResultClass, getResultDeclaration, installEventListener, isAllowFailOnInit, removeEventListener, reset, resetParameters, resetResult
-
-
-
-
Field Detail
-
DEFAULT_DETECTOR_FACTORY
private static final ReloadingDetectorFactory DEFAULT_DETECTOR_FACTORY
The default factory for creating reloading detector objects.
-
reloadingController
private final ReloadingController reloadingController
The reloading controller associated with this object.
-
resultReloadingDetector
private volatile ReloadingDetector resultReloadingDetector
The reloading detector which does the actual reload check for the current result object. A new instance is created whenever a new result object (and thus a new current file handler) becomes available. The field must be volatile because it is accessed by the reloading controller probably from within another thread.
-
-
Constructor Detail
-
ReloadingFileBasedConfigurationBuilder
public ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class.- Parameters:
resCls
- the result class (must not be null- Throws:
java.lang.IllegalArgumentException
- if the result class is null
-
ReloadingFileBasedConfigurationBuilder
public ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls, java.util.Map<java.lang.String,java.lang.Object> params)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class and sets initialization parameters.- Parameters:
resCls
- the result class (must not be nullparams
- a map with initialization parameters- Throws:
java.lang.IllegalArgumentException
- if the result class is null
-
ReloadingFileBasedConfigurationBuilder
public ReloadingFileBasedConfigurationBuilder(java.lang.Class<? extends T> resCls, java.util.Map<java.lang.String,java.lang.Object> params, boolean allowFailOnInit)
Creates a new instance ofReloadingFileBasedConfigurationBuilder
which produces result objects of the specified class and sets initialization parameters and the allowFailOnInit flag.- Parameters:
resCls
- the result class (must not be nullparams
- a map with initialization parametersallowFailOnInit
- the allowFailOnInit flag- Throws:
java.lang.IllegalArgumentException
- if the result class is null
-
-
Method Detail
-
fetchDetectorFactory
private static ReloadingDetectorFactory fetchDetectorFactory(FileBasedBuilderParametersImpl params)
Returns aReloadingDetectorFactory
either from the passed in parameters or a default factory.- Parameters:
params
- the current parameters object- Returns:
- the
ReloadingDetectorFactory
to be used
-
configure
public ReloadingFileBasedConfigurationBuilder<T> configure(BuilderParameters... params)
Appends the content of the specifiedBuilderParameters
objects to the current initialization parameters. Calling this method multiple times will create a union of the parameters provided. This method is overridden here to change the result type. This method is overridden here to change the result type.- Overrides:
configure
in classFileBasedConfigurationBuilder<T extends FileBasedConfiguration>
- Parameters:
params
- an arbitrary number of objects with builder parameters- Returns:
- a reference to this builder for method chaining
-
createReloadingController
private ReloadingController createReloadingController()
Creates theReloadingController
associated with this object. The controller is assigned a specialized reloading detector which delegates to the detector for the current result object. (FileHandlerReloadingDetector
does not support changing the file handler, andReloadingController
does not support changing the reloading detector; therefore, this level of indirection is needed to change the monitored file dynamically.)- Returns:
- the new
ReloadingController
-
createReloadingDetector
protected ReloadingDetector createReloadingDetector(FileHandler handler, FileBasedBuilderParametersImpl fbparams) throws ConfigurationException
Creates aReloadingDetector
which monitors the passed inFileHandler
. This method is called each time a new result object is created with the currentFileHandler
. This implementation checks whether aReloadingDetectorFactory
is specified in the current parameters. If this is the case, it is invoked. Otherwise, a default factory is used to create aFileHandlerReloadingDetector
object. Note: This method is called from a synchronized block.- Parameters:
handler
- the currentFileHandler
fbparams
- the object with parameters related to file-based builders- Returns:
- a
ReloadingDetector
for thisFileHandler
- Throws:
ConfigurationException
- if an error occurs
-
createReloadingDetectorForController
private ReloadingDetector createReloadingDetectorForController()
Creates aReloadingDetector
wrapper to be passed to the associatedReloadingController
. This detector wrapper simply delegates to the currentReloadingDetector
if it is available.- Returns:
- the wrapper
ReloadingDetector
-
getReloadingController
public ReloadingController getReloadingController()
Gets theReloadingController
associated with this builder. This controller is directly created. However, it becomes active (i.e. associated with a meaningful reloading detector) not before a result object was created.- Specified by:
getReloadingController
in interfaceReloadingControllerSupport
- Returns:
- the
ReloadingController
-
initFileHandler
protected void initFileHandler(FileHandler handler) throws ConfigurationException
Initializes the new currentFileHandler
. When a new result object is created, a newFileHandler
is created, too, and associated with the result object. This new handler is passed to this method. If a location is defined, the result object is loaded from this location. Note: This method is called from a synchronized block. This implementation also takes care that a newReloadingDetector
for the new currentFileHandler
is created. Also, the reloading controller's reloading state has to be reset; after the creation of a new result object changes in the underlying configuration source have to be monitored again.- Overrides:
initFileHandler
in classFileBasedConfigurationBuilder<T extends FileBasedConfiguration>
- Parameters:
handler
- the new currentFileHandler
- Throws:
ConfigurationException
- if an error occurs
-
-