Skip to content

Configuration

Ciaran O'Reilly edited this page Jun 3, 2015 · 2 revisions

Configuration

The Configuration utility is a simple API/Convention for retrieving typed configuration information from within your application. Defaults are set programmatically, which can be overridden by setting system properties corresponding to the property at run time, e.g.:

java <main class> -Dconfig.property.name=<a value>

These can again be overridden with a configuration file expressed in java.util.prefs.Preferences.exportNode() format:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
  <root type="user">
    <map>
      <entry key="key1" value="value1"/>
      <entry key="key2" value="value2"/>
      <entry key="key3" value="value3"/>
    </map>
  </root>
</preferences>

Configurations are initialized from configuration files based on their name in the following order:

  • The configuration utility tries to find a file called aic-smf-config-local.xml in the classpath.
  • If no such file is found, it tries to find a file called aic-smf-config-test.xml in the classpath.
  • If no such file is found, it tries to find a file called aic-smf-config.xml in the classpath.
  • If a configuration file is found, only the properties defined in the file will override the programmatically and system property defined values.
  • If no configuration file is found, only the programmatic defaults and system property overrides are used to initialize properties.

Configurations Per-Thread Support

To better enable concurrent execution, configuration information is maintained on a Thread by Thread basis. On initialization a root set of configuration information is set up as described previously and when calls are made to the configuration API from a thread for the first time its configuration settings are setup based on this root set. In addition, it is possible to pass configuration information between threads in a parent child relationship by calling:

Configuration.inheritConfiguration(parentThread, childThread)

This permits configurations specific to a parent Thread to be inherited by a child Thread and then adapted as needed in the child thread without changing the parent threads configuration settings. This can be useful when running a service on a per-thread basis, where each is configured differently based on user defined preferences.

Usage Convention

This class should be sub-classed in each project, where the extended class programmatically lists the configuration properties (and their default values) specific to it, e.g.:

public class MyProjectConfiguration extends Configuration {
    public static final String  KEY_PROPERTY_ONE      = "my.project.property.1";
    public static final Integer DEFAULT_PROPERTY_ONE  = new Integer(0);
    ...
    public static int getProperyOne() {
        int result = getInt(KEY_PROPERTY_ONE, DEFAULT_PROPERTY_ONE);
        return result;
    }
    ...
}

AICUtilConfiguration is an example of this convention being used for this project.