Class MCRConfiguration2

java.lang.Object
org.mycore.common.config.MCRConfiguration2

public class MCRConfiguration2 extends Object
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 Details

    • MCRConfiguration2

      public MCRConfiguration2()
  • Method Details

    • getPropertiesMap

      public static Map<String,String> getPropertiesMap()
    • getSubPropertiesMap

      public static Map<String,String> getSubPropertiesMap(String propertyPrefix)
      Returns a sub map of properties where key is transformed.
      1. if property starts with propertyPrefix, the property is in the result map
      2. the key of the target map is the name of the property without propertPrefix
      Example for propertyPrefix="MCR.Foo.":
           MCR.Foo.Bar=Baz
           MCR.Foo.Hello=World
           MCR.Other.Prop=Value
       
      will result in
           Bar=Baz
           Hello=World
       
      Parameters:
      propertyPrefix - prefix of the property name
      Returns:
      a map of the properties as stated above
    • getInstanceOf

      public static <T> Optional<T> getInstanceOf(String name) throws MCRConfigurationException
      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

      public static <T> Optional<T> getSingleInstanceOf(String name)
      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

      public 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. 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 property
      alternative - 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 property name.
      Type Parameters:
      T - Supertype of class defined in name
      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

      public static Optional<String> getString(String name)
      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

      public static String getStringOrThrow(String name)
      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

      public static <T> T getOrThrow(String name, Function<String,? extends T> mapper)
      Returns the configuration property with the specified name.
      Parameters:
      name - the non-null and non-empty name of the configuration property
      mapper - maps the String value to the return value
      Throws:
      MCRConfigurationException - if property is not set
    • createConfigurationException

      public static MCRConfigurationException createConfigurationException(String propertyName)
    • splitValue

      public static Stream<String> splitValue(String value)
      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

      public static Stream<String> getInstantiatablePropertyKeys(String prefix)
      Parameters:
      prefix -
      Returns:
      a list of properties which represent a configurable class
    • getInstances

      public static <T> Map<String,Callable<T>> getInstances(String prefix)
      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

      public static Optional<Integer> getInt(String name) throws NumberFormatException
      Returns the configuration property with the specified name as an int 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 an int value
    • getLong

      public static Optional<Long> getLong(String name) throws NumberFormatException
      Returns the configuration property with the specified name as a long 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 a long value
    • getFloat

      public static Optional<Float> getFloat(String name) throws NumberFormatException
      Returns the configuration property with the specified name as a float 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 a float value
    • getDouble

      public static Optional<Double> getDouble(String name) throws NumberFormatException
      Returns the configuration property with the specified name as a double 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 a double value
    • getBoolean

      public static Optional<Boolean> getBoolean(String name)
      Returns the configuration property with the specified name as a boolean 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 value true
    • set

      public static void set(String name, String value)
      Sets the configuration property with the specified name to a new String value. If the parameter value is null, the property will be deleted.
      Parameters:
      name - the non-null and non-empty name of the configuration property
      value - the new value of the configuration property, possibly null
    • set

      public static void set(String name, Supplier<String> value)
    • set

      public static <T> void set(String name, T value, Function<T,String> mapper)
    • 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 listener
      listener - a MCRTriConsumer with property name as first argument and than old and new value as Optional.
      Returns:
      a UUID to remove the listener later
    • removePropertyChangeEventListener

      public static boolean removePropertyChangeEventListener(UUID uuid)
    • instantiateClass

      public static <T> T instantiateClass(String classname)