1 17 package org.alfresco.config.xml; 18 19 import java.io.InputStream ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.alfresco.config.BaseConfigService; 25 import org.alfresco.config.ConfigElement; 26 import org.alfresco.config.ConfigException; 27 import org.alfresco.config.ConfigSectionImpl; 28 import org.alfresco.config.ConfigSource; 29 import org.alfresco.config.xml.elementreader.ConfigElementReader; 30 import org.alfresco.config.xml.elementreader.GenericElementReader; 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.dom4j.Document; 34 import org.dom4j.Element; 35 import org.dom4j.io.SAXReader; 36 37 42 public class XMLConfigService extends BaseConfigService implements XMLConfigConstants 43 { 44 private static final Log logger = LogFactory.getLog(XMLConfigService.class); 45 46 private Map <String , ConfigElementReader> elementReaders; 47 private String currentArea; 48 49 55 public XMLConfigService(ConfigSource configSource) 56 { 57 super(configSource); 58 } 59 60 63 public void init() 64 { 65 if (logger.isDebugEnabled()) 66 logger.debug("Commencing initialisation"); 67 68 super.init(); 69 70 this.elementReaders = new HashMap <String , ConfigElementReader>(); 72 73 parse(); 74 75 if (logger.isDebugEnabled()) 76 logger.debug("Completed initialisation"); 77 } 78 79 82 public void destroy() 83 { 84 this.elementReaders.clear(); 85 this.elementReaders = null; 86 this.currentArea = null; 87 88 super.destroy(); 89 } 90 91 94 protected void parse(InputStream stream) 95 { 96 try 97 { 98 SAXReader reader = new SAXReader(); 100 Document document = reader.read(stream); 101 Element rootElement = document.getRootElement(); 102 103 this.currentArea = rootElement.attributeValue("area"); 105 106 Element pluginsConfig = rootElement.element(ELEMENT_PLUG_INS); 108 parsePluginsElement(pluginsConfig); 109 110 Iterator configElements = rootElement.elementIterator(ELEMENT_CONFIG); 112 while (configElements.hasNext()) 113 { 114 Element configElement = (Element) configElements.next(); 115 parseConfigElement(configElement); 116 } 117 } 118 catch (Throwable e) 119 { 120 if (e instanceof ConfigException) 121 { 122 throw (ConfigException)e; 123 } 124 else 125 { 126 throw new ConfigException("Failed to parse config stream", e); 127 } 128 } 129 } 130 131 137 private void parsePluginsElement(Element pluginsElement) 138 { 139 if (pluginsElement != null) 140 { 141 parseEvaluatorsElement(pluginsElement.element(ELEMENT_EVALUATORS)); 143 144 parseElementReadersElement(pluginsElement.element(ELEMENT_ELEMENT_READERS)); 146 } 147 } 148 149 154 private void parseEvaluatorsElement(Element evaluatorsElement) 155 { 156 if (evaluatorsElement != null) 157 { 158 Iterator evaluators = evaluatorsElement.elementIterator(); 159 while (evaluators.hasNext()) 160 { 161 Element evaluatorElement = (Element) evaluators.next(); 162 String evaluatorName = evaluatorElement.attributeValue(ATTR_ID); 163 String evaluatorClass = evaluatorElement.attributeValue(ATTR_CLASS); 164 165 if (evaluatorName == null || evaluatorName.length() == 0) 168 { 169 throw new ConfigException("All evaluator elements must define an id attribute"); 170 } 171 172 if (evaluatorClass == null || evaluatorClass.length() == 0) 173 { 174 throw new ConfigException("Evaluator '" + evaluatorName + "' must define a class attribute"); 175 } 176 177 this.addEvaluator(evaluatorName, evaluatorClass); 179 } 180 } 181 } 182 183 188 private void parseElementReadersElement(Element readersElement) 189 { 190 if (readersElement != null) 191 { 192 Iterator readers = readersElement.elementIterator(); 193 while (readers.hasNext()) 194 { 195 Element readerElement = (Element) readers.next(); 196 String readerElementName = readerElement.attributeValue(ATTR_ELEMENT_NAME); 197 String readerElementClass = readerElement.attributeValue(ATTR_CLASS); 198 199 if (readerElementName == null || readerElementName.length() == 0) 200 { 201 throw new ConfigException("All element-reader elements must define an element-name attribute"); 202 } 203 204 if (readerElementClass == null || readerElementClass.length() == 0) 205 { 206 throw new ConfigException("Element-reader '" + readerElementName 207 + "' must define a class attribute"); 208 } 209 210 addConfigElementReader(readerElementName, readerElementClass); 212 } 213 } 214 } 215 216 222 private void parseConfigElement(Element configElement) 223 { 224 if (configElement != null) 225 { 226 boolean replace = false; 227 String evaluatorName = configElement.attributeValue(ATTR_EVALUATOR); 228 String condition = configElement.attributeValue(ATTR_CONDITION); 229 String replaceValue = configElement.attributeValue(ATTR_REPLACE); 230 if (replaceValue != null && replaceValue.equalsIgnoreCase("true")) 231 { 232 replace = true; 233 } 234 235 ConfigSectionImpl section = new ConfigSectionImpl(evaluatorName, condition, replace); 237 238 Iterator children = configElement.elementIterator(); 240 while (children.hasNext()) 241 { 242 Element child = (Element) children.next(); 243 String elementName = child.getName(); 244 245 ConfigElementReader elementReader = getConfigElementReader(elementName); 247 if (logger.isDebugEnabled()) 248 logger.debug("Retrieved element reader " + elementReader + " for element named '" + elementName 249 + "'"); 250 251 if (elementReader == null) 252 { 253 elementReader = new GenericElementReader(); 254 255 if (logger.isDebugEnabled()) 256 logger.debug("Defaulting to " + elementReader + " as there wasn't an element " 257 + "reader registered for element '" + elementName + "'"); 258 } 259 260 ConfigElement cfgElement = elementReader.parse(child); 261 section.addConfigElement(cfgElement); 262 263 if (logger.isDebugEnabled()) 264 logger.debug("Added " + cfgElement + " to " + section); 265 } 266 267 addConfigSection(section, this.currentArea); 270 } 271 } 272 273 281 private void addConfigElementReader(String elementName, String className) 282 { 283 ConfigElementReader elementReader = null; 284 285 try 286 { 287 Class clazz = Class.forName(className); 288 elementReader = (ConfigElementReader) clazz.newInstance(); 289 } 290 catch (Throwable e) 291 { 292 throw new ConfigException("Could not instantiate element reader for '" + elementName + "' with class: " 293 + className, e); 294 295 } 296 297 this.elementReaders.put(elementName, elementReader); 298 299 if (logger.isDebugEnabled()) 300 logger.debug("Added element reader '" + elementName + "': " + className); 301 } 302 303 310 private ConfigElementReader getConfigElementReader(String elementName) 311 { 312 return (ConfigElementReader) this.elementReaders.get(elementName); 313 } 314 } 315 | Popular Tags |