Package org.mycore.common.config
Class MCRConfiguration2
java.lang.Object
org.mycore.common.config.MCRConfiguration2
Provides methods to manage and read all configuration properties from the MyCoRe configuration files.
The Properties used by this class are used from
MCRConfigurationBase
.
NOTE
All Optional
values returned by this class are empty
if the property
is not set OR the trimmed value is empty
. If you want to distinguish between
empty properties and unset properties use MCRConfigurationBase.getString(String)
instead.
Using this class is very easy, here is an example:
// Get a configuration property as a String: String sValue = MCRConfiguration2.getString("MCR.String.Value").orElse(defaultValue); // Get a configuration property as a List of String (values are seperated by ","): List<String> lValue = MCRConfiguration2.getString("MCR.StringList.Value").stream() .flatMap(MCRConfiguration2::splitValue) .collect(Collectors.toList()); // Get a configuration property as a long array (values are seperated by ","): long[] la = MCRConfiguration2.getString("MCR.LongList.Value").stream() .flatMap(MCRConfiguration2::splitValue) .mapToLong(Long::parseLong) .toArray(); // Get a configuration property as an int, use 500 as default if not set: int max = MCRConfiguration2.getInt("MCR.Cache.Size").orElse(500);There are some helper methods to help you with converting values As you see, the class provides methods to get configuration properties as different data types and allows you to specify defaults. All MyCoRe configuration properties should start with "
MCR.
"
Using the set
methods allows client code to set new configuration properties or
overwrite existing ones with new values.- Since:
- 2018.05
- Author:
- Thomas Scheffler (yagee)
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic UUID
addPropertyChangeEventLister
(Predicate<String> keyPredicate, MCRTriConsumer<String, Optional<String>, Optional<String>> listener) Adds a listener that is called after a new value is set.static MCRConfigurationException
createConfigurationException
(String propertyName) getBoolean
(String name) Returns the configuration property with the specified name as aboolean
value.Loads a Java Class defined in propertyname
.Returns the configuration property with the specified name as adouble
value.Returns the configuration property with the specified name as afloat
value.static <T> Optional<T>
getInstanceOf
(String name) Returns a new instance of the class specified in the configuration property with the given name.getInstances
(String prefix) Gets a list of properties which represent a configurable class and turns them in to a map.getInstantiatablePropertyKeys
(String prefix) Returns the configuration property with the specified name as anint
value.Returns the configuration property with the specified name as along
value.static <T> T
getOrThrow
(String name, Function<String, ? extends T> mapper) Returns the configuration property with the specified name.static <T> Optional<T>
getSingleInstanceOf
(String name) Returns a instance of the class specified in the configuration property with the given name.static <T> Optional<T>
getSingleInstanceOf
(String name, Class<? extends T> alternative) Returns a instance of the class specified in the configuration property with the given name.Returns the configuration property with the specified name.static String
getStringOrThrow
(String name) Returns the configuration property with the specified name as String.getSubPropertiesMap
(String propertyPrefix) Returns a sub map of properties where key is transformed.static <T> T
instantiateClass
(String classname) static boolean
static void
Sets the configuration property with the specified name to a newString
value.static void
static <T> void
splitValue
(String value) Splits a String value in a Stream of trimmed non-empty Strings.
-
Constructor Details
-
MCRConfiguration2
public MCRConfiguration2()
-
-
Method Details
-
getPropertiesMap
-
getSubPropertiesMap
Returns a sub map of properties where key is transformed.- if property starts with
propertyPrefix
, the property is in the result map - the key of the target map is the name of the property without
propertPrefix
propertyPrefix="MCR.Foo."
:MCR.Foo.Bar=Baz MCR.Foo.Hello=World MCR.Other.Prop=Value
will result inBar=Baz Hello=World
- Parameters:
propertyPrefix
- prefix of the property name- Returns:
- a map of the properties as stated above
- if property starts with
-
getInstanceOf
Returns a new instance of the class specified in the configuration property with the given name. If you call a method on the returned Optional directly you need to set the type like this:MCRConfiguration.<MCRMyType> getInstanceOf(name) .ifPresent(myTypeObj -> myTypeObj.method());
- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as a String, or null
- Throws:
MCRConfigurationException
- if the class can not be loaded or instantiated
-
getSingleInstanceOf
Returns a instance of the class specified in the configuration property with the given name. If the class was previously instantiated by this method this instance is returned. If you call a method on the returned Optional directly you need to set the type like this:MCRConfiguration.<MCRMyType> getSingleInstanceOf(name) .ifPresent(myTypeObj -> myTypeObj.method());
- Parameters:
name
- non-null and non-empty name of the configuration property- Returns:
- the instance of the class named by the value of the configuration property
- Throws:
MCRConfigurationException
- if the class can not be loaded or instantiated
-
getSingleInstanceOf
Returns a instance of the class specified in the configuration property with the given name. If the class was previously instantiated by this method this instance is returned. If you call a method on the returned Optional directly you need to set the type like this:MCRConfiguration.<MCRMyType> getSingleInstanceOf(name, alternative) .ifPresent(myTypeObj -> myTypeObj.method());
- Parameters:
name
- non-null and non-empty name of the configuration propertyalternative
- alternative class if property is undefined- Returns:
- the instance of the class named by the value of the configuration property
- Throws:
MCRConfigurationException
- if the class can not be loaded or instantiated
-
getClass
public static <T> Optional<Class<? extends T>> getClass(String name) throws MCRConfigurationException Loads a Java Class defined in propertyname
.- Type Parameters:
T
- Supertype of class defined inname
- Parameters:
name
- Name of the property- Returns:
- Optional of Class asignable to
<T>
- Throws:
MCRConfigurationException
- if the the class can not be loaded or instantiated
-
getString
Returns the configuration property with the specified name. If the value of the property is empty after trimming the returned Optional is empty.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as an
Optional<String>
-
getStringOrThrow
Returns the configuration property with the specified name as String.- Parameters:
name
- the non-null and non-empty name of the configuration property- Throws:
MCRConfigurationException
- if property is not set
-
getOrThrow
Returns the configuration property with the specified name.- Parameters:
name
- the non-null and non-empty name of the configuration propertymapper
- maps the String value to the return value- Throws:
MCRConfigurationException
- if property is not set
-
createConfigurationException
-
splitValue
Splits a String value in a Stream of trimmed non-empty Strings. This method can be used to split a property value delimited by ',' into values.Example:
MCRConfiguration2.getOrThrow("MCR.ListProp", MCRConfiguration2::splitValue)
.map(Integer::parseInt)
.collect(Collectors.toList())
- Parameters:
value
- a property value- Returns:
- a Stream of trimmed, non-empty Strings
-
getInstantiatablePropertyKeys
- Parameters:
prefix
-- Returns:
- a list of properties which represent a configurable class
-
getInstances
Gets a list of properties which represent a configurable class and turns them in to a map.- Type Parameters:
T
-- Parameters:
prefix
-- Returns:
- a map where the key is a String describing the configurable instance value
-
getInt
Returns the configuration property with the specified name as anint
value.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as an
int
value - Throws:
NumberFormatException
- if the configuration property is not anint
value
-
getLong
Returns the configuration property with the specified name as along
value.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as a
long
value - Throws:
NumberFormatException
- if the configuration property is not along
value
-
getFloat
Returns the configuration property with the specified name as afloat
value.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as a
float
value - Throws:
NumberFormatException
- if the configuration property is not afloat
value
-
getDouble
Returns the configuration property with the specified name as adouble
value.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
- the value of the configuration property as a
double
value - Throws:
NumberFormatException
- if the configuration property is not adouble
value
-
getBoolean
Returns the configuration property with the specified name as aboolean
value.- Parameters:
name
- the non-null and non-empty name of the configuration property- Returns:
true
, if and only if the specified property has the valuetrue
-
set
Sets the configuration property with the specified name to a newString
value. If the parametervalue
isnull
, the property will be deleted.- Parameters:
name
- the non-null and non-empty name of the configuration propertyvalue
- the new value of the configuration property, possiblynull
-
set
-
set
-
addPropertyChangeEventLister
public static UUID addPropertyChangeEventLister(Predicate<String> keyPredicate, MCRTriConsumer<String, Optional<String>, Optional<String>> listener) Adds a listener that is called after a new value is set.- Parameters:
keyPredicate
- a filter upon the property name that if matches executes the listenerlistener
- aMCRTriConsumer
with property name as first argument and than old and new value as Optional.- Returns:
- a UUID to
remove the listener
later
-
removePropertyChangeEventListener
-
instantiateClass
-