Package org.kiwiproject.config.provider
Class ExternalConfigProvider
- java.lang.Object
-
- org.kiwiproject.config.provider.ExternalConfigProvider
-
- All Implemented Interfaces:
ConfigProvider
public class ExternalConfigProvider extends Object implements ConfigProvider
Config provider that looks up configuration values from a known properties file. This provider loads the properties so that other providers can access the values.The provider will look for the external config file in the following order:
- System property with the given system property key
- System property with the default system property key (kiwi.external.config.path)
- Environment variable with the given variable name
- Environment variable with the default variable name (KIWI_EXTERNAL_CONFIG_PATH)
- The given properties path
- The default properties path (~/.kiwi.external.config.properties)
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canProvide()
This provider can provide if the properties have been loaded from the given filestatic ExternalConfigProvider
getExternalPropertyProviderOrDefault(ExternalConfigProvider provided)
Will check if a givenExternalConfigProvider
is null and return a new one if so.Optional<String>
getProperty(String propertyKey)
Returns a property for a given key if it exists otherwise anOptional.empty()
.<T> ResolverResult<T>
resolveExternalProperty(String propertyKey, Function<String,ResolverResult<T>> propertyValueFunction, Supplier<ResolverResult<T>> orElseSupplier)
Executes a given function if the requested property is found, otherwise runs theorElseSupplier
supplier.void
usePropertyIfPresent(String propertyKey, Consumer<String> propertyValueConsumer, Runnable orElse)
Executes given consumer if the requested property is found, otherwise runs theorElse
function.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.kiwiproject.config.provider.ConfigProvider
canNotProvide, getResolvedBy
-
-
-
-
Method Detail
-
canProvide
public boolean canProvide()
This provider can provide if the properties have been loaded from the given file- Specified by:
canProvide
in interfaceConfigProvider
- Returns:
true
if properties are loaded,false
otherwise.
-
getProperty
public Optional<String> getProperty(String propertyKey)
Returns a property for a given key if it exists otherwise anOptional.empty()
.- Parameters:
propertyKey
- the key of the property to look up- Returns:
- An
Optional
with the requested property or empty if not found
-
usePropertyIfPresent
public void usePropertyIfPresent(String propertyKey, Consumer<String> propertyValueConsumer, Runnable orElse)
Executes given consumer if the requested property is found, otherwise runs theorElse
function.- Parameters:
propertyKey
- the key of the property to look uppropertyValueConsumer
- a consumer to process the value of the property if foundorElse
- a runnable to execute if the property is not found
-
resolveExternalProperty
public <T> ResolverResult<T> resolveExternalProperty(String propertyKey, Function<String,ResolverResult<T>> propertyValueFunction, Supplier<ResolverResult<T>> orElseSupplier)
Executes a given function if the requested property is found, otherwise runs theorElseSupplier
supplier.- Type Parameters:
T
- The class type of the value inside the ResolverResult- Parameters:
propertyKey
- the key of the property to look uppropertyValueFunction
- a function to process the value of the property if found and return aResolverResult
orElseSupplier
- a supplier to execute if the property is not found and return aResolverResult
- Returns:
- a
ResolverResult
containing the resolved value and the resolution method
-
getExternalPropertyProviderOrDefault
public static ExternalConfigProvider getExternalPropertyProviderOrDefault(ExternalConfigProvider provided)
Will check if a givenExternalConfigProvider
is null and return a new one if so. Otherwise return the one passed in.- Parameters:
provided
- anExternalConfigProvider
to test for null- Returns:
- A new
ExternalConfigProvider
if the givenprovided
is null orprovided
-
-