Class ReloadingFileBasedConfigurationBuilder<T extends FileBasedConfiguration>
- Type Parameters:
T- the concrete type ofConfigurationobjects created by this builder
- All Implemented Interfaces:
ConfigurationBuilder<T>,EventSource,ReloadingControllerSupport
A specialized ConfigurationBuilder implementation which can handle configurations read from a
FileHandler and supports reloading.
This builder class exposes a ReloadingController object controlling reload operations on the file-based
configuration produced as result object. For the FileHandler defining the location of the configuration a
configurable ReloadingDetector 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
time getConfiguration() is called a new Configuration 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
FieldsModifier and TypeFieldDescriptionprivate static final ReloadingDetectorFactoryThe default factory for creating reloading detector objects.private final ReloadingControllerThe reloading controller associated with this object.private ReloadingDetectorThe reloading detector which does the actual reload check for the current result object. -
Constructor Summary
ConstructorsConstructorDescriptionReloadingFileBasedConfigurationBuilder(Class<? extends T> resCls) Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich produces result objects of the specified class.Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich produces result objects of the specified class and sets initialization parameters.ReloadingFileBasedConfigurationBuilder(Class<? extends T> resCls, Map<String, Object> params, boolean allowFailOnInit) Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich produces result objects of the specified class and sets initialization parameters and the allowFailOnInit flag. -
Method Summary
Modifier and TypeMethodDescriptionconfigure(BuilderParameters... params) Appends the content of the specifiedBuilderParametersobjects to the current initialization parameters.private ReloadingControllerCreates theReloadingControllerassociated with this object.protected ReloadingDetectorcreateReloadingDetector(FileHandler handler, FileBasedBuilderParametersImpl fbparams) Creates aReloadingDetectorwhich monitors the passed inFileHandler.private ReloadingDetectorCreates aReloadingDetectorwrapper to be passed to the associatedReloadingController.private static ReloadingDetectorFactoryReturns aReloadingDetectorFactoryeither from the passed in parameters or a default factory.Gets theReloadingControllerassociated with this builder.protected voidinitFileHandler(FileHandler handler) Initializes the new currentFileHandler.Methods inherited from class org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder
getDefaultEncoding, getFileHandler, initResultInstance, isAutoSave, save, setAutoSave, setDefaultEncoding, setParametersMethods 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 Details
-
DEFAULT_DETECTOR_FACTORY
The default factory for creating reloading detector objects. -
reloadingController
The reloading controller associated with this object. -
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 Details
-
ReloadingFileBasedConfigurationBuilder
Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich produces result objects of the specified class.- Parameters:
resCls- the result class (must not be null- Throws:
IllegalArgumentException- if the result class is null
-
ReloadingFileBasedConfigurationBuilder
Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich 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:
IllegalArgumentException- if the result class is null
-
ReloadingFileBasedConfigurationBuilder
public ReloadingFileBasedConfigurationBuilder(Class<? extends T> resCls, Map<String, Object> params, boolean allowFailOnInit) Creates a new instance ofReloadingFileBasedConfigurationBuilderwhich 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:
IllegalArgumentException- if the result class is null
-
-
Method Details
-
fetchDetectorFactory
Returns aReloadingDetectorFactoryeither from the passed in parameters or a default factory.- Parameters:
params- the current parameters object- Returns:
- the
ReloadingDetectorFactoryto be used
-
configure
Appends the content of the specifiedBuilderParametersobjects 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:
configurein classFileBasedConfigurationBuilder<T extends FileBasedConfiguration>- Parameters:
params- an arbitrary number of objects with builder parameters- Returns:
- a reference to this builder for method chaining
-
createReloadingController
Creates theReloadingControllerassociated with this object. The controller is assigned a specialized reloading detector which delegates to the detector for the current result object. (FileHandlerReloadingDetectordoes not support changing the file handler, andReloadingControllerdoes 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 aReloadingDetectorwhich monitors the passed inFileHandler. This method is called each time a new result object is created with the currentFileHandler. This implementation checks whether aReloadingDetectorFactoryis specified in the current parameters. If this is the case, it is invoked. Otherwise, a default factory is used to create aFileHandlerReloadingDetectorobject. Note: This method is called from a synchronized block.- Parameters:
handler- the currentFileHandlerfbparams- the object with parameters related to file-based builders- Returns:
- a
ReloadingDetectorfor thisFileHandler - Throws:
ConfigurationException- if an error occurs
-
createReloadingDetectorForController
Creates aReloadingDetectorwrapper to be passed to the associatedReloadingController. This detector wrapper simply delegates to the currentReloadingDetectorif it is available.- Returns:
- the wrapper
ReloadingDetector
-
getReloadingController
Gets theReloadingControllerassociated 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:
getReloadingControllerin interfaceReloadingControllerSupport- Returns:
- the
ReloadingController
-
initFileHandler
Initializes the new currentFileHandler. When a new result object is created, a newFileHandleris 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 newReloadingDetectorfor the new currentFileHandleris 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:
initFileHandlerin classFileBasedConfigurationBuilder<T extends FileBasedConfiguration>- Parameters:
handler- the new currentFileHandler- Throws:
ConfigurationException- if an error occurs
-