1 17 18 package org.sape.carbon.core.config.cache; 19 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.sape.carbon.core.config.Configuration; 24 import org.sape.carbon.core.config.ConfigurationNotFoundException; 25 import org.sape.carbon.core.config.ConfigurationService; 26 import org.sape.carbon.core.config.format.ConfigurationFormatException; 27 import org.sape.carbon.core.config.node.ConfigurationDocument; 28 import org.sape.carbon.core.config.node.Node; 29 import org.sape.carbon.core.config.node.NodeIOException; 30 import org.sape.carbon.core.config.node.NodeNotFoundException; 31 import org.sape.carbon.core.config.node.event.NodeEventListener; 32 import org.sape.carbon.core.exception.ExceptionUtility; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 38 73 public class DoubleCheckConfigurationCache 74 implements ConfigurationCache, NodeEventListener { 75 76 79 private Log log = LogFactory.getLog(this.getClass()); 80 81 82 private Map activeCache = new HashMap (); 83 84 85 private Map backgroundCache = new HashMap (); 86 87 88 private ConfigurationService configService; 89 90 91 private int entranceCount = 0; 92 93 94 private Map loadingConfigurations = new HashMap (); 95 96 102 public DoubleCheckConfigurationCache(ConfigurationService configService) { 103 this.configService = configService; 104 } 105 106 116 public Configuration getConfiguration(String name) { 117 Configuration config = (Configuration) this.activeCache.get(name); 118 if (config == null) { 119 synchronized (this) { 120 config = (Configuration) this.activeCache.get(name); 122 if (config == null) { 123 config = 125 (Configuration) this.loadingConfigurations.get(name); 126 127 if (config == null) { 128 132 this.entranceCount++; 137 try { 138 config = 139 this.configService.fetchWritableConfiguration( 140 name); 141 142 this.loadingConfigurations.put(name, config); 143 config.setConfigurationReadOnly(); 144 this.configService.addNodeListener(name, this); 145 146 if (this.entranceCount == 1) { 147 151 this.backgroundCache.putAll( 153 this.loadingConfigurations); 154 155 activateBackgroundCache(); 157 158 this.backgroundCache.putAll( 162 this.loadingConfigurations); 163 } 164 165 } catch (NodeNotFoundException nnfe) { 166 throw new ConfigurationNotFoundException( 169 this.getClass(), 170 "The configuration [" 171 + name 172 + "] was not found", 173 nnfe); 174 } finally { 175 this.entranceCount--; 176 177 if (this.entranceCount == 0) { 178 this.loadingConfigurations.clear(); 182 } 183 } 184 } 185 } 186 } 187 } 188 189 return config; 190 191 } 192 193 198 public synchronized void nodeChanged(Node changedNode) { 199 try { 200 Configuration config = 201 ((ConfigurationDocument) changedNode).readConfiguration(); 202 203 this.backgroundCache.clear(); 205 this.backgroundCache.put(config.getConfigurationName(), config); 206 207 activateBackgroundCache(); 209 210 this.backgroundCache.clear(); 213 this.backgroundCache.put(config.getConfigurationName(), config); 214 215 } catch (NodeIOException nioe) { 216 if (log.isWarnEnabled()) { 217 log.warn("Could not read new configuration: " 218 + ExceptionUtility.printStackTracesToString(nioe)); 219 } 220 } catch (ConfigurationFormatException cfe) { 221 if (log.isWarnEnabled()) { 222 log.warn("Could not read new configuration: " 223 + ExceptionUtility.printStackTracesToString(cfe)); 224 } 225 } 226 } 227 228 233 public synchronized void nodeRemoved(String removedNodeName) { 234 this.backgroundCache.clear(); 236 this.backgroundCache.remove(removedNodeName); 237 238 activateBackgroundCache(); 240 241 this.backgroundCache.clear(); 244 this.backgroundCache.remove(removedNodeName); 245 } 246 247 250 private void activateBackgroundCache() { 251 Map swapMap = this.activeCache; 254 this.activeCache = this.backgroundCache; 255 this.backgroundCache = swapMap; 256 } 257 } 258 | Popular Tags |