Managing Your Configuration with JFig, Page 2
For example:
<section name="authentication">
......
<entry key="userid" value="admin" />
......
</section>
......
<section name=" notification ">
......
<entry key="receiverAddress"
value="[authentication]{userid}@mycomp.com" />
</section>
For example:
<entry key="class_Path" value="$CLASSPATH$" />
Putting JFig to Work
Requirements
To run, JFig requires the following .jar files, and their respective paths included in the classpath.
- xerces-1.2.3.jar from Xerces
- log4j-1.2.6.jar from Log4j
- Jakarta-regexp-1.2.jar from Jakarta-regexp
Installing JFig
Installing JFig is a pretty simple task. Just download the JFig source archive (jfig-1.4.1.zip or jfig-1.4.1.jar, lightweight with sizes 3 Mb and 50 Kb respectively, both platform independent), uncompress the archive in the desired location, and include that location's path in the classpath. Downloading jfig-1.4.1.zip is recommended because it also includes javadocs for the JFig API.
The next step (optional) would be integrating JFig with an existing application. As said earlier, at present JFig supports Maven, ANT, Log4j, and Weblogic. JFig provides the MavenFig and AntFig classes bundled with its distribution for Maven and ANT respectively, to perform such integration. Also, the JFig home page lists various techniques used for integration with Log4j, Weblogic, and other tools. Check it out for the latest and most detailed information on this subject.
Running JFig
After installing JFig and writing the necessary configuration files (such as Listings 1 and 2), it is time to write JFig code in the application using it. The API behind JFig tool can be used by the application to access the configuration info inside these files.
In the first class to be executed at application startup, include the following code:
Listing 3: JFig initialization code
try {
JFig.initialize();
} catch (JFigException e) {
e.getMessage();
}
Here, JFig class's method initialize() either initializes JFig's one and only instance (JFig is based on a Singleton pattern) or throws a JFigException. There is another version of this method, initialize(JFigLocator jl), where jl can be either a default or user-defined (extended) implementation of JFigLocator.
After initialization, configuration values can be accessed in the application by using the following JFig methods:
- getValue(String section_name, String key_name): Returns a String value for a key with the given name in the section with the given name. Otherwise, it throws a JFigException.
Usage:String uid = JFig.getInstance().getValue("authentication", "userid"); - getVaue(String section_name, String key_name, String defaultValue): Returns the String value of the given key_name in the given section_name if the corresponding section and key exist. If they don't exist, defaultValue specified (optional) will be returned.
Usage:String uid = JFig.getInstance().getValue("authentication", "userid", "guest"); - setConfigurationValue(String section_name, String key_name, String value): Sets the configuration value; in other words, the value of the given key_name in the given section_name with the given value. Rarely used, because0 configuration values are usually set during the initial parsing of configuration files.
Usage:JFig.setConfigurationValue(("authentication", "userid", "developer"); - reprocessConfiguration(): Reprocesses configuration on a running application and creates a new configuration directory containing all JFig values. Used when a change in the configuration values occurs.
Usage:JFig.getInstance().reprocessConfiguration();
- printConfigurationDictionary(): Prints all JFig values in a JFig dictionary to the console. Used for monitoring purposes.
Usage:JFig.getInstance().printConfigurationDictionary();
Some other useful JFig methods, known as helper methods, are:
- getIntegerValue(String sectionName, String keyName, String defaultValue)
- getFloatValue(String sectionName, String keyName, String defaultValue)
- getBooleanValue(String sectionName, String keyName, String defaultValue)
- getArrayValue(String sectionName, String keyName)
- getEntriesStartingWith(String sectionName, String keyName)
- getValuesStartingWith(String sectionName, String keyValue)
- getSection(String sectionName)
- getSectionAsProperties(String sectionName)
- addConfigEventListener(JFigListener listener)
See JFig javadocs for more methods and details of the whole JFig API.
