I am working on a product suite which has 4 products. Right now, all of the configuration data is either in the XML or properties files.This approach is not maintainable as
You're almost there... I would keep your same approach and pull in the correct configuration file for the instance of the application that is running by a method similar to either of the following:
Name all of your configuration files differently and have your application pull them in by some unique criteria (username, hostname, etc.):
Keep them outside the codebase in a location based on an environment variable that the application assumes exists:
I've even used a combination of these approaches on the same project (#1 for build process configurations and #2 for runtime configurations).
If your applications work with a database, you can create a "configuration" table as follows:
create table configuration (mode char(3), key varchar(255), value varchar(1023));
You would initialize it using an init script, say init.sql with contents along the lines of:
insert into configuration values ('pro', 'param1', 'value1'); -- production
insert into configuration values ('dev', 'param1', 'value1'); -- development
insert into configuration values ('tst', 'param1', 'value1'); -- testing
...
The benefits of this approach are as follows:
Config is a configuration file management tool. You can create configuration that is common on all environments, or you can create an environment specific configuration. You can keep using your XML and properties files, and let Config maintain the differences in environment. You can think of Config as your centralized database and it can output the configuration file in the format that you want. Whenever you want your configuration file, just deploy (push or pull) it from Config to your desired location. Note that I'm part of the Config team.
Environment variables are just about the easiest way to go. Set them as you would any other time, access them w/ System.getenv("...")
Using commons-configuration you have a unified API for accessing the properties, no matter how they are represented - .properties, xml, JNDI, etc. For example:
config.properties
:
jdbcHost=192.168.12.35
jdbcUsername=dbuser
jdbcPassword=pass
config.xml
:
<config>
<jdbcHost>192.168.12.35</jdbcHost>
<jdbcUsername>dbuser</jdbcUsername>
<jdbcPassword>pass</jdbcPassword>
</config>
in both cases they will be accessible with something like:
String host = config.getString("jdbcHost");
There are plenty of different strategies. All of them are good and depends on what suit you best.