1 28 29 package org.objectweb.jonas.management; 30 31 import java.io.FileNotFoundException ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.util.Enumeration ; 36 import java.util.Properties ; 37 import java.util.StringTokenizer ; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 45 public class ReconfiguratorProp extends AbsReconfigurator { 46 47 50 private Properties stableConfig; 51 52 55 private Properties currentConfig; 56 57 64 public ReconfiguratorProp(String name, String configFileName, Properties config) { 65 super(name, configFileName); 66 stableConfig = config; 67 currentConfig = new Properties (); 68 } 69 70 76 public void updateConfig(String key, String value, long sequence) { 77 if (sequence > lastSequence) { 78 currentConfig.setProperty(key, value); 79 lastSequence = sequence; 80 if (logger.isLoggable(BasicLevel.DEBUG)) { 81 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " with value : " + value); 82 } 83 } else { 84 logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !"); 85 logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!"); 86 } 87 } 88 89 96 void updateConfig(String key, String value, boolean add, long sequence) { 97 if (sequence > lastSequence) { 98 String oldValue = currentConfig.getProperty(key); 99 if (oldValue == null) 100 oldValue = stableConfig.getProperty(key); 101 if (logger.isLoggable(BasicLevel.DEBUG)) { 102 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Recufigured property to update " + key + " having value : " + oldValue); 103 } 104 String newValue = updateValue(oldValue, value, add); 105 currentConfig.setProperty(key, newValue); 106 lastSequence = sequence; 107 if (logger.isLoggable(BasicLevel.DEBUG)) { 108 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + key + " with value : " + newValue); 109 } 110 } else { 111 logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !"); 112 logger.log(BasicLevel.WARN, "Reconfiguration value for property " + key + " : " + value + " lost!"); 113 } 114 } 115 120 void updateConfig(Properties props, long sequence) { 121 if (sequence > lastSequence) { 122 for(Enumeration pNames = props.propertyNames(); pNames.hasMoreElements() ; ) { 123 String aName = (String )pNames.nextElement(); 124 String aValue = (String )props.getProperty(aName); 125 if (aValue != null) { 126 currentConfig.setProperty(aName, aValue); 127 if (logger.isLoggable(BasicLevel.DEBUG)) { 128 logger.log(BasicLevel.DEBUG, "- " + sequence + " - Updated property " + aName + " with value : " + aValue); 129 } 130 } 131 } 132 lastSequence = sequence; 133 } else { 134 logger.log(BasicLevel.WARN, "Received out of order reconfiguration message !"); 135 } 136 } 137 138 String updateValue(String oldValue, String value, boolean add) { 139 value = value.trim(); 140 oldValue = oldValue.trim(); 141 String returnValue; 142 if (add) { 143 returnValue = new String (oldValue); 144 returnValue = returnValue + ',' + value; 145 } else { 146 if (logger.isLoggable(BasicLevel.DEBUG)) { 147 logger.log(BasicLevel.DEBUG, "Remove " + value + " from " + oldValue); 148 } 149 returnValue = new String (); 150 boolean firstToken = true; 151 StringTokenizer st = new StringTokenizer (oldValue, ","); 152 while (st.hasMoreTokens()) { 153 String token = st.nextToken().trim(); 154 if (!token.equals(value)) { 155 if (firstToken) { 157 returnValue = new String (token); 158 firstToken = false; 159 } else { 160 returnValue = returnValue + ',' + token; 161 } 162 } 163 } 164 } 165 return returnValue; 166 } 167 168 172 public void saveConfig(long sequence) throws ReconfigException { 173 if (logger.isLoggable(BasicLevel.DEBUG)) { 174 logger.log(BasicLevel.DEBUG, ""); 175 } 176 if (sequence > lastSequence) { 177 for (Enumeration props = currentConfig.keys() ; props.hasMoreElements() ;) { 179 String reconfiguredProp = (String )props.nextElement(); 180 String reconfiguredPropValue = currentConfig.getProperty(reconfiguredProp); 181 stableConfig.setProperty(reconfiguredProp, reconfiguredPropValue); 182 } 183 try { 184 FileOutputStream fo = new FileOutputStream (configFileName); 186 stableConfig.store(fo, "Saved configuration file at "); 187 fo.close(); 188 lastSequence = sequence; 189 if (logger.isLoggable(BasicLevel.DEBUG)) { 190 logger.log(BasicLevel.DEBUG, "Configuration file " + configFileName + " updated"); 191 } 192 } catch (FileNotFoundException e) { 193 throw new ReconfigException("Cant' save configuration file: " + e.toString()); 194 } catch(IOException ioe) { 195 throw new ReconfigException("Cant' save configuration file: " + ioe.toString()); 196 } 197 } else { 198 logger.log(BasicLevel.WARN, "Received out of order save reconfiguration message for " + name + " !"); 199 logger.log(BasicLevel.WARN, "Can not save !!"); 200 logger.log(BasicLevel.WARN, "Please reconfigure and than save !!"); 201 currentConfig = new Properties (); 202 } 203 } 204 } 205 | Popular Tags |