1 10 11 package org.mule.config.builders; 12 13 import java.io.InputStream ; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Properties ; 20 21 import org.apache.commons.beanutils.MethodUtils; 22 import org.apache.commons.digester.CallMethodRule; 23 import org.apache.commons.digester.CallParamRule; 24 import org.apache.commons.digester.Digester; 25 import org.apache.commons.digester.ObjectCreateRule; 26 import org.apache.commons.digester.Rule; 27 import org.apache.commons.digester.RuleSetBase; 28 import org.mule.MuleManager; 29 import org.mule.MuleServer; 30 import org.mule.config.ConfigurationException; 31 import org.mule.config.MuleConfiguration; 32 import org.mule.config.PropertyFactory; 33 import org.mule.config.i18n.Message; 34 import org.mule.config.i18n.Messages; 35 import org.mule.util.ClassUtils; 36 import org.mule.util.IOUtils; 37 import org.mule.util.StringUtils; 38 import org.xml.sax.Attributes ; 39 40 46 public class MulePropertiesRuleSet extends RuleSetBase 47 { 48 private String path; 49 private PlaceholderProcessor processor; 50 private String propertiesSetterName; 51 private List objectRefs = null; 52 private String parentElement = "properties"; 53 54 public MulePropertiesRuleSet(String path, String propertiesSetterName, List objectRefs) 55 { 56 this(path, objectRefs); 57 this.propertiesSetterName = propertiesSetterName; 58 } 59 60 public MulePropertiesRuleSet(String path, 61 String propertiesSetterName, 62 List objectRefs, 63 String parentElement) 64 { 65 this(path, objectRefs); 66 this.propertiesSetterName = propertiesSetterName; 67 this.parentElement = parentElement; 68 } 69 70 public MulePropertiesRuleSet(String path, List objectRefs) 71 { 72 this.path = path; 73 processor = new PlaceholderProcessor(); 74 this.objectRefs = objectRefs; 75 } 76 77 public void addRuleInstances(Digester digester) 78 { 79 80 path += "/" + parentElement; 81 digester.addRule(path, new ObjectCreateRule(path, HashMap .class) 83 { 84 public void end(String string, String string1) throws Exception 87 { 88 Map props = (Map )digester.peek(); 89 if (props.containsKey(MuleConfiguration.USE_MANAGER_PROPERTIES)) 90 { 91 props.putAll(MuleManager.getInstance().getProperties()); 92 props.remove(MuleConfiguration.USE_MANAGER_PROPERTIES); 93 } 94 super.end(string, string1); 95 96 if (propertiesSetterName == null) 97 { 98 org.mule.util.BeanUtils.populateWithoutFail(digester.peek(), props, true); 99 } 100 else 101 { 102 MethodUtils.invokeMethod(digester.peek(), propertiesSetterName, props); 103 } 105 } 112 }); 113 digester.addCallMethod(path + "/property", "put", 2); 114 115 digester.addRule(path + "/property", new ProcessedCallParamRule(0, "name")); 116 digester.addRule(path + "/property", new ProcessedCallParamRule(1, "value")); 117 118 addPropertyFactoryRule(digester, path + "/factory-property"); 119 addSystemPropertyRule(digester, path + "/system-property"); 120 addFilePropertiesRule(digester, path + "/file-properties"); 121 addContainerPropertyRule(digester, path + "/container-property", propertiesSetterName == null); 122 addTextPropertyRule(digester, path + "/text-property"); 123 124 addMapPropertyRules(digester, path); 125 addListPropertyRules(digester, path); 126 127 addMapPropertyRules(digester, path + "/map"); 128 addListPropertyRules(digester, path + "/map"); 129 } 130 131 protected void addMapPropertyRules(Digester digester, String path) 132 { 133 digester.addObjectCreate(path + "/map", HashMap .class); 134 digester.addCallMethod(path + "/map/property", "put", 2); 135 digester.addRule(path + "/map/property", new ProcessedCallParamRule(0, "name")); 136 digester.addRule(path + "/map/property", new ProcessedCallParamRule(1, "value")); 137 138 addPropertyFactoryRule(digester, path + "/map/factory-property"); 139 addSystemPropertyRule(digester, path + "/map/system-property"); 140 addFilePropertiesRule(digester, path + "/map/file-properties"); 141 addContainerPropertyRule(digester, path + "/map/container-property", false); 142 143 digester.addRule(path + "/map", new CallMethodOnIndexRule("put", 2, 1)); 145 digester.addCallParam(path + "/map", 0, "name"); 146 digester.addCallParam(path + "/map", 1, true); 147 } 148 149 protected void addListPropertyRules(Digester digester, String path) 150 { 151 digester.addObjectCreate(path + "/list", ArrayList .class); 152 153 digester.addRule(path + "/list/entry", new CallMethodRule("add", 1) 155 { 156 public void begin(String endpointName, String endpointName1, Attributes attributes) 157 throws Exception 158 { 159 attributes = processor.processAttributes(attributes, endpointName1); 161 super.begin(endpointName, endpointName1, attributes); 162 } 163 }); 164 165 digester.addRule(path + "/list/entry", new ProcessedCallParamRule(0, "value")); 166 167 addPropertyFactoryRule(digester, path + "/list/factory-entry"); 168 addSystemPropertyRule(digester, path + "/list/system-entry"); 169 addContainerPropertyRule(digester, path + "/list/container-entry", false); 170 171 digester.addRule(path + "/list", new CallMethodOnIndexRule("put", 2, 1)); 173 digester.addCallParam(path + "/list", 0, "name"); 174 digester.addCallParam(path + "/list", 1, true); 175 } 176 177 protected void addPropertyFactoryRule(Digester digester, String path) 178 { 179 digester.addRule(path, new Rule() 180 { 181 182 public void begin(String s, String s1, Attributes attributes) throws Exception 183 { 184 attributes = processor.processAttributes(attributes, s1); 186 187 String clazz = attributes.getValue("factory"); 188 String name = attributes.getValue("name"); 189 Object props = digester.peek(); 190 Object obj = ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS); 191 if (obj instanceof PropertyFactory) 192 { 193 if (props instanceof Map ) 194 { 195 obj = ((PropertyFactory)obj).create((Map )props); 196 } 197 else 198 { 199 obj = ((PropertyFactory)obj).create((Map )digester.peek(1)); 202 } 203 } 204 if (obj != null) 205 { 206 if (props instanceof Map ) 207 { 208 ((Map )props).put(name, obj); 209 } 210 else 211 { 212 ((List )props).add(obj); 213 } 214 } 215 } 216 }); 217 } 218 219 protected void addSystemPropertyRule(Digester digester, String path) 220 { 221 digester.addRule(path, new Rule() 222 { 223 public void begin(String s, String s1, Attributes attributes) throws Exception 224 { 225 attributes = processor.processAttributes(attributes, s1); 227 228 String name = attributes.getValue("name"); 229 String key = attributes.getValue("key"); 230 String defaultValue = attributes.getValue("defaultValue"); 231 String value = System.getProperty(key, defaultValue); 232 if (value != null) 233 { 234 Object props = digester.peek(); 235 if (props instanceof Map ) 236 { 237 ((Map )props).put(name, value); 238 } 239 else 240 { 241 ((List )props).add(value); 242 } 243 } 244 } 245 }); 246 } 247 248 protected synchronized void addFilePropertiesRule(Digester digester, String path) 249 { 250 digester.addRule(path, new Rule() 251 { 252 public void begin(String s, String s1, Attributes attributes) throws Exception 253 { 254 attributes = processor.processAttributes(attributes, s1); 256 257 String location = attributes.getValue("location"); 258 String temp = attributes.getValue("override"); 259 boolean override = "true".equalsIgnoreCase(temp); 260 InputStream is = IOUtils.getResourceAsStream(location, getClass()); 261 if (is == null) 262 { 263 throw new ConfigurationException(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, 264 location)); 265 } 266 267 Properties fileProps = new Properties (); 268 fileProps.load(is); 269 Map digesterProps = (Map )digester.peek(); 270 271 if (override) 272 { 273 digesterProps.putAll(fileProps); 275 } 276 else 277 { 278 String key; 280 for (Iterator iterator = fileProps.keySet().iterator(); iterator.hasNext();) 281 { 282 key = (String )iterator.next(); 283 if (!digesterProps.containsKey(key)) 284 { 285 digesterProps.put(key, fileProps.getProperty(key)); 286 } 287 } 288 } 289 290 if (StringUtils.isNotBlank(MuleServer.getStartupPropertiesFile())) 293 { 294 is = IOUtils.getResourceAsStream(MuleServer.getStartupPropertiesFile(), getClass(), 295 true, false); 296 if (is != null) 297 { 298 Properties startupProps = new Properties (); 299 startupProps.load(is); 300 String key; 301 for (Iterator iterator = startupProps.keySet().iterator(); iterator.hasNext();) 304 { 305 key = (String )iterator.next(); 306 if (digesterProps.containsKey(key)) 307 { 308 digesterProps.put(key, startupProps.getProperty(key)); 309 } 310 } 311 } 312 } 313 } 314 }); 315 } 316 317 protected void addContainerPropertyRule(Digester digester, String path, final boolean asBean) 318 { 319 digester.addRule(path, new Rule() 320 { 321 public void begin(String s, String s1, Attributes attributes) throws Exception 322 { 323 attributes = processor.processAttributes(attributes, s1); 324 325 String name = attributes.getValue("name"); 326 String value = attributes.getValue("reference"); 327 String required = attributes.getValue("required"); 328 String container = attributes.getValue("container"); 329 if (required == null) 330 { 331 required = "true"; 332 } 333 boolean req = Boolean.valueOf(required).booleanValue(); 334 Object obj = null; 338 if (asBean) 339 { 340 obj = digester.peek(1); 341 } 342 else 343 { 344 obj = digester.peek(); 345 } 346 objectRefs.add(new ContainerReference(name, value, obj, req, container)); 347 } 348 }); 349 } 350 351 protected void addTextPropertyRule(Digester digester, String path) 352 { 353 354 digester.addRule(path, new Rule() 355 { 356 private String name = null; 357 358 public void begin(String s, String s1, Attributes attributes) throws Exception 359 { 360 attributes = processor.processAttributes(attributes, s1); 362 name = attributes.getValue("name"); 363 } 364 365 public void body(String string, String string1, String string2) throws Exception 366 { 367 Object props = digester.peek(); 368 if (props instanceof Map ) 369 { 370 ((Map )props).put(name, string2); 371 } 372 else 373 { 374 ((List )props).add(string2); 375 } 376 } 377 }); 378 } 379 380 private class ProcessedCallParamRule extends CallParamRule 381 { 382 public ProcessedCallParamRule(int i) 383 { 384 super(i); 385 } 386 387 public ProcessedCallParamRule(int i, String s) 388 { 389 super(i, s); 390 } 391 392 public ProcessedCallParamRule(int i, boolean b) 393 { 394 super(i, b); 395 } 396 397 public ProcessedCallParamRule(int i, int i1) 398 { 399 super(i, i1); 400 } 401 402 public void begin(String endpointName, String endpointName1, Attributes attributes) throws Exception 403 { 404 attributes = processor.processAttributes(attributes, endpointName1); 406 super.begin(endpointName, endpointName1, attributes); 407 } 408 } 409 410 public static interface PropertiesCallback 411 { 412 public void setProperties(Map props, Digester digester) throws Exception ; 413 } 414 } 415 | Popular Tags |