Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is apache common configuration

Can some one throw some light on what is common configuration (apache common configuration)? It will he helpful if some one can explain it with some use-case.

Also any links (other than google) from where I can get some useful information are very much appreciated.

like image 233
Rohit Jain Avatar asked Dec 16 '22 02:12

Rohit Jain


2 Answers

Apache Commons Configuration is most famous for the library's ability for you to work with configuration files, i.e. parse in .properties file.

For example:

color=Green
person=John

This property file may reside in a classpath or in a hard directory. Using Apache Commons Configurations, you may parse in the parse easily, and get to the value represented by its key.

See this quick tutorial.

EDIT

But why configuration or Apache Commons Configurations?

Sometimes, you do not wish to hard code a particular value into the codes that are to be compiled. For example, you may have a application variable BACKGROUND_COLOR, the value of this variable controls the color of the background of your application. How would you store this in your application?

You can do this:

public static final String BACKGROUND_COLOR = "Green";

However, if you want to change the color of the background into "Red", then you would have to change the above code, recompile it into:

public static final String BACKGROUND_COLOR = "Red";

What if you do not want to change your codes, recompile to change the background of your application? Yes, you could store this value into a text file called system.properties, or any name and extension actually.

For example, you can store it in system.properties:

background_color=Green

But how do you read this text file? (which is saves in properties format, key=value) Would you want to go into low level File and IO in order to read these values? Chances are you don't, you would want a mature and established library to do so for you.

For this purpose, you may use the Apache Commons Configurations. This library is primed for reading configurations such as a properties file.

Using Apache Commons Configurations, here are the codes to read the above properties file and extract the value of the key background_color.

public static void main(String [] args){
  Configuration config = new PropertiesConfiguration("system.properties");
  String backColor = config.getString("background_color");
  System.out.println(backColor); // this will give you green
}

Hope this helps to your understanding. :)

like image 132
Oh Chin Boon Avatar answered Dec 29 '22 10:12

Oh Chin Boon


Apache Commons Configuration is a java library that simplifies managing application configuration properties. It allows you to collect properties from different configuration sources like properties files, XML files, Java System properties, Environemnt variables etc. It also allows you to override properties by settings preference chain of configuration sources. Refer this article.

like image 27
Drona Avatar answered Dec 29 '22 10:12

Drona