This guide shows how to use EasyConf to solve the simplest configuration problem Its purpose is to get EasyConf running and show how easy it is to use. In the user manual you'll find detailed explanations of all of its features.
You are developing an application and you have to use a value which may change in the future. For example you are developing a calculator and you have to implement a function to convert Euros to Dollars. Currently you know its (for example) 1.2:1 ratio so you hardcode it in a constant:
public static final double EURO_TO_DOLLARS_RATIO = 1.2; public double toDollars(double euros) { return euros/EURO_TO_DOLLARS_RATIO; }
You wish you could make that value configurable so you don't have to recompile when it changes. How would you do it? You could use the Properties class which comes with Java but you'll have to define conventions about where to place the configuration file and which method to use to find it. Furthermore you'll have to deal with the conversion of the value to a double. It's not that much work, but we programmers are lazy.
Here is how you would do it with EasyConf. First change the above lines with:
public static final double EURO_TO_DOLLARS_RATIO = EasyConf. getConfiguration("calculator").getProperties(). getDouble("euro-to-dollars"); public double toDollars(double euros) { return euros/EURO_TO_DOLLARS_RATIO; }
Then create a file called calculator.properties and place it anywhere that will end up being in the classpath of the application. This file should have the following content:
euro-to-dollars=1.2
And that's it!
In this simple case EasyConf provided a few simple conveniences:
Later on, when developing more functionalities of the calculator you find more places where you could take advantage of your newly created configuration file. In this case you need a list of items and a boolean value. With a small refactoring you get:
public static final double EURO_TO_DOLLARS_RATIO = getProperties().getDouble("euro-to-dollars"); public static final String[] SUPPORTED_CURRENCIES = getProperties().getStringArray("supported-currencies"); public static final String[] TRUSTED_CURRENCIES = getProperties().getStringArray("trusted-currencies"); public static final boolean CONVERSION_ALLOWED = getProperties().getBoolean("conversion-allowed"); private static ComponentProperties getProperties(double euros) { return Easyconf.getConfiguration("calculator").getProperties(); }
And add the new properties to calculator.properties:
euro-to-dollars=1.2 supported-currencies=${trusted-currencies},pound trusted-currencies=dollar,euro conversion-allowed=true
This example shows a new feature of EasyConf (inherited from Commons Configuration in fact): the ability to use variables inside the property file. It also shows how handy it is to have automatic conversion of properties to different types. Check the complete list of supported types.
An ASP or Application Service Provider offers applications to several customers from a single installation. In this environment a single running application may need to have different configurations depending on the customer that is accessing the application. EasyConf is prepared to configure applications which are developed to work in this environment.
In this situation the application must ask EasyConf for the configuration providing both the component name and a company id. The company id will represent the unique customer id which is currently accesing the application:
Easyconf.getConfiguration("companyA", "calculator").getProperties();
When used this way EasyConf allows to have configuration files specific to each company which override the default configuration. To know how to achieve this read the ASP model section of the documentation.
As your configuration needs grow you'll find other useful features in EasyConf. For example, it is possible to complement or substitute the properties file with an XML file. EasyConf allows any arbitrary XML vocabulary and converts it to Java POJOs (using Digester).
In the manual you'll also find how EasyConf supports changing the configuration for different environments. For example if you need to use some configuration in the local environment and a different one for the production environment. EasyConf supports an unlimitted number of environments.
Another interesting feature is to configure a set of components when they are aggregated to form a more complex application. For example if you deploy several portlets which use EasyConf in a portal you can change the default property values of any or all of the portlets in a single property files.
Follow on by reading the user manual.