KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > builders > MulePropertiesRuleSet


1 /*
2  * $Id: MulePropertiesRuleSet.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.config.builders;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Properties JavaDoc;
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 JavaDoc;
39
40 /**
41  * A digester rule set that loads rules for <properties> tags and its child tags;
42  *
43  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
44  * @version $Revision: 3798 $
45  */

46 public class MulePropertiesRuleSet extends RuleSetBase
47 {
48     private String JavaDoc path;
49     private PlaceholderProcessor processor;
50     private String JavaDoc propertiesSetterName;
51     private List JavaDoc objectRefs = null;
52     private String JavaDoc parentElement = "properties";
53
54     public MulePropertiesRuleSet(String JavaDoc path, String JavaDoc propertiesSetterName, List JavaDoc objectRefs)
55     {
56         this(path, objectRefs);
57         this.propertiesSetterName = propertiesSetterName;
58     }
59
60     public MulePropertiesRuleSet(String JavaDoc path,
61                                  String JavaDoc propertiesSetterName,
62                                  List JavaDoc objectRefs,
63                                  String JavaDoc parentElement)
64     {
65         this(path, objectRefs);
66         this.propertiesSetterName = propertiesSetterName;
67         this.parentElement = parentElement;
68     }
69
70     public MulePropertiesRuleSet(String JavaDoc path, List JavaDoc 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.addObjectCreate(path, HashMap.class);
82
digester.addRule(path, new ObjectCreateRule(path, HashMap JavaDoc.class)
83         {
84             // This will set the properties on the top object as bean setters if
85
// the flag is set
86
public void end(String JavaDoc string, String JavaDoc string1) throws Exception JavaDoc
87             {
88                 Map JavaDoc props = (Map JavaDoc)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                     // digester.addSetNext(path + "/properties", "setProperties");
104
}
105                 // todo - is this needed?
106
// support for setting transformers as properties
107
// String trans = (String) props.remove("transformer");
108
// if (trans != null) {
109
// addTransformerReference("transformer", trans, digester.peek());
110
// }
111
}
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 JavaDoc path)
132     {
133         digester.addObjectCreate(path + "/map", HashMap JavaDoc.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         // call the put method on top -1 object
144
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 JavaDoc path)
150     {
151         digester.addObjectCreate(path + "/list", ArrayList JavaDoc.class);
152
153         // digester.addCallMethod(path + "/list/entry", "add", 1);
154
digester.addRule(path + "/list/entry", new CallMethodRule("add", 1)
155         {
156             public void begin(String JavaDoc endpointName, String JavaDoc endpointName1, Attributes JavaDoc attributes)
157                 throws Exception JavaDoc
158             {
159                 // Process template tokens
160
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         // A small hack to call a method on top -1
172
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 JavaDoc path)
178     {
179         digester.addRule(path, new Rule()
180         {
181
182             public void begin(String JavaDoc s, String JavaDoc s1, Attributes JavaDoc attributes) throws Exception JavaDoc
183             {
184                 // Process template tokens
185
attributes = processor.processAttributes(attributes, s1);
186
187                 String JavaDoc clazz = attributes.getValue("factory");
188                 String JavaDoc name = attributes.getValue("name");
189                 Object JavaDoc props = digester.peek();
190                 Object JavaDoc obj = ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS);
191                 if (obj instanceof PropertyFactory)
192                 {
193                     if (props instanceof Map JavaDoc)
194                     {
195                         obj = ((PropertyFactory)obj).create((Map JavaDoc)props);
196                     }
197                     else
198                     {
199                         // this must be a list so we'll get the containing
200
// properties map
201
obj = ((PropertyFactory)obj).create((Map JavaDoc)digester.peek(1));
202                     }
203                 }
204                 if (obj != null)
205                 {
206                     if (props instanceof Map JavaDoc)
207                     {
208                         ((Map JavaDoc)props).put(name, obj);
209                     }
210                     else
211                     {
212                         ((List JavaDoc)props).add(obj);
213                     }
214                 }
215             }
216         });
217     }
218
219     protected void addSystemPropertyRule(Digester digester, String JavaDoc path)
220     {
221         digester.addRule(path, new Rule()
222         {
223             public void begin(String JavaDoc s, String JavaDoc s1, Attributes JavaDoc attributes) throws Exception JavaDoc
224             {
225                 // Process template tokens
226
attributes = processor.processAttributes(attributes, s1);
227
228                 String JavaDoc name = attributes.getValue("name");
229                 String JavaDoc key = attributes.getValue("key");
230                 String JavaDoc defaultValue = attributes.getValue("defaultValue");
231                 String JavaDoc value = System.getProperty(key, defaultValue);
232                 if (value != null)
233                 {
234                     Object JavaDoc props = digester.peek();
235                     if (props instanceof Map JavaDoc)
236                     {
237                         ((Map JavaDoc)props).put(name, value);
238                     }
239                     else
240                     {
241                         ((List JavaDoc)props).add(value);
242                     }
243                 }
244             }
245         });
246     }
247
248     protected synchronized void addFilePropertiesRule(Digester digester, String JavaDoc path)
249     {
250         digester.addRule(path, new Rule()
251         {
252             public void begin(String JavaDoc s, String JavaDoc s1, Attributes JavaDoc attributes) throws Exception JavaDoc
253             {
254                 // Process template tokens
255
attributes = processor.processAttributes(attributes, s1);
256
257                 String JavaDoc location = attributes.getValue("location");
258                 String JavaDoc temp = attributes.getValue("override");
259                 boolean override = "true".equalsIgnoreCase(temp);
260                 InputStream JavaDoc 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 JavaDoc fileProps = new Properties JavaDoc();
268                 fileProps.load(is);
269                 Map JavaDoc digesterProps = (Map JavaDoc)digester.peek();
270
271                 if (override)
272                 {
273                     // Set all properties.
274
digesterProps.putAll(fileProps);
275                 }
276                 else
277                 {
278                     // Set only those properties which have not yet been set.
279
String JavaDoc key;
280                     for (Iterator JavaDoc iterator = fileProps.keySet().iterator(); iterator.hasNext();)
281                     {
282                         key = (String JavaDoc)iterator.next();
283                         if (!digesterProps.containsKey(key))
284                         {
285                             digesterProps.put(key, fileProps.getProperty(key));
286                         }
287                     }
288                 }
289
290                 // If startup properties were given on the command line, they should
291
// override the file properties.
292
if (StringUtils.isNotBlank(MuleServer.getStartupPropertiesFile()))
293                 {
294                     is = IOUtils.getResourceAsStream(MuleServer.getStartupPropertiesFile(), getClass(),
295                     /* tryAsFile */true, /* tryAsUrl */false);
296                     if (is != null)
297                     {
298                         Properties JavaDoc startupProps = new Properties JavaDoc();
299                         startupProps.load(is);
300                         String JavaDoc key;
301                         // For each startup property, if the property has been set,
302
// override its value.
303
for (Iterator JavaDoc iterator = startupProps.keySet().iterator(); iterator.hasNext();)
304                         {
305                             key = (String JavaDoc)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 JavaDoc path, final boolean asBean)
318     {
319         digester.addRule(path, new Rule()
320         {
321             public void begin(String JavaDoc s, String JavaDoc s1, Attributes JavaDoc attributes) throws Exception JavaDoc
322             {
323                 attributes = processor.processAttributes(attributes, s1);
324
325                 String JavaDoc name = attributes.getValue("name");
326                 String JavaDoc value = attributes.getValue("reference");
327                 String JavaDoc required = attributes.getValue("required");
328                 String JavaDoc container = attributes.getValue("container");
329                 if (required == null)
330                 {
331                     required = "true";
332                 }
333                 boolean req = Boolean.valueOf(required).booleanValue();
334                 // if we're not setting as bean properties we need get the
335
// top-most object
336
// which will be a list or Map
337
Object JavaDoc 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 JavaDoc path)
352     {
353
354         digester.addRule(path, new Rule()
355         {
356             private String JavaDoc name = null;
357
358             public void begin(String JavaDoc s, String JavaDoc s1, Attributes JavaDoc attributes) throws Exception JavaDoc
359             {
360                 // Process template tokens
361
attributes = processor.processAttributes(attributes, s1);
362                 name = attributes.getValue("name");
363             }
364
365             public void body(String JavaDoc string, String JavaDoc string1, String JavaDoc string2) throws Exception JavaDoc
366             {
367                 Object JavaDoc props = digester.peek();
368                 if (props instanceof Map JavaDoc)
369                 {
370                     ((Map JavaDoc)props).put(name, string2);
371                 }
372                 else
373                 {
374                     ((List JavaDoc)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 JavaDoc 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 JavaDoc endpointName, String JavaDoc endpointName1, Attributes JavaDoc attributes) throws Exception JavaDoc
403         {
404             // Process template tokens
405
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 JavaDoc props, Digester digester) throws Exception JavaDoc;
413     }
414 }
415
Popular Tags