@Typed public final class ConfigResolver extends Object
Resolves configured values of properties by going through the list of configured ConfigSource
s and using the
one with the highest ordinal. If multiple ConfigSource
s have the same ordinal, their order is undefined.
You can provide your own lookup paths by implementing and registering additional PropertyFileConfig
or
ConfigSource
or ConfigSourceProvider
implementations.
The resolved configuration is also accessible by simple injection using the ConfigProperty
qualifier.
Modifier and Type | Method and Description |
---|---|
static void |
addConfigFilter(ConfigFilter configFilter)
Add a
ConfigFilter to the ConfigResolver. |
static void |
addConfigSources(List<ConfigSource> configSourcesToAdd)
This method can be used for programmatically adding
ConfigSource s. |
static String |
filterConfigValue(String key,
String value)
Filter the configured value.
|
static String |
filterConfigValueForLog(String key,
String value)
Filter the configured value for logging.
|
static void |
freeConfigSources()
Clear all ConfigSources for the current ClassLoader.
|
static Map<String,String> |
getAllProperties()
Returns a Map of all properties from all scannable config sources.
|
static List<String> |
getAllPropertyValues(String key)
Resolve all values for the given key.
|
static List<ConfigFilter> |
getConfigFilters() |
static ConfigSource[] |
getConfigSources() |
static String |
getProjectStageAwarePropertyValue(String key)
Resolves the value configured for the given key in the current
ProjectStage . |
static String |
getProjectStageAwarePropertyValue(String key,
String defaultValue)
getProjectStageAwarePropertyValue(String) which returns the provided default value if no configured
value can be found (null or empty). |
static String |
getPropertyAwarePropertyValue(String key,
String property)
Resolves the value configured for the given key, parameterized by the current
ProjectStage and by the value of a second property. |
static String |
getPropertyAwarePropertyValue(String key,
String property,
String defaultValue)
getPropertyAwarePropertyValue(java.lang.String, java.lang.String) which returns the provided default
value if no configured value can be found (null or empty). |
static String |
getPropertyValue(String key)
Resolves the value configured for the given key.
|
static String |
getPropertyValue(String key,
String defaultValue)
getPropertyValue(java.lang.String) which returns the provided default value if no configured value can
be found (null or empty). |
public static void addConfigSources(List<ConfigSource> configSourcesToAdd)
ConfigSource
s.
It is not needed for normal 'usage' by end users, but only for Extension Developers!configSourcesToAdd
- the ConfigSources to addpublic static void freeConfigSources()
public static void addConfigFilter(ConfigFilter configFilter)
ConfigFilter
to the ConfigResolver. This will only affect the current WebApp (or more precisely the
current ClassLoader and it's children).configFilter
- public static List<ConfigFilter> getConfigFilters()
ConfigFilter
s for the current application.public static String getPropertyValue(String key, String defaultValue)
getPropertyValue(java.lang.String)
which returns the provided default value if no configured value can
be found (null
or empty).key
- the property keydefaultValue
- fallback valueConfigSource
with the highest ordinal or the defaultValue
if there is no value explicitly configuredpublic static String getPropertyValue(String key)
key
- the property keyConfigSource
with the highest ordinal or null if there is
no configured value for itpublic static String getProjectStageAwarePropertyValue(String key)
ProjectStage
.
First, it will search for a value configured for the given key suffixed with the current ProjectStage (e.g. 'myproject.myconfig.Production'), and in case this value is not found (null or empty), it will look up the given key without any suffix.
Attention This method must only be used after all ConfigSources got registered and it also must not be used to determine the ProjectStage itself.
key
- <given key>.<current project stage>
, or just the configured value of
<given key>
if the project-stage-specific value is not found (null or empty)public static String getProjectStageAwarePropertyValue(String key, String defaultValue)
getProjectStageAwarePropertyValue(String)
which returns the provided default value if no configured
value can be found (null
or empty).key
- defaultValue
- fallback valuepublic static String getPropertyAwarePropertyValue(String key, String property)
ProjectStage
and by the value of a second property.
Example:
Suppose the current ProjectStage is UnitTest
and we are looking for the value of datasource
parameterized by the configured dbvendor
.
The first step is to resolve the value of the second property, dbvendor
. This will also take the current
ProjectStage into account. The following lookup is performed:
If a value was found for the second property (e.g. dbvendor = 'mysql') then we will use its value for the main
lookup. If no value is found for the parameterized key <key>.<second property value>.<project stage>
, we
will do the <key>.<second property value>
, then <key>.<project stage>
and finally a <key>
lookup:
Attention This method must only be used after all ConfigSources got registered and it also must not be used to determine the ProjectStage itself.
key
- property
- the property to look up first and use as the parameter for the main lookuppublic static String getPropertyAwarePropertyValue(String key, String property, String defaultValue)
getPropertyAwarePropertyValue(java.lang.String, java.lang.String)
which returns the provided default
value if no configured value can be found (null
or empty).
Attention This method must only be used after all ConfigSources got registered and it also must not be used to determine the ProjectStage itself.
key
- property
- the property to look up first and use as the parameter for the main lookupdefaultValue
- fallback valuepublic static List<String> getAllPropertyValues(String key)
key
- ConfigSource.getOrdinal()
public static Map<String,String> getAllProperties()
getPropertyValue(java.lang.String)
, that is, the value of the
property from the ConfigSource with the highest ordinal.ConfigSource.isScannable()
public static ConfigSource[] getConfigSources()
public static String filterConfigValue(String key, String value)
Copyright © 2015 The Apache Software Foundation. All rights reserved.