1 10 11 package org.mule.config.builders; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.Reader ; 16 import java.util.ArrayList ; 17 import java.util.List ; 18 19 import org.apache.commons.digester.Digester; 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.mule.MuleManager; 23 import org.mule.config.ConfigurationException; 24 import org.mule.config.MuleDtdResolver; 25 import org.mule.config.MuleProperties; 26 import org.mule.config.ReaderResource; 27 import org.mule.config.i18n.Message; 28 import org.mule.config.i18n.Messages; 29 import org.mule.impl.container.MuleContainerContext; 30 import org.mule.umo.UMOFilter; 31 import org.mule.util.IOUtils; 32 import org.xml.sax.ErrorHandler ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.SAXParseException ; 35 36 42 public abstract class AbstractDigesterConfiguration 43 { 44 public static final String DEFAULT_CONTAINER_CONTEXT = MuleContainerContext.class.getName(); 45 public static final String FILTER_INTERFACE = UMOFilter.class.getName(); 46 47 50 protected transient Log logger = LogFactory.getLog(getClass()); 51 52 protected Digester digester; 53 protected List containerReferences = new ArrayList (); 54 protected String configEncoding; 55 56 protected AbstractDigesterConfiguration(boolean validate, String dtd) 57 { 58 digester = new Digester() 61 { 62 public void warning(SAXParseException e) throws SAXException 63 { 64 if (errorHandler != null) 65 { 66 errorHandler.warning(e); 67 } 68 } 69 }; 70 71 configEncoding = System.getProperty(MuleProperties.MULE_ENCODING_SYSTEM_PROPERTY, 72 MuleManager.getConfiguration().getEncoding()); 73 74 digester.setValidating(validate); 75 digester.setEntityResolver(new MuleDtdResolver(dtd)); 76 77 digester.setErrorHandler(new ErrorHandler () 78 { 79 public void error(SAXParseException exception) throws SAXException 80 { 81 logger.error(exception.getMessage(), exception); 82 throw new SAXException (exception); 83 } 84 85 public void fatalError(SAXParseException exception) throws SAXException 86 { 87 logger.fatal(exception.getMessage(), exception); 88 throw new SAXException (exception); 89 } 90 91 public void warning(SAXParseException exception) 92 { 93 logger.warn(exception.getMessage()); 94 } 95 }); 96 } 97 98 114 protected Object process(ReaderResource[] configResources) throws ConfigurationException 115 { 116 Object result = null; 117 Reader configResource; 118 for (int i = 0; i < configResources.length; i++) 119 { 120 try 121 { 122 configResource = configResources[i].getReader(); 123 result = digester.parse(configResource); 124 } 125 catch (Exception e) 126 { 127 throw new ConfigurationException(new Message(Messages.FAILED_TO_PARSE_CONFIG_RESOURCE_X, 128 configResources[i].getDescription()), e); 129 } 130 } 131 132 return result; 133 } 134 135 144 protected InputStream loadConfig(String configResource) throws ConfigurationException 145 { 146 InputStream is = null; 147 try 148 { 149 is = IOUtils.getResourceAsStream(configResource, getClass()); 150 } 151 catch (IOException e) 152 { 153 throw new ConfigurationException(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, 154 configResource), e); 155 } 156 157 if (is != null) 158 { 159 return is; 160 } 161 else 162 { 163 throw new ConfigurationException(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, 164 configResource)); 165 } 166 } 167 168 public abstract String getRootName(); 169 170 protected void addContainerContextRules(String path, String setterMethod, int parentIndex) 171 throws ConfigurationException 172 { 173 digester.addObjectCreate(path, DEFAULT_CONTAINER_CONTEXT, "className"); 175 digester.addSetProperties(path, "name", "name"); 176 addMulePropertiesRule(path, digester); 177 178 digester.addSetNext(path, setterMethod); 180 } 181 182 protected void addServerPropertiesRules(String path, String setterMethod, int parentIndex) 183 { 184 int i = path.lastIndexOf("/"); 186 addMulePropertiesRule(path.substring(0, i), digester, setterMethod, path.substring(i + 1)); 187 } 188 189 protected void addSetPropertiesRule(String path, Digester digester) 190 { 191 digester.addRule(path, new MuleSetPropertiesRule()); 192 } 193 194 protected void addSetPropertiesRule(String path, Digester digester, String [] s1, String [] s2) 195 { 196 digester.addRule(path, new MuleSetPropertiesRule(s1, s2)); 197 } 198 199 protected void addMulePropertiesRule(String path, Digester digester) 200 { 201 digester.addRuleSet(new MulePropertiesRuleSet(path, containerReferences)); 202 } 203 204 protected void addMulePropertiesRule(String path, Digester digester, String propertiesSetter) 205 { 206 digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences)); 207 } 208 209 protected void addMulePropertiesRule(String path, 210 Digester digester, 211 String propertiesSetter, 212 String parentElement) 213 { 214 digester.addRuleSet(new MulePropertiesRuleSet(path, propertiesSetter, containerReferences, 215 parentElement)); 216 } 217 218 protected void addFilterRules(Digester digester, String path) throws ConfigurationException 219 { 220 addSingleFilterRule(digester, path); 222 path += "/filter"; 223 addFilterGroupRule(digester, path); 224 225 addFilterGroupRule(digester, path + "/left-filter"); 226 addFilterGroupRule(digester, path + "/right-filter"); 227 addFilterGroupRule(digester, path + "/filter"); 228 229 addFilterGroupRule(digester, path + "/left-filter/left-filter"); 230 addFilterGroupRule(digester, path + "/left-filter/right-filter"); 231 addFilterGroupRule(digester, path + "/left-filter/filter"); 232 233 addFilterGroupRule(digester, path + "/right-filter/left-filter"); 234 addFilterGroupRule(digester, path + "/right-filter/right-filter"); 235 addFilterGroupRule(digester, path + "/right-filter/filter"); 236 237 addFilterGroupRule(digester, path + "/filter/left-filter"); 238 addFilterGroupRule(digester, path + "/filter/right-filter"); 239 addFilterGroupRule(digester, path + "/filter/filter"); 240 241 } 243 244 protected void addFilterGroupRule(Digester digester, String path) throws ConfigurationException 245 { 246 addLeftFilterRule(digester, path); 247 addRightFilterRule(digester, path); 248 addSingleFilterRule(digester, path); 249 } 250 251 protected void addLeftFilterRule(Digester digester, String path) throws ConfigurationException 252 { 253 path += "/left-filter"; 254 digester.addObjectCreate(path, FILTER_INTERFACE, "className"); 255 addSetPropertiesRule(path, digester); 256 addMulePropertiesRule(path, digester); 257 digester.addSetNext(path, "setLeftFilter"); 258 } 259 260 protected void addRightFilterRule(Digester digester, String path) throws ConfigurationException 261 { 262 path += "/right-filter"; 263 digester.addObjectCreate(path, FILTER_INTERFACE, "className"); 264 addSetPropertiesRule(path, digester); 265 addMulePropertiesRule(path, digester); 266 digester.addSetNext(path, "setRightFilter"); 267 } 268 269 protected void addSingleFilterRule(Digester digester, String path) throws ConfigurationException 270 { 271 path += "/filter"; 272 digester.addObjectCreate(path, FILTER_INTERFACE, "className"); 273 addSetPropertiesRule(path, digester); 274 addMulePropertiesRule(path, digester); 275 digester.addSetNext(path, "setFilter"); 276 } 277 } 278 | Popular Tags |