com.ibm.wsspi.kernel.service.utils
Class MetatypeUtils

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

public class MetatypeUtils
extends java.lang.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
MetatypeUtils()
           
 
Method Summary
static java.lang.Long evaluateDuration(java.lang.String strVal, java.util.concurrent.TimeUnit endUnit)
          Converts a string value representing a unit of time into a Long value.
static java.lang.String evaluateToken(java.lang.String strVal)
          Converts a String value into a token value by collapsing whitespace.
static boolean parseBoolean(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.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(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.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(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.Object obj, long defaultValue, java.util.concurrent.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(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.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(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.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(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.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 java.lang.String[] parseStringArray(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.Object obj, java.lang.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.
static java.util.Collection<java.lang.String> parseStringCollection(java.lang.Object configAlias, java.lang.String propertyKey, java.lang.Object obj, java.util.Collection<java.lang.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 Detail

MetatypeUtils

public MetatypeUtils()
Method Detail

parseBoolean

public static boolean parseBoolean(java.lang.Object configAlias,
                                   java.lang.String propertyKey,
                                   java.lang.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:
java.lang.IllegalArgumentException - If value is not a String/Boolean, or if the String boolean is not "true" or "false" (ignoring case)

parseStringArray

public static java.lang.String[] parseStringArray(java.lang.Object configAlias,
                                                  java.lang.String propertyKey,
                                                  java.lang.Object obj,
                                                  java.lang.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:
java.lang.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 java.util.Collection<java.lang.String> parseStringCollection(java.lang.Object configAlias,
                                                                           java.lang.String propertyKey,
                                                                           java.lang.Object obj,
                                                                           java.util.Collection<java.lang.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:
java.lang.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(java.lang.Object configAlias,
                             java.lang.String propertyKey,
                             java.lang.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:
java.lang.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(java.lang.Object configAlias,
                               java.lang.String propertyKey,
                               java.lang.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:
java.lang.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(java.lang.Object configAlias,
                                      java.lang.String propertyKey,
                                      java.lang.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:
java.lang.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(java.lang.Object configAlias,
                                 java.lang.String propertyKey,
                                 java.lang.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:
java.lang.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(java.lang.Object configAlias,
                                 java.lang.String propertyKey,
                                 java.lang.Object obj,
                                 long defaultValue,
                                 java.util.concurrent.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:
java.lang.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 java.lang.Long evaluateDuration(java.lang.String strVal,
                                              java.util.concurrent.TimeUnit endUnit)
Converts a string value representing a unit of time into a Long value.

Parameters:
strVal - A String representing a unit of time.
unit - The unit of time that the string value should be converted into
Returns:
Long The value of the string in the desired time unit

evaluateToken

public static java.lang.String evaluateToken(java.lang.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