January 26, 2021
Hot Topics:

Managing Your Configuration with JFig

  • By Mugdha Vairagade
  • Send Email »
  • More Articles »
  • JFig stores all configuration files in a single common repository, ordered in a neat hierarchy. This helps while locating, managing, and modifying them.
  • JFig contains a component named JFigLocator, which can be extended and used (optional) to automatically locate configuration files not in the classpath, according to a user-defined search scheme.
  • Instead of hard-coding property values everywhere in configuration, JFig assigns values to these properties once (either manually or programmatically) and substitutes variables in their place at other locations. Whenever property values change, these changes are easily reflected in all places the values are referred to by using variables. This practice makes value modifications less error-prone. These variables are defined as [section_name] {key_name}.
    For example:
  • <section name="authentication">
       <entry key="userid" value="admin" />
    <section name=" notification ">
       <entry key="receiverAddress"
              value="[authentication]{userid}@mycomp.com" />
  • JFig easily integrates with other Java-based tools to allow developers to use JFig with their existing applications. Presently, it supports Maven, ANT, Log4j, and Weblogic. Work is in progress to include more tools. Check out JFig's home page for the latest updates.
  • The latest version, 1.4.1, of JFig provides means to convert and store existing non-JFig configuration files to a JFig-supported format, using JFig's substitution syntax. This makes switching to JFig with an existing configuration possible. Check out JFig's home page for the latest updates.
  • JFig provides a means to access system properties through JFig configuration files. One can substitute variables in place of hard-coding system properties to dynamically retrieve system properties, using the following syntax: value="$system_propert_name$"
    For example:
  • <entry key="class_Path" value="$CLASSPATH$" />
  • Other than XML-based configuration files, JFig also supports .ini files. This is useful for Windows-based applications. This article concentrates on XML-based configuration files only. Check out JFig's home page for .ini examples.
  • Putting JFig to Work


    To run, JFig requires the following .jar files, and their respective paths included in the classpath.

    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 {
    } catch (JFigException e) {

    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.
      String uid = JFig.getInstance().getValue("authentication",
    • 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.
      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.
      JFig.setConfigurationValue(("authentication", "userid",
    • 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.
    • printConfigurationDictionary(): Prints all JFig values in a JFig dictionary to the console. Used for monitoring purposes.

    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.

    Page 2 of 3

    This article was originally published on April 1, 2004

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Thanks for your registration, follow us on our social networks to keep up-to-date