Configuration

public class Configuration extends Object

Static factory for handling connector configurations.

The caller can use one of the available factory methods to create an instance of ConfigValue. If initConfig(String[]) is not yet executed, this factory holds a references to a registered ConfigValue instances to initialize later. Once the Configuration is initialized, each new ConfigValue instance is initialized immediately according to the loaded configuration values. Use Configuration.ResetConfigRule in unit tests to reset the Configuration object. Configuration values read from properties file are trimmed before parsing.

For example, caller could use one of the factory methods to define ConfigValue.

ConfigValue<Boolean> booleanParam = Configuration.getBoolean("config.key1", false);
 ConfigValue<Integer> integerParam = Configuration.getInteger("config.key2", 10);
 ConfigValue<String> stringParam = Configuration.getString("required.config.key3", null);
 Parser<URL> urlParser =
     value -> {
       try {
         return new URL(value);
       } catch (MalformedURLException e) {
         throw new InvalidConfigurationException(e);
       }
     };
 ConfigValue<URL> configuredUrl =
     Configuration.getValue("required.config.key4", null, urlParser);
 

Nested Class Summary

interface Configuration.Parser<T> General purpose ConfigValue parser. 
class Configuration.ResetConfigRule TestRule to reset static Configuration object for unit tests. 
class Configuration.SetupConfigRule TestRule to initialize static Configuration object for unit tests. 

Field Summary

public static final Parser<Boolean> BOOLEAN_PARSER Configuration.Parser for parsing string values as boolean.
public static final Parser<Double> DOUBLE_PARSER Configuration.Parser for parsing string values as double.
public static final Parser<Integer> INTEGER_PARSER Configuration.Parser for parsing string values as integer.
public static final Parser<String> STRING_PARSER Configuration.Parser for parsing string values.

Public Method Summary

static void
checkConfiguration(boolean condition, String errorFormat, Object... errorArgs)
Checks for a configuration error.
static void
checkConfiguration(boolean condition, String errorMessage)
Checks for a configuration error.
static ConfigValue<Boolean>
getBoolean(String configKey, Boolean defaultValue)
Get configuration values parsed as boolean for supplied configuration key.
static Properties
getConfig()
Retrieves all of the configuration properties.
static ConfigValue<Integer>
getInteger(String configKey, Integer defaultValue)
Get configuration values parsed as integer for supplied configuration key.
static <T> ConfigValue<List<T>>
getMultiValue(String configKey, List<T> defaultValues, Parser<T> parser)
Allows for retrieving a parameter value that consists of a comma delimited list.
static <T> ConfigValue<List<T>>
getMultiValue(String configKey, List<T> defaultValues, Parser<T> parser, String delimiter)
Allows for retrieving a parameter value that consists of a delimited list.
static <T> ConfigValue<T>
getOverriden(String configKey, ConfigValue<T> defaultValue)
Allows for chaining default configuration values.
static ConfigValue<String>
getString(String configKey, String defaultValue)
Get configuration values parsed as string for supplied configuration key.
static <T> ConfigValue<T>
getValue(String configKey, T defaultValue, Parser<T> parser)
Allows for creating custom type ConfigValue instances.
static void
initConfig(String[] args)
Initializes the Configuration instance using the properties file specified in the command line arguments.
synchronized static void
initConfig(Properties config)
Initializes the Configuration instance using the provided Properties.
static boolean
isInitialized()
Checks whether the Configuration is initialized.

Inherited Method Summary

Fields

public static final Parser<Boolean> BOOLEAN_PARSER

Configuration.Parser for parsing string values as boolean. This parser supports only 'true' and 'false' as valid values, ignoring case.

public static final Parser<Double> DOUBLE_PARSER

Configuration.Parser for parsing string values as double.

public static final Parser<Integer> INTEGER_PARSER

Configuration.Parser for parsing string values as integer.

public static final Parser<String> STRING_PARSER

Configuration.Parser for parsing string values.

Public Methods

public static void checkConfiguration (boolean condition, String errorFormat, Object... errorArgs)

Checks for a configuration error.

This allows the connector to throw a InvalidConfigurationException instead of a checkArgument() method's IllegalArgumentException. This prevents the SDK start up code from performing retries when a quick exit is appropriate.

Parameters
condition the valid condition to test
errorFormat the format string for the thrown exception message when the condition is not met
errorArgs the arguments for the errorFormat

public static void checkConfiguration (boolean condition, String errorMessage)

Checks for a configuration error.

This allows the connector to throw a InvalidConfigurationException instead of a checkArgument() method's IllegalArgumentException. This prevents the SDK start up code from performing retries when a quick exit is appropriate.

Parameters
condition the valid condition to test
errorMessage the thrown exception message when the condition is not met

public static ConfigValue<Boolean> getBoolean (String configKey, Boolean defaultValue)

Get configuration values parsed as boolean for supplied configuration key.

Parameters
configKey configuration key to read value for
defaultValue default value to return if configuration key value doesn't exist.

public static Properties getConfig ()

Retrieves all of the configuration properties.

Caller should check isInitialized() before calling.

Returns
  • configuration properties

public static ConfigValue<Integer> getInteger (String configKey, Integer defaultValue)

Get configuration values parsed as integer for supplied configuration key.

Parameters
configKey configuration key to read value for
defaultValue default value to return if configuration key value doesn't exist.

public static ConfigValue<List<T>> getMultiValue (String configKey, List<T> defaultValues, Parser<T> parser)

Allows for retrieving a parameter value that consists of a comma delimited list.

The most common usage is for a configuration parameter that is a list of comma delimited strings. For example:

// list of strings
   ConfigValue<List<String>> listParam =
       Configuration.getMultiValue("config.stringList", null, Configuration.STRING_PARSER); 
 

Parameters
configKey configuration file parameter key
defaultValues list of default key values
parser specific parser to support the list object's data type
Returns
  • the list type object

public static ConfigValue<List<T>> getMultiValue (String configKey, List<T> defaultValues, Parser<T> parser, String delimiter)

Allows for retrieving a parameter value that consists of a delimited list. For a comma-delimited list, see getMultiValue.

The most common usage is for a configuration parameter that is a list of delimited strings. For example:

// list of strings
   ConfigValue<List<String>> listParam =
       Configuration.getMultiValue(
           "config.stringList", null, Configuration.STRING_PARSER, ";"); 
 

Parameters
configKey configuration file parameter key
defaultValues list of default key values
parser specific parser to support the list object's data type
delimiter the character between multiple values
Returns
  • the list type object

public static ConfigValue<T> getOverriden (String configKey, ConfigValue<T> defaultValue)

Allows for chaining default configuration values.

This is used to allow fetching a configuration value while using a previously defined configuration value as a default value. That default can also be the result of a another parameter, and so on.

For example:

// set configDefaultValue to the configuration file value, or 10 if not defined
   ConfigValue<Integer> configDefaultValue = Configuration.getInteger("connector.count", 10);

   // set configSpecificValue to the configuration file value or configDefaultValue
   ConfigValue<Integer> configSpecificValue =
       Configuration.getOverriden("specific.connector.count", configDefaultValue);

   // at this point configSpecificValue is set to:
   //    1) the configuration value for "specific.connector.count"; if not defined then:
   //    2) the configuration value for "connector.count"; if that is not defined then:
   //    3) 10, which is the default value for "connector.count".
  

Parameters
configKey
defaultValue

public static ConfigValue<String> getString (String configKey, String defaultValue)

Get configuration values parsed as string for supplied configuration key.

Parameters
configKey configuration key to read value for
defaultValue default value to return if configuration key value doesn't exist.

public static ConfigValue<T> getValue (String configKey, T defaultValue, Parser<T> parser)

Allows for creating custom type ConfigValue instances.

For example:

// custom parser for URL values
   Parser<URL> urlParser =
       value -> {
         try {
           return new URL(value);
         } catch (MalformedURLException e) {
           throw new InvalidConfigurationException(e);
         }
       };
   ConfigValue<URL> configuredUrl = Configuration.getValue("config.url", null, urlParser);
 

Parameters
configKey configuration file parameter key
defaultValue default key value
parser custom parser to support the data type
Returns
  • the custom type object

public static void initConfig (String[] args)

Initializes the Configuration instance using the properties file specified in the command line arguments. Properties can also be specified on the command line as "-DpropertyName=propertyValue". A property given on the command line will override the same property from the config file.

Caller should check isInitialized() before calling initConfig(String[]).

Parameters
args command line arguments
Throws
IOException

public static synchronized void initConfig (Properties config)

Initializes the Configuration instance using the provided Properties.

Caller should check isInitialized() before calling initConfig(String[])

Parameters
config Properties properties to initialize

public static boolean isInitialized ()

Checks whether the Configuration is initialized.

Returns
  • true if configuration has been initialized. false otherwise.