October 2, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Managing Your Configuration with JFig

  • April 1, 2004
  • 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>
          ......
    <section name=" notification ">
          ......
       <entry key="receiverAddress"
              value="[authentication]{userid}@mycomp.com" />
    </section>
    
  • 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

    Requirements

    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 {
       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.





    Page 2 of 3



    Comment and Contribute

     


    (Maximum characters: 1200). You have characters left.

     

     


    Sitemap | Contact Us

    Rocket Fuel