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