1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.config.spring.processors; |
11 | |
|
12 | |
import org.mule.RegistryContext; |
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.config.PropertyFactory; |
15 | |
import org.mule.api.context.MuleContextAware; |
16 | |
import org.mule.config.i18n.CoreMessages; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.Map; |
22 | |
import java.util.Properties; |
23 | |
|
24 | |
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class PropertyPlaceholderProcessor extends PropertyPlaceholderConfigurer implements MuleContextAware |
30 | |
{ |
31 | |
private MuleContext muleContext; |
32 | 0 | private Map factories = new HashMap(); |
33 | |
|
34 | |
|
35 | |
|
36 | |
protected Properties mergeProperties() throws IOException |
37 | |
{ |
38 | 0 | RegistryProperties props = new RegistryProperties(); |
39 | 0 | props.putAll(super.mergeProperties()); |
40 | |
|
41 | |
|
42 | 0 | props.put("mule.working.dir", muleContext.getConfiguration().getWorkingDirectory()); |
43 | |
|
44 | 0 | if (factories != null) |
45 | |
{ |
46 | 0 | for (Iterator iterator = factories.entrySet().iterator(); iterator.hasNext();) |
47 | |
{ |
48 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
49 | 0 | if (entry.getKey() == null) |
50 | |
{ |
51 | 0 | throw new NullPointerException(CoreMessages.objectIsNull("Factories.Key").getMessage()); |
52 | |
} |
53 | 0 | if (entry.getValue() == null) |
54 | |
{ |
55 | 0 | throw new NullPointerException(CoreMessages.objectIsNull("Factories.Value").getMessage()); |
56 | |
} |
57 | |
try |
58 | |
{ |
59 | 0 | props.put(entry.getKey(), ((PropertyFactory) entry.getValue()).create(props)); |
60 | |
} |
61 | 0 | catch (Exception e) |
62 | |
{ |
63 | 0 | throw new IOException("Failed to invoke PropertyFactory: " + entry.getValue() + ". Error is: " + e.toString()); |
64 | 0 | } |
65 | 0 | } |
66 | |
} |
67 | 0 | return props; |
68 | |
} |
69 | |
|
70 | |
public Map getFactories() |
71 | |
{ |
72 | 0 | return factories; |
73 | |
} |
74 | |
|
75 | |
public void setFactories(Map factories) |
76 | |
{ |
77 | 0 | this.factories = factories; |
78 | 0 | } |
79 | |
|
80 | 0 | private class RegistryProperties extends Properties |
81 | |
{ |
82 | |
public String getProperty(String key) |
83 | |
{ |
84 | 0 | Object oval = super.get(key); |
85 | 0 | if (oval == null) |
86 | |
{ |
87 | 0 | oval = RegistryContext.getRegistry().lookupObject(key); |
88 | |
} |
89 | 0 | String sval = (oval instanceof String) ? (String)oval : null; |
90 | 0 | return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
public void setMuleContext(MuleContext muleContext) |
95 | |
{ |
96 | 0 | this.muleContext = muleContext; |
97 | |
|
98 | 0 | } |
99 | |
|
100 | |
} |