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