The configuration framework allows you to statically define configuration parameters and lists in an XML file. This file is typically interpreted at startup and gives access to a Config instance throughout the life-cycle of the application.
A less known feature is that the Config instance can be modified at runtime and written back to XML with the storeToXml() method.
For example:
Config config = Config.getRepInstance();
config.setParameter("param1", "astring");
config.setParameter("param2", true);
config.addListItem("list1", "item1");
config.addListItem("list1", "item2");
config.storeToXml();
While this is a handy feature, it will not work if the configuration XML file is shipped inside a jar or a war. Therefore, it's now possible to store the configuration to a standard Java preferences node. For more information about the standard Preferences API, please look here
.
Basically it allows data to be stored and retrieved from a backing store on the executing host. This currently defaults to file-system storage. It can either use system-specific or user-specific storage. Storage can be isolated and identified by nodes. These are in fact simple path strings that separate levels by slashes (just as for directories on the file system).
Two special configuration parameters can now be used in the XML file: CONFIG_PREFERENCES_USER and CONFIG_PREFERENCES_SYSTEM, referring respectively to user-specific or system-specific storage. The value of these parameters should be node paths.
For example consider the following rep/config.xml file:
<config>
<param name="CONFIG_PREFERENCES_USER">/myapplication</param>
<param name="L10N_DEFAULT_LANGUAGE">en</param>
</config>
The following Java code allows a user to select a new language and to make it be remembered without modifying any file of the original application.
Config config = Config.getRepInstance();
config.setParameter("L10N_DEFAULT_LANGUAGE", "fr");
config.storeToPreferences();