T
- the concrete type of Configuration
objects created by this builderpublic class MultiFileConfigurationBuilder<T extends FileBasedConfiguration> extends BasicConfigurationBuilder<T>
A specialized ConfigurationBuilder
implementation providing access to multiple file-based configurations
based on a file name pattern.
This builder class is initialized with a pattern string and a ConfigurationInterpolator
object. Each time a
configuration is requested, the pattern is evaluated against the ConfigurationInterpolator
(so all variables
are replaced by their current values). The resulting string is interpreted as a file name for a configuration file to
be loaded. For example, providing a pattern of file:///opt/config/${product}/${client}/config.xml will
result in product and client being resolved on every call. By storing configuration files in a
corresponding directory structure, specialized configuration files associated with a specific product and client can
be loaded. Thus an application can be made multi-tenant in a transparent way.
This builder class keeps a map with configuration builders for configurations already loaded. The
getConfiguration()
method first evaluates the pattern string and checks whether a builder for the resulting
file name is available. If yes, it is queried for its configuration. Otherwise, a new file-based configuration
builder is created now and initialized.
Configuration of an instance happens in the usual way for configuration builders. A
MultiFileBuilderParametersImpl
parameters object is expected which must contain a file name pattern string
and a ConfigurationInterpolator
. Other properties of this parameters object are used to initialize the
builders for managed configurations.
Constructor and Description |
---|
MultiFileConfigurationBuilder(Class<? extends T> resCls)
Creates a new instance of
MultiFileConfigurationBuilder without setting initialization parameters. |
MultiFileConfigurationBuilder(Class<? extends T> resCls,
Map<String,Object> params)
Creates a new instance of
MultiFileConfigurationBuilder and sets initialization parameters. |
MultiFileConfigurationBuilder(Class<? extends T> resCls,
Map<String,Object> params,
boolean allowFailOnInit)
Creates a new instance of
MultiFileConfigurationBuilder and sets initialization parameters and a flag whether
initialization failures should be ignored. |
Modifier and Type | Method and Description |
---|---|
<E extends Event> |
addEventListener(EventType<E> eventType,
EventListener<? super E> l)
Adds an event listener for the specified event type.
|
MultiFileConfigurationBuilder<T> |
configure(BuilderParameters... params)
Appends the content of the specified
BuilderParameters objects to the current initialization parameters. |
protected String |
constructFileName(MultiFileBuilderParametersImpl multiParams)
Determines the file name of a configuration based on the file name pattern.
|
protected FileBasedConfigurationBuilder<T> |
createInitializedManagedBuilder(String fileName,
Map<String,Object> params)
Creates a fully initialized builder for a managed configuration.
|
protected ConfigurationInterpolator |
createInterpolator()
Creates the
ConfigurationInterpolator to be used by this instance. |
protected FileBasedConfigurationBuilder<T> |
createManagedBuilder(String fileName,
Map<String,Object> params)
Creates a builder for a managed configuration.
|
T |
getConfiguration()
Gets the configuration provided by this builder.
|
protected ConfigurationInterpolator |
getInterpolator()
Returns the
ConfigurationInterpolator used by this instance. |
FileBasedConfigurationBuilder<T> |
getManagedBuilder()
Returns the managed
FileBasedConfigurationBuilder for the current file name pattern. |
protected ConcurrentMap<String,FileBasedConfigurationBuilder<T>> |
getManagedBuilders()
Returns the map with the managed builders created so far by this
MultiFileConfigurationBuilder . |
<E extends Event> |
removeEventListener(EventType<E> eventType,
EventListener<? super E> l)
Removes the event listener registration for the given event type and listener.
|
void |
resetParameters()
Removes all initialization parameters of this builder.
|
addParameters, connectToReloadingController, copyEventListeners, copyEventListeners, createResult, createResultDeclaration, createResultInstance, fetchBeanHelper, fireBuilderEvent, getParameters, getResultClass, getResultDeclaration, initResultInstance, installEventListener, isAllowFailOnInit, reset, resetResult, setParameters
public MultiFileConfigurationBuilder(Class<? extends T> resCls, Map<String,Object> params, boolean allowFailOnInit)
MultiFileConfigurationBuilder
and sets initialization parameters and a flag whether
initialization failures should be ignored.resCls
- the result configuration classparams
- a map with initialization parametersallowFailOnInit
- a flag whether initialization errors should be ignoredIllegalArgumentException
- if the result class is nullpublic MultiFileConfigurationBuilder(Class<? extends T> resCls, Map<String,Object> params)
MultiFileConfigurationBuilder
and sets initialization parameters.resCls
- the result configuration classparams
- a map with initialization parametersIllegalArgumentException
- if the result class is nullpublic MultiFileConfigurationBuilder(Class<? extends T> resCls)
MultiFileConfigurationBuilder
without setting initialization parameters.resCls
- the result configuration classIllegalArgumentException
- if the result class is nullpublic MultiFileConfigurationBuilder<T> configure(BuilderParameters... params)
BuilderParameters
objects to the current initialization parameters.
Calling this method multiple times will create a union of the parameters provided. This method is overridden to adapt the return type.configure
in class BasicConfigurationBuilder<T extends FileBasedConfiguration>
params
- an arbitrary number of objects with builder parameterspublic T getConfiguration() throws ConfigurationException
ImmutableConfiguration
object. This implementation creates the result configuration on first access. Later invocations return the same
object until this builder is reset. The double-check idiom for lazy initialization is used (Bloch, Effective Java,
item 71). This implementation evaluates the file name pattern using the configured
ConfigurationInterpolator
. If this file has already been loaded, the corresponding builder is accessed.
Otherwise, a new builder is created for loading this configuration file.getConfiguration
in interface ConfigurationBuilder<T extends FileBasedConfiguration>
getConfiguration
in class BasicConfigurationBuilder<T extends FileBasedConfiguration>
ConfigurationException
- if an error occurspublic FileBasedConfigurationBuilder<T> getManagedBuilder() throws ConfigurationException
FileBasedConfigurationBuilder
for the current file name pattern. It is determined based
on the evaluation of the file name pattern using the configured ConfigurationInterpolator
. If this is the
first access to this configuration file, the builder is created.ConfigurationException
- if the builder cannot be determined (e.g. due to missing initialization parameters)public <E extends Event> void addEventListener(EventType<E> eventType, EventListener<? super E> l)
addEventListener
in interface EventSource
addEventListener
in class BasicConfigurationBuilder<T extends FileBasedConfiguration>
E
- the type of events processed by this listenereventType
- the event type (must not be null)l
- the listener to be registered (must not be null)public <E extends Event> boolean removeEventListener(EventType<E> eventType, EventListener<? super E> l)
removeEventListener
in interface EventSource
removeEventListener
in class BasicConfigurationBuilder<T extends FileBasedConfiguration>
E
- the type of events processed by this listenereventType
- the event typel
- the event listener to be removedpublic void resetParameters()
resetParameters
in class BasicConfigurationBuilder<T extends FileBasedConfiguration>
protected ConfigurationInterpolator getInterpolator()
ConfigurationInterpolator
used by this instance. This is the object used for evaluating the file
name pattern. It is created on demand.ConfigurationInterpolator
protected ConfigurationInterpolator createInterpolator()
ConfigurationInterpolator
to be used by this instance. This method is called when a file name is
to be constructed, but no current ConfigurationInterpolator
instance is available. It obtains an instance
from this builder's parameters. If no properties of the ConfigurationInterpolator
are specified in the
parameters, a default instance without lookups is returned (which is probably not very helpful).ConfigurationInterpolator
to be usedprotected String constructFileName(MultiFileBuilderParametersImpl multiParams)
ConfigurationInterpolator
from this builder's parameters and
uses it to interpolate the file name pattern.multiParams
- the parameters object for this builderprotected FileBasedConfigurationBuilder<T> createManagedBuilder(String fileName, Map<String,Object> params) throws ConfigurationException
fileName
- the name of the file to be loadedparams
- a map with initialization parameters for the new builderConfigurationException
- if an error occursprotected FileBasedConfigurationBuilder<T> createInitializedManagedBuilder(String fileName, Map<String,Object> params) throws ConfigurationException
getConfiguration()
whenever a configuration file is requested which has not yet been loaded. This implementation delegates to
createManagedBuilder()
for actually creating the builder object. Then it sets the location to the
configuration file.fileName
- the name of the file to be loadedparams
- a map with initialization parameters for the new builderConfigurationException
- if an error occursprotected ConcurrentMap<String,FileBasedConfigurationBuilder<T>> getManagedBuilders()
MultiFileConfigurationBuilder
. This map is
exposed to derived classes so they can access managed builders directly. However, derived classes are not expected to
manipulate this map.Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.