You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Component Overview

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f1eb4ca3-9be5-4919-9dcb-f18a3127ce56"><ac:plain-text-body><![CDATA[

http://jakarta.apache.org/commons/component/images/component-logo-white.png

[http://jakarta.apache.org/commons/component/ Commons-Configuration] Tools to assist in the reading of configuration/preferences files in various formats.[BR] A lot of information is available on the [http://jakarta.apache.org/commons/configuration/ Commons-Configuration website]. If you don't find the information you need you can always contact us using one of the [http://jakarta.apache.org/site/mail2.html#Commons mailing lists].

]]></ac:plain-text-body></ac:structured-macro>

External Resources

Do you have a good example, add it here!

[HierarchicalXMLConfiguration Example]

Auto-Configuration-Reloading-Class

AndresValdez:
I have made an Auto-Configuration-Reloading Class.

  1. This class implements the Runnable interface and inherits from PropertiesConfiguration. 2. This class also has a Singleton behavior, so there is only one Configuration class at time, provided every X milliseconds, read from your config.txt file.

Advantages:

1.Only a big one: You can edit your configuration file at any time you want, and your changes will take effect immediately!!!
So your application will change it's behavior at runtime!! (big grin)

You can see the implementation here:

import org.apache.commons.configuration.ConfigurationException;

import org.apache.commons.configuration.PropertiesConfiguration;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;


/**
 * @author avaldez
 * Esta clase contiene la configuracion obtenida desde el archivo de
 * configuracion llamado "config.txt"
 * Permite que el archivo de configuracion se modifique al vuelo y carga la nueva configuracion automaticamente
 * momento desde el cual los nuevos threads se comportaran segun la nueva configuracion.
 * La linea para delimitar cada cuantos milisegundos se lee el archivo es:
 * "reload.config.after"
 * 
 * Represents a singleton.
 */
public class Configuration extends PropertiesConfiguration implements Runnable {
    static Log log = LogFactory.getLog(Configuration.class);

    private static String configFile;
    private static boolean goOn = true;

    static {
        configFile = "config.txt";
    }

    /**
     * Holds singleton instance
     */
    private static Configuration instance;

    /**
     * prevents instantiation
     */
    private Configuration() {
        // prevent creation
    }

    private Configuration(String file) throws ConfigurationException {
        super(file);
    }

    /**
     * Returns the "singleton" instance.
     @return        the singleton instance
     */
    static public Configuration getInstance() {
        if (instance == null) {
            try {
                instance = new Configuration(configFile);
                log.debug("Loaded:" + ((PropertiesConfiguration) instance).toString());
            } catch (ConfigurationException e) {
                log.error("Error reading configuration file: " + configFile);
            }
        }            
        return instance;
    }

    public static void main(String[] args) {
        Thread t = new Thread(Configuration.getInstance());
        t.start();
    }

    /* Runner
     * @see java.lang.Runnable#run()
     */
    public void run() {
        while (goOn) {
            try {
                instance = new Configuration(configFile);
                log.debug("Loaded:" + ((PropertiesConfiguration) instance).toString());
            } catch (ConfigurationException e) {
                log.error("Error reading configuration file: " + configFile);
            } catch (Exception e) {
                log.error("Error reading configuration file: " + configFile);
            }

            try {
                Thread.sleep(instance.getLong("reload.config.after"));
            } catch (InterruptedException e) {
                log.error("Error reading configuration file: " + configFile);
                log.error(": " + e.getLocalizedMessage());
            }
        }
    }

    
  }

FAQ

Add your questions/answers here.

  • No labels