Migration Guide to Version 2.0This document aims at supporting with the migration from version 1.x of Commons Configuration to version 2.0. Target audience are users of an older version who want to upgrade. The document describes the areas in which major changes have been implemented; here problems are likely to be encountered during migration. It has the following content:
IntroductionVersion 2.0 of Commons Configuration is the result of a major redesign of this library. While version 1.x has become pretty stable and does what it is expected to do, there are some limitations and design flaws which could not be fixed in a painless and compatible way. In order to overcome these restrictions, version 2.0 has applied significant changes to some of the problematic concepts or even replaced them by alternative approaches. This has lead to an ambivalent situation: On one hand, you will recognize many similarities to the old version - classes with familiar names that continue to do what they have done before. On the other hand, completely new approaches have been introduced; in the affected areas Commons Configuration 2.0 will look like a completely new product rather than a plain upgrade. Because of such major changes, you cannot simply drop the new jar in your classpath and expect that everything continues to work. In the remaining part of this document the most important changes are described. This should give you an impression about the effort required to integrate the new version with your application. Also note that the user's guide has been fully reworked to cover all the new features and concepts offered by Commons Configuration 2.0. Because of that, this document will not describe interfaces or classes in detail, but simply refer to the corresponding sections of the user guide. Structural Changes
The most obvious change you will notice at the very beginning is that
the root package was renamed to For the same reason the Maven coordinates have been changed. Use the following dependency declaration in your pom: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-configuration2</artifactId> <version>2.7</version> </dependency> So for Maven version 2.0 is a completely different artifact. This allows a peaceful coexistence of Commons Configuration 1.x and 2.0 in the dependency set of a project. Accessing Configuration Properties
The good news is that there are only minor changes in the central
What has changed is the default implementation of
List handling in
Version 2.0 also has changes related to
Hierarchical
Configurations.
Creating Configurations
A major difference between Commons Configuration 1.x and 2.0 is
the way configuration objects are created, initialized, and managed. In
version 1.x configurations are created directly using their constructor.
Especially for file-based configuration implementations - like
// Version 1.x: Initializing a properties configuration PropertiesConfiguration config = new PropertiesConfiguration("myconfig.properties"); config.setThrowExceptionOnMissing(true); config.setIncludesAllowed(false); config.setListDelimiter(';'); While this code is easy to write, there are some non-obvious problems:
To overcome these problems, Commons Configuration uses a different approach for the creation of configuration objects based on configuration builders. The basic idea is that a configuration builder is created and initialized with all parameters to be applied to the new configuration object. When the configuration instance is queried from its builder it is guaranteed that it has been fully initialized in the correct order. In addition, access to configuration builders is thread-safe. Configuration builders offer a fluent API for setting the initialization parameters for the configuration to be created. The example above would become something like the following in version 2.0: FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class) .configure(new Parameters().properties() .setFileName("myconfig.properties") .setThrowExceptionOnMissing(true) .setListDelimiterHandler(new DefaultListDelimiterHandler(';')) .setIncludesAllowed(false)); PropertiesConfiguration config = builder.getConfiguration();
Builders also offer an increased flexibility regarding the management of
configuration objects. While in version 1.x of Commons Configuration
typically Working with builders may seem a bit verbose at first. There are some ways to simplify their usage. Be sure to read the section Making it easier which describes some useful short cuts. It is also possible to define default values for initialization parameters. This allows simplifying of builder configurations and can establish application-global standard settings for configuration objects. This mechanism is described in Default Initialization Parameters. ReloadingSupport for reloading of externally changed configuration sources was limited in Commons Configuration 1.x. There was a reloading strategy implementation that was triggered on each access to a configuration property and checked whether an underlying file was changed in the meantime. If this was the case, the configuration was automatically reloaded. CONFIGURATION-520 contains a discussion about the problems and limitations of this approach. In version 2.0 reloading functionality has been completely redesigned. The new approaches are described in the chapter Automatic Reloading of Configuration Sources of the user's guide. In a nutshell, configuration builders play an important role here. There are builder implementations available which can be configured to monitor external configuration sources in a pretty generic way. When a change is detected, the builder resets its managed configuration so that the next time it is accessed a new instance is created. In addition, an event can be generated notifying the application that new configuration information might be available. The whole mechanism can be setup to perform reloading checks periodically and automatically in a background thread.
The Combining Configuration SourcesIn Commons Configuration 1.x, there were two options for creating a combined configuration out of multiple sources:
The former has been removed. The functionality provided by
In version 1.x In both the old and the version, a XML-based definition file is used to declare the different configuration sources that are to be combined plus some additional settings. The principle structure of this file has not changed - the full description of the new format is available at Configuration definition file reference.
A problem when migrating from A prominent example of bean definitions were reloading strategies assigned to specific configuration sources. As the whole reloading mechanism has changed significantly, such declarations are no longer supported. There is a much simpler replacement: just add the config-reload attribute to a configuration source declaration to enable reloading support for this source.
Another incompatible change is related to the extensibility of the
definition file format. It used to be possible - and still is - to define
custom tags for declaring special configuration sources. This is done by
registering provider objects at the configuration builder. Because the
internal implementation of
A complete description of Concurrency IssuesAn important design goal of Commons Configuration 2.0 was to improve the behavior of configuration objects when accessed by multiple threads. In the 1.x series, support for concurrent access to configuration data has grown historically: The original intent was that a configuration object can be read by multiple threads in a safe way, but as soon as one thread modifies the data, the user has to ensure proper synchronization manually. Later on, also due to the reloading implementation, more and more synchronization was added. This even caused performance bottlenecks, for instance as reported in CONFIGURATION-530.
The changes in version 2.0 related to multi-threading include multiple
aspects. The most obvious change is probably that synchronization of
configurations is now much more flexible. A configuration instance is
assigned a
NoOpSynchronizer . This means
that configuration objects are not thread-safe per default! You have to
change the synchronizer in order to make them safe for concurrent access.
This can be done for instance by using a builder which is configured
accordingly.
Talking about builders: This is another concept which supports access to configuration data by multiple threads. Access to a builder is always thread-safe. By shifting the responsibility for reloading operations from the configuration to the builder, the need for intensive locking on each property access could be eliminated.
Hierarchical configurations derived from
There are some other changes on classes with the goal to make them well-behaving citizens in a concurrent environment. This includes:
Please refer to Configurations and Concurrent Access for a full description of this complex topic. Events
Another area in which major changes took place is the support for
event notifications. Commons
Configuration 1.x had two types of event listeners for configuration
update events and error events. Version 2.0 adds some more event sources -
events generated by configuration builders and reloading events. Despite
this increased number of event sources, there is now only a single event
listener interface
(
While the old version used numeric constants to define specific event types,
the new events are classified by instances of the
The most basic use case for event listeners in version 1.x was probably
the registration of a change listener at a single configuration instance.
To achieve an equivalent effect with the new API, one would implement an
event listener and register it for the event type
There is, however, an important difference with the event listener registration: The recommended way is to add the listener to the configuration builder which creates the configuration rather than to the configuration itself. This ensures that registration is done at the correct moment in time and also updated when the builder decides to replace its managed configuration instance. All in all the new event mechanism should be much more flexible and powerful than the old one. |