The first steps
Getting started will guide you through the very basic usage of jConfig.
What you need
In order to use jConfig make sure you have the following jar files in the classpath:
- jconfig.jar
- jaxp.jar (optional)
- crimson.jar or xerces.jar (optional)
The first configuration
Now we will write an XML configuration. Save the following to a file called "config.xml" and make sure that this file is also in the classpath. Here is the XML:
<?xml version="1.0" encoding="iso-8859-1" ?>
<properties>
<variables>
<variable name="my.path" value="/home/foo"/>
</variables>
<category name="general">
<property name="upload_dir" value="${my.path}/data"/>
<property name="NewsCounter" value="10"/>
<property name="showNews" value="true"/>
<property name="MyProp" value="Hello world"/>
</category>
<category name="JDBC">
<property name="URL" value="jdbc:mysql://localhost/iportal"/>
<property name="DRIVER" value="org.gjt.mm.mysql.Driver"/>
<property name="PWD" value="pwd"/>
<property name="USER" value="user"/>
</category>
</properties>
The root of the configuration is "properties". The next level can be a block of variables or categories. There is no limitation on the number of categories.
So the structure is to have categories and properties inside the categories. The default configuration does not cover nested categories. Using such a configuration is described later.
The application
Now we will write the "Hello world" application:
import org.jconfig.*;
public class ConfigDemo {
private static final Configuration configuration = ConfigurationManager.getConfiguration();
public static void main(String[] args) {
String myProp = configuration.getProperty("MyProp");
System.out.println("MyProp:"+myProp);
}
}
This little demo will write out "Hello world". Actually not really surprisingly.
How does it work internally
What happens behind the scenes is that the ConfigurationManager tries to find the file config.xml in the classpath and if it is found it will read in your configuration automatically. You do not have to load the configuration. When you call the
getConfiguration()method then the ConfigurationManager returns the default configuration which in our case is the configuration inside our config.xml.
Getting the values of the properties
When you use the
getProperty(String name)or
getProperty(String name,String defaultValue)methods then jConfig will return the property value from the default category. In order to get the property value from a specific category we use the
getProperty(String name,String defaultValue,String category).
Here is an example that shows the usage of the different getProperty methods:
public static void main(String[] arg) {
String myProp = configuration.getProperty("MyProp");
System.out.println("MyProp:"+myProp);
String jdbcUser = configuration.getProperty("USER",null,"JDBC");
System.out.println("jdbcUser:"+jdbcUser);
int newsCounter = configuration.getIntProperty("NewsCounter",-1);
if ( newsCounter == 10 ) {
System.out.println("We have found the correct value");
}
boolean showNews = configuration.getBooleanProperty("showNews",false);
if ( showNews ) {
System.out.println("We have to show the news");
}
}