File-based Configurations
Often configuration properties are stored in files on the user's hard
disk, e.g. in .properties files or as XML documents. Configuration
classes that deal with such properties need to provide typical operations
like loading or saving files. The files to be processed can be specified
in several different flavors like
To provide a consistent way of dealing with configuration files in
Commons Configuration the
In the following sections we take a closer look at the methods of the
Specifying the file
The
While a
ConfigurationException will
be thrown.
Loading
After the file name has been defined using one of the methods mentioned
above, the
The
File-based configurations typically define a set of constructors that
correspond to the various setter methods for defining the data file.
These constructors will set the file and then invoke the Saving
Saving is implemented analogously to loading: There is a no argument
An example for loading, manipulating, and saving a configuration
(based on a PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000"); config.save(); You can also save a copy of the configuration to another file: PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setProperty("colors.background", "#000000"); config.save("usergui.backup.properties); Automatic Saving
If you want to ensure that every modification of a configuration
object is immediately written to disk, you can enable the automatic
saving mode. This is done through the PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setAutoSave(true); config.setProperty("colors.background", "#000000"); // the configuration is saved after this call Be careful with this mode when you have many updates on your configuration. This will lead to many I/O operations, too. Automatic Reloading
A common issue with file-based configurations is to handle the
reloading of the data file when it changes. This is especially important
if you have long running applications and do not want to restart them
when a configuration file was updated. Commons Configuration has the
concept of so called reloading strategies that can be
associated with a file-based configuration. Such a strategy monitors
a configuration file and is able to detect changes. A default reloading
strategy is PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties"); config.setReloadingStrategy(new FileChangedReloadingStrategy());
Managed Reloading
A typical use of this feature is to setup ManagedReloadingStrategy as a JMX MBean. The following code sample uses Springframework MBeanExporter to expose the ManagedReloadingStrategy to the JMX console : <!-- A file based configuration bean --> <bean id="configuration" class="(...).PropertiesConfiguration"> <constructor-arg type="java.net.URL" value="file:${user.home}/custom.properties"/> <property name="reloadingStrategy" ref="reloadingStrategy"/> </bean> <!-- The managed reloading strategy for the configuration bean --> <bean id="reloadingStrategy" class="...ManagedReloadingStrategy"/> <!-- The MBeanExporter that exposes reloadingStrategy to the JMX console --> <bean id="mbeanMetadataExporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="server" ref="mbeanServer"/> <property name="beans"> <map> <entry key="myApp:bean=configuration" value-ref="reloadingStrategy"/> </map> </property> </bean> |