1 17 package org.alfresco.config; 18 19 import java.io.InputStream ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.alfresco.config.evaluator.Evaluator; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 46 public abstract class BaseConfigService implements ConfigService 47 { 48 private static final Log logger = LogFactory.getLog(BaseConfigService.class); 49 50 protected ConfigSource configSource; 51 protected ConfigImpl globalConfig; 52 protected Map <String , Evaluator> evaluators; 53 protected Map <String , List <ConfigSection>> sectionsByArea; 54 protected List <ConfigSection> sections; 55 56 62 public BaseConfigService(ConfigSource configSource) 63 { 64 if (configSource == null) 65 { 66 throw new IllegalArgumentException ("The config source is mandatory"); 67 } 68 this.configSource = configSource; 69 } 70 71 74 public void init() 75 { 76 this.sections = new ArrayList <ConfigSection>(); 77 this.sectionsByArea = new HashMap <String , List <ConfigSection>>(); 78 this.evaluators = new HashMap <String , Evaluator>(); 79 this.globalConfig = new ConfigImpl(); 80 81 addEvaluator("string-compare", "org.alfresco.config.evaluator.StringEvaluator"); 83 addEvaluator("object-type", "org.alfresco.config.evaluator.ObjectTypeEvaluator"); 84 } 85 86 89 public void destroy() 90 { 91 this.sections.clear(); 92 this.sectionsByArea.clear(); 93 this.evaluators.clear(); 94 95 this.sections = null; 96 this.sectionsByArea = null; 97 this.evaluators = null; 98 } 99 100 103 public void reset() 104 { 105 if (logger.isDebugEnabled()) 106 logger.debug("Resetting config service"); 107 108 destroy(); 109 init(); 110 } 111 112 115 public Config getConfig(Object object) 116 { 117 return getConfig(object, new ConfigLookupContext()); 118 } 119 120 123 public Config getConfig(Object object, ConfigLookupContext context) 124 { 125 if (logger.isDebugEnabled()) 126 logger.debug("Retrieving configuration for " + object); 127 128 ConfigImpl results = null; 129 130 if (context.includeGlobalSection()) 131 { 132 results = new ConfigImpl(this.globalConfig); 133 134 if (logger.isDebugEnabled()) 135 logger.debug("Created initial config results using global section"); 136 } 137 else 138 { 139 results = new ConfigImpl(); 140 141 if (logger.isDebugEnabled()) 142 logger.debug("Created initial config results ignoring the global section"); 143 } 144 145 if (context.getAreas().size() > 0) 146 { 147 if (logger.isDebugEnabled()) 148 logger.debug("Restricting search within following areas: " + context.getAreas()); 149 150 for (String area : context.getAreas()) 153 { 154 List <ConfigSection> areaSections = this.sectionsByArea.get(area); 155 if (areaSections == null) 156 { 157 throw new ConfigException("Requested area '" + area + "' has not been defined"); 158 } 159 160 for (ConfigSection section: areaSections) 161 { 162 processSection(section, object, context, results); 163 } 164 } 165 } 166 else 167 { 168 for (ConfigSection section: this.sections) 170 { 171 processSection(section, object, context, results); 172 } 173 } 174 175 return results; 176 } 177 178 181 public Config getGlobalConfig() 182 { 183 return this.globalConfig; 184 } 185 186 189 protected void parse() 190 { 191 for (InputStream inputStream : this.configSource) 192 { 193 if (logger.isDebugEnabled()) 194 logger.debug("Commencing parse of input stream"); 195 196 parse(inputStream); 197 198 if (logger.isDebugEnabled()) 199 logger.debug("Completed parse of input stream"); 200 } 201 } 202 203 209 protected abstract void parse(InputStream stream); 210 211 221 protected void addConfigSection(ConfigSection section, String area) 222 { 223 if (section.isGlobal()) 224 { 225 for (ConfigElement ce : section.getConfigElements()) 228 { 229 ConfigElement existing = this.globalConfig.getConfigElement(ce.getName()); 230 231 if (existing != null) 232 { 233 if (section.isReplace()) 234 { 235 this.globalConfig.putConfigElement(ce); 238 239 if (logger.isDebugEnabled()) 240 logger.debug("Replaced " + existing + " with " + ce); 241 } 242 else 243 { 244 ConfigElement combined = existing.combine(ce); 246 this.globalConfig.putConfigElement(combined); 247 248 if (logger.isDebugEnabled()) 249 { 250 logger.debug("Combined " + existing + " with " + ce + 251 " to create " + combined); 252 } 253 } 254 } 255 else 256 { 257 this.globalConfig.putConfigElement(ce); 258 } 259 } 260 261 262 if (logger.isDebugEnabled()) 263 logger.debug("Added config elements from " + section + " to the global section"); 264 } 265 else 266 { 267 if (area != null && area.length() > 0) 268 { 269 List <ConfigSection> areaSections = this.sectionsByArea.get(area); 272 if (areaSections == null) 273 { 274 areaSections = new ArrayList <ConfigSection>(); 275 this.sectionsByArea.put(area, areaSections); 276 } 277 278 areaSections.add(section); 280 281 if (logger.isDebugEnabled()) 282 logger.debug("Added " + section + " to the '" + area + "' area"); 283 } 284 else 285 { 286 this.sections.add(section); 288 289 if (logger.isDebugEnabled()) 290 logger.debug("Added " + section + " to the sections list"); 291 } 292 } 293 } 294 295 302 protected Evaluator getEvaluator(String name) 303 { 304 return (Evaluator) this.evaluators.get(name); 305 } 306 307 315 protected void addEvaluator(String name, String className) 316 { 317 Evaluator evaluator = null; 318 319 try 320 { 321 Class clazz = Class.forName(className); 322 evaluator = (Evaluator) clazz.newInstance(); 323 } 324 catch (Throwable e) 325 { 326 throw new ConfigException("Could not instantiate evaluator for '" + name + "' with class: " + className, e); 327 } 328 329 this.evaluators.put(name, evaluator); 330 331 if (logger.isDebugEnabled()) 332 logger.debug("Added evaluator '" + name + "': " + className); 333 } 334 335 348 protected void processSection(ConfigSection section, Object object, ConfigLookupContext context, 349 ConfigImpl results) 350 { 351 String evaluatorName = section.getEvaluator(); 352 Evaluator evaluator = getEvaluator(evaluatorName); 353 354 if (evaluator == null) 355 { 356 throw new ConfigException("Unable to locate evaluator implementation for '" + evaluatorName + 357 "' for " + section); 358 } 359 360 context.getAlgorithm().process(section, evaluator, object, results); 361 } 362 } 363 | Popular Tags |