Quick start guideThis document is a short introduction into the basic use cases of Commons Configuration for the impatient. Later chapters of this user's guide explain the concepts presented here in more detail. Reading a properties file
Configuration information is frequently stored in properties files.
Consider the following simple file that defines some properties related
to accessing a database. We assume that it is stored as
database.host = db.acme.com database.port = 8199 database.user = admin database.password = ??? database.timeout = 60000
The easiest way to read this file is via the
Configurations configs = new Configurations(); try { Configuration config = configs.properties(new File("config.properties")); // access configuration properties ... } catch (ConfigurationException cex) { // Something went wrong } Accessing properties
The String dbHost = config.getString("database.host"); int dbPort = config.getInt("database.port"); String dbUser = config.getString("database.user"); String dbPassword = config.getString("database.password", "secret"); // provide a default long dbTimeout = config.getLong("database.timeout"); Note that the keys passed to the get methods match the keys contained in the properties file. If a key cannot be resolved, the default behavior of a configuration is to return null. (Methods that return a primitive type throw an exception because in this case there is no null value.) It is possible to provide a default value which is used when the key cannot be found. Reading an XML file
XML is also a suitable format for storing configuration information,
especially if the data becomes more complex. For instance, lists of
values can be stored in a natural way by just repeating tags. The
example file for this section defines some directory paths that are to be
processed by an application. It is named <?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <processing stage="qa"> <paths> <path>/data/path1</path> <path>/data/otherpath</path> <path>/var/log</path> </paths> </processing> </configuration>
Reading this file works analogously to reading a properties file. Again a
Configurations configs = new Configurations(); try { XMLConfiguration config = configs.xml("paths.xml"); // access configuration properties ... } catch (ConfigurationException cex) { // Something went wrong }
The Accessing properties from XMLAccessing properties in a XML configuration (or any other hierarchical configuration) supports the same query methods as for regular configurations. There are some additional facilities that take the hierarchical nature of these sources into account. The properties in the example configuration can be read in the following way: String stage = config.getString("processing[@stage]"); List<String> paths = config.getList(String.class, "processing.paths.path");
The keys for properties are generated by concatenating the possibly nested
tag names in the XML document (ignoring the root element). For attributes,
there is a special syntax as shown for the stage property.
Because the path element appears multiple times it actually
defines a list. With the Hierarchical configurations support an advanced syntax for keys that allows a navigation to a specific element in the source document. This is achieved by adding numeric indices in parentheses after the single key parts. For instance, in order to reference the second path element in the list, the following key can be used (indices are 0-based): String secondPath = config.getString("processing.paths.path(1)"); For elements which are not repeated such indices can be dropped. It is also possible to set an alternative expression engine - the component that evaluates and interprets configuration keys. There is an implementation available which can deal with XPath expressions. Refer to Expression engines for further details. Updating a configuration
The config.setProperty("database.port", 8200); config.addProperty("database.type", "production");
Saving a configuration
After a configuration has been manipulated, it should probably be saved
again to make the changes persistent. Otherwise, the changes are only in
memory. If configurations are to be changed, it is preferable to obtain
them via a different mechanism: a configuration builder.
Builders are the most powerful and flexible way to construct
configurations. They support many settings that impact the way the
configuration data is loaded and the resulting configuration object
behaves. Builders for file-based configurations also offer a
Configurations configs = new Configurations(); try { // obtain the configuration FileBasedConfigurationBuilder<XMLConfiguration> builder = configs.xmlBuilder("paths.xml"); XMLConfiguration config = builder.getConfiguration(); // update property config.addProperty("newProperty", "newValue"); // save configuration builder.save(); } catch (ConfigurationException cex) { // Something went wrong } |