You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Apache Wiki <wi...@apache.org> on 2005/07/22 13:14:31 UTC

[Jakarta-commons Wiki] Update of "Configuration" by EmmanuelBourg

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by EmmanuelBourg:
http://wiki.apache.org/jakarta-commons/Configuration

The comment on the change is:
Reloading is now included in the API, not need to publish a workaround anymore

------------------------------------------------------------------------------
   ||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!! :D
- 
- 
- 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 =
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org