00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 package org.objectweb.jonas.management;
00030
00031
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
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
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
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 }