ReconfiguratorProp.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  * 
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  * 
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * Initial developer(s): Adriana Danes
00022  * Contributor(s):
00023  *
00024  * --------------------------------------------------------------------------
00025  * $Id: ReconfiguratorProp.java,v 1.4 2004/09/22 17:30:36 ehardesty Exp $
00026  * --------------------------------------------------------------------------
00027  */
00028 
00029 package org.objectweb.jonas.management;
00030 
00031 // general Java imports
00032 import java.io.FileNotFoundException;
00033 import java.io.FileOutputStream;
00034 import java.io.IOException;
00035 import java.util.Enumeration;
00036 import java.util.Properties;
00037 import java.util.StringTokenizer;
00038 
00039 import org.objectweb.util.monolog.api.BasicLevel;
00045 public class ReconfiguratorProp extends AbsReconfigurator {
00046 
00050     private Properties stableConfig;
00051 
00055     private Properties currentConfig;
00056    
00064     public ReconfiguratorProp(String name, String configFileName, Properties config) {
00065         super(name, configFileName);
00066         stableConfig = config;
00067         currentConfig = new Properties();
00068     }
00069     
00076     public void updateConfig(String key, String value, long sequence) {
00077         if (sequence > lastSequence) {
00078             currentConfig.setProperty(key, value);
00079             lastSequence = sequence;
00080             logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " with value : " + value);
00081         } else {
00082             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
00083             logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!");
00084         }
00085     }  
00086   
00094     void updateConfig(String key, String value, boolean add, long sequence) {
00095         if (sequence > lastSequence) {
00096             String oldValue = currentConfig.getProperty(key);
00097             if (oldValue == null)
00098                 oldValue = stableConfig.getProperty(key);
00099             logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " having value : " + oldValue);
00100             String newValue = updateValue(oldValue, value, add);
00101             currentConfig.setProperty(key, newValue);
00102             lastSequence = sequence;
00103             logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + key + " with value : " + newValue);
00104         } else {
00105             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
00106             logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!");
00107         }
00108     }
00114     void updateConfig(Properties props, long sequence) {
00115         if (sequence > lastSequence) {
00116             for(Enumeration pNames = props.propertyNames(); pNames.hasMoreElements() ; ) {  
00117                 String aName = (String)pNames.nextElement();
00118                 String aValue = (String)props.getProperty(aName);
00119                 if (aValue != null) {
00120                     currentConfig.setProperty(aName, aValue);
00121                     logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + aName + " with value : " + aValue);
00122                 }
00123             }
00124             lastSequence = sequence;
00125         } else {
00126             logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !");
00127         }
00128     }
00129 
00130     String updateValue(String oldValue, String value, boolean add) {
00131         value = value.trim();
00132         oldValue = oldValue.trim();
00133         String returnValue;
00134         if (add) {
00135             returnValue = new String(oldValue);
00136             returnValue = returnValue + ',' + value;
00137         } else {          
00138             logger.log(BasicLevel.DEBUG, "Remove " + value + " from " + oldValue);
00139             returnValue = new String();
00140             boolean firstToken = true;
00141             StringTokenizer st = new StringTokenizer(oldValue, ",");
00142             while (st.hasMoreTokens()) {
00143                 String token = st.nextToken().trim(); 
00144                 if (!token.equals(value)) {
00145                     // add the token
00146                     if (firstToken) {
00147                         returnValue = new String(token);
00148                         firstToken = false;
00149                     } else {
00150                         returnValue = returnValue + ',' + token;
00151                     }
00152                 }
00153             }
00154         }
00155         return returnValue;
00156     }
00157 
00162     public void saveConfig(long sequence) throws ReconfigException {
00163         logger.log(BasicLevel.DEBUG, "");
00164         if (sequence > lastSequence) {
00165             // Update stableConfig with properties in currentConfig
00166             for (Enumeration props = currentConfig.keys() ; props.hasMoreElements() ;) {
00167                 String reconfiguredProp = (String)props.nextElement();
00168                 String reconfiguredPropValue = currentConfig.getProperty(reconfiguredProp);
00169                 stableConfig.setProperty(reconfiguredProp, reconfiguredPropValue);
00170             }
00171             try {
00172                 // Store stableConfig
00173                 FileOutputStream fo = new FileOutputStream(configFileName);
00174                 stableConfig.store(fo,  "Saved configuration file at ");
00175                 fo.close();
00176                 lastSequence = sequence;
00177                 logger.log(BasicLevel.DEBUG, "Configuration file " + configFileName + " updated");
00178             } catch (FileNotFoundException e) {
00179                 throw new ReconfigException("Cant' save configuration file: " + e.toString());
00180             } catch(IOException ioe) {
00181                 throw new ReconfigException("Cant' save configuration file: " + ioe.toString());
00182             }
00183         } else {
00184             logger.log(BasicLevel.WARN, "Received out of order save reconfiguration message for " + name + " !");
00185             logger.log(BasicLevel.WARN, "Can not save !!");
00186             logger.log(BasicLevel.WARN, "Please reconfigure and than save !!");
00187             currentConfig = new Properties();
00188         }
00189     }
00190 }

Generated on Tue Feb 15 15:05:18 2005 for JOnAS by  doxygen 1.3.9.1