Class MetatypeUtils

java.lang.Object
com.ibm.wsspi.kernel.service.utils.MetatypeUtils

public class MetatypeUtils extends Object
Utility class to simplify retrieving values from injected config.

The presence of the Metatype service will cause ConfigAdmin to use real types (in addition to String) as values in the the configuration properties provided to declarative services components.

This class provides utility methods for retrieving a desired type (a boolean, a long, an array of strings.. ) from a component's configuration properties. They handle objects of either type (those already created/populated as the right type, or values that are still encoded as strings).

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Long
    evaluateDuration(String strVal, TimeUnit endUnit)
    Converts a string value representing a unit of time into a Long value.
    static String
    Converts a String value into a token value by collapsing whitespace.
    static boolean
    parseBoolean(Object configAlias, String propertyKey, Object obj, boolean defaultValue)
    Parse a boolean from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted boolean.
    static long
    parseDuration(Object configAlias, String propertyKey, Object obj, long defaultValue)
    Parse a duration from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long duration value
    static long
    parseDuration(Object configAlias, String propertyKey, Object obj, long defaultValue, TimeUnit units)
    Parse a duration from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long duration value
    static int
    parseInteger(Object configAlias, String propertyKey, Object obj, int defaultValue)
    Parse a int from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted int
    static int[]
    parseIntegerArray(Object configAlias, String propertyKey, Object obj, int[] defaultValue)
    Parse a string array from the provided config value: returns an array of strings generated from either a comma-separated single string value, or a metatype generated string array.
    static long
    parseLong(Object configAlias, String propertyKey, Object obj, long defaultValue)
    Parse a long from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long
    static String[]
    parseStringArray(Object configAlias, String propertyKey, Object obj, String[] defaultValue)
    Parse a string array from the provided config value: returns an array of strings generated from either a comma-separated single string value, or a metatype generated string array.
    parseStringCollection(Object configAlias, String propertyKey, Object obj, Collection<String> defaultValue)
    Parse a string collection from the provided config value: returns a collection of strings generated from either a comma-separated single string value, or a metatype provided string collection.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MetatypeUtils

      public MetatypeUtils()
  • Method Details

    • parseBoolean

      public static boolean parseBoolean(Object configAlias, String propertyKey, Object obj, boolean defaultValue)
      Parse a boolean from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted boolean.

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      Returns:
      boolean parsed from obj, the default value if obj is null.
      Throws:
      IllegalArgumentException - If value is not a String/Boolean, or if the String boolean is not "true" or "false" (ignoring case)
    • parseStringArray

      public static String[] parseStringArray(Object configAlias, String propertyKey, Object obj, String[] defaultValue)
      Parse a string array from the provided config value: returns an array of strings generated from either a comma-separated single string value, or a metatype generated string array.

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      An array of strings parsed from the obj parameter, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String or String array, or if an error occurs while converting/casting the object to the return parameter type.
    • parseStringCollection

      public static Collection<String> parseStringCollection(Object configAlias, String propertyKey, Object obj, Collection<String> defaultValue)
      Parse a string collection from the provided config value: returns a collection of strings generated from either a comma-separated single string value, or a metatype provided string collection.

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      Collection of strings parsed/retrieved from obj, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String, String collection, or String array, or if an error occurs while converting/casting the object to the return parameter type.
    • parseLong

      public static long parseLong(Object configAlias, String propertyKey, Object obj, long defaultValue)
      Parse a long from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      Long parsed/retrieved from obj, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String/Short/Integer/Long, or if an error occurs while converting/casting the object to the return parameter type.
    • parseInteger

      public static int parseInteger(Object configAlias, String propertyKey, Object obj, int defaultValue)
      Parse a int from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted int

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      Integer parsed/retrieved from obj, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String/Short/Integer/Long, or if an error occurs while converting/casting the object to the return parameter type.
    • parseIntegerArray

      public static int[] parseIntegerArray(Object configAlias, String propertyKey, Object obj, int[] defaultValue)
      Parse a string array from the provided config value: returns an array of strings generated from either a comma-separated single string value, or a metatype generated string array.

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      An array of strings parsed from the obj parameter, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String or String array, or if an error occurs while converting/casting the object to the return parameter type.
    • parseDuration

      public static long parseDuration(Object configAlias, String propertyKey, Object obj, long defaultValue)
      Parse a duration from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long duration value

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      Returns:
      Long parsed/retrieved from obj, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a Long, or if an error occurs while converting/casting the object to the return parameter type.
    • parseDuration

      public static long parseDuration(Object configAlias, String propertyKey, Object obj, long defaultValue, TimeUnit units)
      Parse a duration from the provided config value: checks for whether or not the object read from the Service/Component configuration is a String or a Metatype converted long duration value

      If an exception occurs converting the object parameter: A translated warning message will be issued using the provided propertyKey and object as parameters. FFDC for the exception is suppressed: Callers should handle the thrown IllegalArgumentException as appropriate.

      Parameters:
      configAlias - Name of config (pid or alias) associated with a registered service or DS component.
      propertyKey - The key used to retrieve the property value from the map. Used in the warning message if the value is badly formed.
      obj - The object retrieved from the configuration property map/dictionary.
      defaultValue - The default value that should be applied if the object is null.
      units - The unit of time for the duration value. This is only used when converting from a String value.
      Returns:
      Long parsed/retrieved from obj, or default value if obj is null
      Throws:
      IllegalArgumentException - If value is not a String/Short/Integer/Long, or if an error occurs while converting/casting the object to the return parameter type.
    • evaluateDuration

      public static Long evaluateDuration(String strVal, TimeUnit endUnit)
      Converts a string value representing a unit of time into a Long value.
      Parameters:
      strVal - A String representing a unit of time.
      endUnit - The unit of time into which the string value should be converted.
      Returns:
      Long The value of the string in the desired time unit
    • evaluateToken

      public static String evaluateToken(String strVal)
      Converts a String value into a token value by collapsing whitespace. All whitespace at the beginning and end of the String will be removed, and contiguous sequences of whitespace will be replaced with a single space character.
      Parameters:
      strVal - A String to be trimmed
      Returns:
      String The collapsed String