Variables
We have defined a variable in our configuration and now it is time to explain it. In order to ease the usage of often repeated phrases inside property values jConfig offers the possibility to define variables. These variables will be replaced with the defined values when you call getProperty(....).
You need to define the variables inside a variables block at the beginning of the configuration like this:
.... <variables> <variable name="upload_dir" value="/tmp/data/uploads"/> </variables> ....
In order to use a variable inside a property value you need to use ${var_name}.
Example:
<property name="home.dir" value="${upload_dir}/home"/>
Now if we take the configuration from the last step here is a simple java application that will write out the value of a property that contains a variable:
import org.jconfig.*;
public class VariableDemo {
private static final Configuration configuration =
ConfigurationManager.getConfiguration();
public static void main(String[] args) {
String uploadDir = configuration.getProperty("upload_dir");
System.out.println("upload_dir:"+uploadDir);
}
}
This will write out:
uploadDir:/home/foo/data
System properties
Since the latest release v2.3 the VariableManager can translate java system properties. These system properties are set by the JVM and are mostly platform dependent. The temp-directory for example differs for every operaring system. In order to build a platform independent configuration you can use these system properties. You need to put the sytem: keyword in front of the property name:
<property name="tmpdir" value="${system:java.io.tmpdir}"/>
After the keyword you have to put the name of the system propery. This example will set the tmpdir to the OS specific temp-directory.You can use any system property.
Environment variables
Another way for writing platform indenpendet applications is the use of environment variables. This is an usual at least under Linux/Unix. You can set a home variable to your application. When you want to use an environment variable you have to put the env: keyword in front of the name. Here is a short example:
<property name="app_home" value="${env:MY_APP_HOME}"/>
This MYAPPHOME can be either "/usr/local/myapp" under Linux or "c:\myapp" under Windows. All you have to do is define such an environment variable and you can move your applications between different OS.