Using ConfigurationOne of the strength of Commons Configuration is its ability to mix configurations from heterogeneous sources, this section will introduce you to the different configurations available and will show you how to combine them. Configuration SourcesCurrently there are quite a number of different sources of Configuration objects. But, by just using a Configuration object versus a specific type like XMLConfiguration or JNDIConfiguration, you are sheltered from the mechanics of actually retrieving the configuration values. These various sources include:
Mixing Configuration Sources
Often you want to provide a base set of configuration values, but allow the user to easily
override them for their specific environment. Well one way is to hard code the default
values into your code, and have then provide a property file that overrides this. However,
this is a very rigid way of doing things. Instead, with the CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new PropertiesConfiguration("application.properties")); or via the ConfigurationFactory factory = new ConfigurationFactory("config.xml"); Configuration config = factory.getConfiguration();
The <?xml version="1.0" encoding="ISO-8859-1" ?> <configuration> <system/> <properties fileName="application.properties"/> </configuration>
What this says is that we are loading up all system properties, as well as the properties
file Configuration DetailsConfiguration is done by taking the configuration descriptor XML file and parsing the individual configurations. Make sure to include the various dependencies required for each type of configuration! Classic Properties File<properties fileName="conf/test.properties"/> This configuration file is very simple. You just need to specify the path to the property file. XML Properties File<xml fileName="conf/test.xml"/> The configuration is very similar to the classic properties file. However, the xml file must be in a specific format. Currently there is no DTD. <baseElement> <element>value</element> <element2> <subelement> <subsubelement>I'm complex!</subsubelement> </subelement> </element2> <test> <short>8</short> </test> </baseElement>
In the above example, the root element is ignored. So to get the value "8", you would
request from your Configuration object the key " JNDI Environment Properties<jndi prefix="java:comp/env"/>
This configuration is very useful for setting environment specific settings like mail
servers! The prefix tells the <env-entry> <env-entry-name>smtp</env-entry-name> <env-entry-value>127.0.0.1</env-entry-value> <env-entry-type>java.lang.String</env-entry-type> </env-entry> <env-entry> <env-entry-name>test/short</env-entry-name> <env-entry-value>80</env-entry-value> <env-entry-type>java.lang.Short</env-entry-type> </env-entry>
Note! If you have a property called " |