public class ConfigurationInterpolator
extends org.apache.commons.lang.text.StrLookup
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
works together with the
StrSubstitutor
class from Commons Lang. By extending
StrLookup
it is able to provide values for variables that
appear in expressions.
The basic idea of this class is that it can maintain a set of primitive
StrLookup
objects, each of which is identified by a special
prefix. The variables to be processed have the form
${prefix:name}
. 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 a default lookup object, which
will be used for variables that do not have a prefix or that cannot be
resolved by their associated lookup object.
When a new instance of this class is created it is initialized with a default
set of primitive lookup objects. This set can be customized using the static
methods registerGlobalLookup()
and
deregisterGlobalLookup()
. Per default it contains the
following standard lookup objects:
Prefix | Lookup object |
---|---|
sys | With this prefix a lookup object is associated that is able to resolve system properties. |
const | The const prefix indicates that a variable is to be
interpreted as a constant member field of a class (i.e. a field with the
static final modifiers). The name of the variable must be of the form
<full qualified class name>.<field name> , e.g.
org.apache.commons.configuration.interpol.ConfigurationInterpolator.PREFIX_CONSTANTS .
|
After an instance has been created the current set of lookup objects can be
modified using the registerLookup()
and
deregisterLookup()
methods. The default lookup object (that is
invoked for variables without a prefix) can be set with the
setDefaultLookup()
method. (If a
ConfigurationInterpolator
instance is created by a
configuration object, this lookup points to the configuration itself, so that
variables are resolved using the configuration's properties. This ensures
backward compatibility to earlier version of Commons Configuration.)
Implementation node: Instances of this class are not thread-safe related to
modifications of their current set of registered lookup objects. It is
intended that each instance is associated with a single
Configuration
object and used for its interpolation tasks.
Modifier and Type | Field and Description |
---|---|
static String |
PREFIX_CONSTANTS
Constant for the prefix of the standard lookup object for resolving
constant values.
|
static String |
PREFIX_ENVIRONMENT
Constant for the prefix of the standard lookup object for resolving
environment properties.
|
static String |
PREFIX_SYSPROPERTIES
Constant for the prefix of the standard lookup object for resolving
system properties.
|
Constructor and Description |
---|
ConfigurationInterpolator()
Creates a new instance of
ConfigurationInterpolator . |
Modifier and Type | Method and Description |
---|---|
static boolean |
deregisterGlobalLookup(String prefix)
Deregisters the global lookup object for the specified prefix.
|
boolean |
deregisterLookup(String prefix)
Deregisters the lookup object for the specified prefix at this instance.
|
protected org.apache.commons.lang.text.StrLookup |
fetchLookupForPrefix(String prefix)
Obtains the lookup object for the specified prefix.
|
protected org.apache.commons.lang.text.StrLookup |
fetchNoPrefixLookup()
Returns the lookup object to be used for variables without a prefix.
|
org.apache.commons.lang.text.StrLookup |
getDefaultLookup()
Returns the default lookup object.
|
ConfigurationInterpolator |
getParentInterpolator()
Requests the parent interpolator.
|
String |
lookup(String var)
Resolves the specified variable.
|
Set<String> |
prefixSet()
Returns a set with the prefixes, for which lookup objects are registered
at this instance.
|
static void |
registerGlobalLookup(String prefix,
org.apache.commons.lang.text.StrLookup lookup)
Registers the given lookup object for the specified prefix globally.
|
void |
registerLocalLookups(ConfigurationInterpolator interpolator)
Registers the local lookup instances for the given interpolator.
|
void |
registerLookup(String prefix,
org.apache.commons.lang.text.StrLookup lookup)
Registers the given lookup object for the specified prefix at this
instance.
|
void |
setDefaultLookup(org.apache.commons.lang.text.StrLookup defaultLookup)
Sets the default lookup object.
|
void |
setParentInterpolator(ConfigurationInterpolator parentInterpolator)
Sets the parent interpolator.
|
public static final String PREFIX_SYSPROPERTIES
public static final String PREFIX_CONSTANTS
public static final String PREFIX_ENVIRONMENT
public ConfigurationInterpolator()
ConfigurationInterpolator
.public static void registerGlobalLookup(String prefix, org.apache.commons.lang.text.StrLookup lookup)
prefix
- the variable prefix (must not be null)lookup
- the lookup object to be used for this prefix (must not be
null)public static boolean deregisterGlobalLookup(String prefix)
prefix
- the variable prefixpublic void registerLookup(String prefix, org.apache.commons.lang.text.StrLookup lookup)
prefix
- the variable prefix (must not be null)lookup
- the lookup object to be used for this prefix (must not be
null)public boolean deregisterLookup(String prefix)
prefix
- the variable prefixpublic Set<String> prefixSet()
public org.apache.commons.lang.text.StrLookup getDefaultLookup()
public void setDefaultLookup(org.apache.commons.lang.text.StrLookup defaultLookup)
defaultLookup
- the new default lookup objectpublic String lookup(String var)
lookup
in class org.apache.commons.lang.text.StrLookup
var
- the name of the variable whose value is to be looked upprotected org.apache.commons.lang.text.StrLookup fetchNoPrefixLookup()
protected org.apache.commons.lang.text.StrLookup 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 prefixpublic void registerLocalLookups(ConfigurationInterpolator interpolator)
interpolator
- the instance receiving the local lookupspublic void setParentInterpolator(ConfigurationInterpolator parentInterpolator)
parentInterpolator
- the parent interpolator object or nullpublic ConfigurationInterpolator getParentInterpolator()
Copyright © 2001–2013 The Apache Software Foundation. All rights reserved.