public class ConfigurationInterpolator extends Object
A class that handles interpolation (variable substitution) for configuration objects.
Each instance of
AbstractConfiguration is associated with an object
of this class. All interpolation tasks are delegated to this object.
ConfigurationInterpolator internally uses the
class from Commons Text. Thus it
supports the same syntax of variable expressions.
The basic idea of this class is that it can maintain a set of primitive
Lookup objects, each of which is identified by a special prefix. The
variables to be processed have the form
ConfigurationInterpolator will extract the prefix and determine,
which primitive lookup object is registered for it. Then the name of the
variable is passed to this object to obtain the actual value. It is also
possible to define an arbitrary number of default lookup objects, which are
used for variables that do not have a prefix or that cannot be resolved by
their associated lookup object. When adding default lookup objects their
order matters; they are queried in this order, and the first non-null
variable value is used.
After an instance has been created it does not contain any
objects. The current set of lookup objects can be modified using the
deregisterLookup() methods. Default
lookup objects (that are invoked for variables without a prefix) can be added
or removed with the
removeDefaultLookup() methods respectively. (When a
ConfigurationInterpolator instance is created by a configuration
object, a default lookup object is added pointing to the configuration
itself, so that variables are resolved using the configuration's properties.)
The default usage scenario is that on a fully initialized instance the
interpolate() method is called. It is passed an object value which
may contain variables. All these variables are substituted if they can be
resolved. The result is the passed in value with variables replaced.
resolve() method can be called to obtain the
values of specific variables without performing interpolation.
Implementation node: This class is thread-safe. Lookup objects can be added or removed at any time concurrent to interpolation operations.
|Constructor and Description|
Creates a new instance of
|Modifier and Type||Method and Description|
Adds a default
Obtains the lookup object for the specified prefix.
Creates a new
Returns a collection with the default
Returns a map containing the default prefix lookups.
Returns a map with the currently registered
Returns the parent
Performs interpolation of the passed in value.
Sets a flag that variable names can contain other variables.
Utility method for obtaining a
Returns an unmodifiable set with the prefixes, for which
Registers the given
Removes the specified
Resolves the specified variable.
Sets the flag whether variable names can contain other variables.
Sets the parent
public static ConfigurationInterpolator fromSpecification(InterpolatorSpecification spec)
ConfigurationInterpolatorinstance based on the passed in specification object. If the
InterpolatorSpecificationalready contains a
ConfigurationInterpolatorobject, it is used directly. Otherwise, a new instance is created and initialized with the properties stored in the specification.
InterpolatorSpecification(must not be null)
ConfigurationInterpolatorobtained or created based on the given specification
IllegalArgumentException- if the specification is null
public static Map<String,Lookup> getDefaultPrefixLookups()
AbstractConfigurationis by default initialized with a
Lookupobjects and their prefixes. The map cannot be modified
Lookupobjects and their prefixes
public static Lookup nullSafeLookup(Lookup lookup)
Lookupobject in a safe way. This method always returns a non-null
Lookupobject. If the passed in
Lookupis not null, it is directly returned. Otherwise, result is a dummy
Lookupwhich does not provide any values.
public void addDefaultLookup(Lookup defaultLookup)
Lookupobjects are queried (in the order they were added) for all variables without a special prefix. If no default
Lookupobjects are present, such variables won't be processed.
defaultLookup- the default
Lookupobject to be added (must not be null)
IllegalArgumentException- if the
Lookupobject is null
public void addDefaultLookups(Collection<? extends Lookup> lookups)
Lookupobjects in the given collection as default lookups. The collection can be null, then this method has no effect. It must not contain null entries.
Lookupobjects to be added as default lookups
IllegalArgumentException- if the collection contains a null entry
public boolean deregisterLookup(String prefix)
Lookupobject for the specified prefix at this instance. It will be removed from this instance.
prefix- the variable prefix
protected Lookup fetchLookupForPrefix(String prefix)
lookup()method. This implementation will check whether a lookup object is registered for the given prefix. If not, a null lookup object will be returned (never null).
prefix- the prefix
public List<Lookup> getDefaultLookups()
Lookupobjects added to this
ConfigurationInterpolator. These objects are not associated with a variable prefix. The returned list is a snapshot copy of the internal collection of default lookups; so manipulating it does not affect this instance.
public Map<String,Lookup> getLookups()
Lookupobjects and their prefixes. This is a snapshot copy of the internally used map. So modifications of this map do not effect this instance.
ConfigurationInterpolator(can be null)
public Object interpolate(Object value)
value- the value to be interpolated
public boolean isEnableSubstitutionInVariables()
Lookupobjects are registered at this instance. This means that variables with these prefixes can be processed.
public void registerLookup(String prefix, Lookup lookup)
Lookupobject for the specified prefix at this instance. From now on this lookup object will be used for variables that have the specified prefix.
prefix- the variable prefix (must not be null)
Lookupobject to be used for this prefix (must not be null)
IllegalArgumentException- if either the prefix or the
Lookupobject is null
public void registerLookups(Map<String,? extends Lookup> lookups)
Lookupobjects in the given map with their prefixes at this
ConfigurationInterpolator. Using this method multiple
Lookupobjects can be registered at once. If the passed in map is null, this method does not have any effect.
lookups- the map with lookups to register (may be null)
IllegalArgumentException- if the map contains entries
public boolean removeDefaultLookup(Lookup lookup)
Lookupobject from the list of default
Lookupobject to be removed
Lookupobject actually existed and was removed
ConfigurationInterpolatoris available, this object is asked to resolve the variable.
var- the name of the variable whose value is to be looked up which may contain a prefix.
public void setEnableSubstitutionInVariables(boolean f)
enableSubstitutionInVariablesproperty of the underlying
f- the new value of the flag
public void setParentInterpolator(ConfigurationInterpolator parentInterpolator)
ConfigurationInterpolator. This object is used if the
Lookupobjects registered at this object cannot resolve a variable.
parentInterpolator- the parent
ConfigurationInterpolatorobject (can be null)
Copyright © 2001–2020 The Apache Software Foundation. All rights reserved.