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 public class PropertyPlaceholderProcessor extends PropertyPlaceholderConfigurer implements MuleContextAware
30 {
31 private MuleContext muleContext;
32 private Map factories = new HashMap();
33
34
35
36 protected Properties mergeProperties() throws IOException
37 {
38 RegistryProperties props = new RegistryProperties();
39 props.putAll(super.mergeProperties());
40
41
42 props.put("mule.working.dir", muleContext.getConfiguration().getWorkingDirectory());
43
44 if (factories != null)
45 {
46 for (Iterator iterator = factories.entrySet().iterator(); iterator.hasNext();)
47 {
48 Map.Entry entry = (Map.Entry) iterator.next();
49 if (entry.getKey() == null)
50 {
51 throw new NullPointerException(CoreMessages.objectIsNull("Factories.Key").getMessage());
52 }
53 if (entry.getValue() == null)
54 {
55 throw new NullPointerException(CoreMessages.objectIsNull("Factories.Value").getMessage());
56 }
57 try
58 {
59 props.put(entry.getKey(), ((PropertyFactory) entry.getValue()).create(props));
60 }
61 catch (Exception e)
62 {
63 throw new IOException("Failed to invoke PropertyFactory: " + entry.getValue() + ". Error is: " + e.toString());
64 }
65 }
66 }
67 return props;
68 }
69
70 public Map getFactories()
71 {
72 return factories;
73 }
74
75 public void setFactories(Map factories)
76 {
77 this.factories = factories;
78 }
79
80 private class RegistryProperties extends Properties
81 {
82 public String getProperty(String key)
83 {
84 Object oval = super.get(key);
85 if (oval == null)
86 {
87 oval = RegistryContext.getRegistry().lookupObject(key);
88 }
89 String sval = (oval instanceof String) ? (String)oval : null;
90 return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
91 }
92 }
93
94 public void setMuleContext(MuleContext muleContext)
95 {
96 this.muleContext = muleContext;
97
98 }
99
100 }