Using a Configuration Factory
This section explains how a
The configuration definition file
When a single configuration file is the only
source of configuration data it is very simple to
load it using a
<?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <properties fileName="usergui.properties"/> </configuration>
Definition files for
For this example we store the definition file for
Setting up a ConfigurationFactory
Now we have to create a ConfigurationFactory factory = new ConfigurationFactory(); URL configURL = new File("config.xml").toURL(); factory.setConfigurationFileName(configURL.toString()); Configuration config = factory.getConfiguration(); As this code fragment shows the file name passed to the factory can be a full URL. This is also the recommended way of specifying the file because it provides the greatest flexibility and a consistent way of handling relative file names found in the definition file. Here we assumed the configuration definition file to be located in the current directory. It is also possible (and probably a better approach) to load the file from the class path. This could be done as follows: ConfigurationFactory factory = new ConfigurationFactory(); URL configURL = getClass().getResource("/config.xml"); factory.setConfigurationURL(configURL); Configuration config = factory.getConfiguration(); Accessing properties
Whatever way we used to load the configuration factory, we
should now have a All of these methods have in common that they expect a property key as argument. Here the name of the searched property must be provided in exact the same way as it is contained in the properties file. To obtain the value of the background color property that is defined in the properties file shown earlier the following code fragment can be used: String backColor = config.getString("color.background"); Multiple configuration sources
Using A XML configuration fileMany applications use the popular XML format for storing configuration information. So it is no wonder that Configuration also supports this type of configuration sources. In general each XML document can be used to define configuration settings. We start here with a rather simple one: <?xml version="1.0" encoding="ISO-8859-1" ?> <gui-definition> <colors> <background>#808080</background> <text>#000000</text> <header>#008000</header> <link normal="#000080" visited="#800080"/> </colors> <rowsPerPage>15</rowsPerPage> </gui-definition>
(As becomes obvious, this tutorial does not bother with good
design of XML documents, the example file should rather
demonstrate the different ways of accessing properties.)
This XML document should be stored under the name
Overriding properties
To make this XML document part of our global configuration we
have to modify our configuration definition file to also include
the new file. For XML documents the element <?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <properties fileName="usergui.properties"/> <xml fileName="gui.xml"/> </configuration>
The code for setting up the String backColor = config.getString("color.background"); String textColor = config.getString("color.text"); String linkNormal = config.getString("color.link[@normal]"); int rowsPerPage = config.getInt("rowsPerPage"); This listing demonstrates some important points of constructing keys for accessing properties load from XML documents:
There is one problem with the example code fragement: It queries
the value of the
The answer is that the configuration sources are searched in the
order they are defined in the configuration definition file.
Here the properties file is included first, then comes the XML
file. Because the
It might not be obvious why it makes sense to define the value
of one and the same property in multiple configuration sources.
But consider the following scenario: An application comes with
a set of default properties and allows the user to override some
or all of them. This can now easy be realized by saving the
user's settings in a file and the default settings in another.
Then in the configuration definition file the file with the
user settings is included first and after that the file with the
default values. The application code that queries these
settings need not be aware whether a property was overriden by
the user. The |