Inheritance for configurations
As of version v2.8 you can now extends configurations. There is only single inheritance supported.
When to use it
There is often the case that a configuration differs in a few properties when used in different environments or shared between different developers. One way of handling these differences is to put the differences into a properties file and include it in the configuration. You can read more about it here. Another way is to build a common configuration as a base and then extend this configuration and overwrite the special properties. Using this approach it is also possible to build a common configuration that might be used in different projects where only a basic set of properties are the same.How to use it
In order to define that a configuration extends another one you have to define it in the properties tag like this:<?xml version="1.0"?> <properties extends="base"> .... </properties>
This means that this configuration extends the configuration called base. Following the naming convention of jConfig this means that this configuration extends a configuration that it read from a file called base_config.xml.
How does it work
When you try to get a property from a configuration the Configuration will first of all try to find it in the specific category. If the propery is not found then it will look inside the main category (called "default").Still not found then it will call the base configuration and try to get the property from it. If still not found it will return the default value.
Circular dependencies
Using inheritance can lead to an circular dependeny. For example if config A would extend config B and config B would extend config A. The AbstractConfigParser takes care of this and will check if there is such a dependency before the base configuration name is set. If there is such a case the inheritance will not take effect. You will get an error reporting this when this happens.One short example
First we create a file called base_config.xml:
<?xml version="1.0"?>
<properties>
<category name="default">
<server>10.1.100.18</server>
<port>8080</port>
<user_required>true</user_required>
<base_path>/usr/local/data</base_path>
</category>
</properties>
This will be the base for our confguration. Now let us write a file called app_config.xml:
<?xml version="1.0"?>
<properties extends="base">
<category name="default">
<server>10.1.100.22</server>
<base_path>/home/amecky/data</base_path>
</category>
</properties>
When you now get the configuration app like this:
Configuration config = ConfigurationManager.getConfiguration("app");
and you try to get the property server it will return 10.1.100.22 since you have overwritten this property. If you try to get port it will return 8080 since it is defined in the base configuration.