1
2
3
4
5
6
7
8
9
10 package org.mule.transport;
11
12 import org.mule.api.transport.PropertyScope;
13 import org.mule.util.MapUtils;
14 import org.mule.util.ObjectUtils;
15
16 import java.io.Serializable;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.Set;
22 import java.util.TreeMap;
23 import java.util.TreeSet;
24
25 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
26
27
28 public class MessagePropertiesContext implements Serializable
29 {
30 protected Map scopedMap;
31 protected Set keySet;
32
33
34
35 Map applicationProperties = new ConcurrentHashMap(0);
36
37 private PropertyScope defaultScope = PropertyScope.OUTBOUND;
38
39
40
41
42
43 private boolean fallbackToRegistry = false;
44
45 public MessagePropertiesContext()
46 {
47 keySet = new TreeSet();
48 scopedMap = new TreeMap(new PropertyScope.ScopeComparator());
49
50 scopedMap.put(PropertyScope.INVOCATION, new HashMap(6));
51 scopedMap.put(PropertyScope.INBOUND, new HashMap(6));
52 scopedMap.put(PropertyScope.OUTBOUND, new HashMap(6));
53 scopedMap.put(PropertyScope.SESSION, new HashMap(6));
54
55 }
56
57 public MessagePropertiesContext(PropertyScope defaultScope)
58 {
59 this();
60
61 checkScopeForWriteAccess(defaultScope);
62 this.defaultScope = defaultScope;
63 }
64
65 protected Map getScopedProperties(PropertyScope scope)
66 {
67 Map map = (Map) scopedMap.get(scope);
68 if (map == null)
69 {
70 throw new IllegalArgumentException("Scope not registered: " + scope);
71 }
72 return map;
73 }
74
75 void registerInvocationProperties(Map properties)
76 {
77 if (properties != null)
78 {
79 getScopedProperties(PropertyScope.INVOCATION).putAll(properties);
80 keySet.addAll(properties.keySet());
81 }
82 }
83
84 public PropertyScope getDefaultScope()
85 {
86 return defaultScope;
87 }
88
89 void addInboundProperties(Map properties)
90 {
91 if (properties != null)
92 {
93 getScopedProperties(PropertyScope.INBOUND).putAll(properties);
94 keySet.addAll(properties.keySet());
95 }
96 }
97
98 void registerSessionProperties(Map properties)
99 {
100 if (properties != null)
101 {
102 getScopedProperties(PropertyScope.SESSION).putAll(properties);
103 keySet.addAll(properties.keySet());
104 }
105 }
106
107
108 public Object getProperty(String key)
109 {
110 Object value = null;
111 for (Iterator iterator = scopedMap.values().iterator(); iterator.hasNext();)
112 {
113 Map props = (Map) iterator.next();
114 value = props.get(key);
115 if (value != null)
116 {
117 break;
118 }
119 }
120 if (value == null && fallbackToRegistry)
121 {
122 value = applicationProperties.get(key);
123 }
124 return value;
125 }
126
127 public Object getProperty(String key, PropertyScope scope)
128 {
129 if (PropertyScope.APPLICATION.equals(scope))
130 {
131 return applicationProperties.get(key);
132 }
133
134 Map props = getScopedProperties(scope);
135 return props.get(key);
136 }
137
138 public void clearProperties()
139 {
140 Map props = getScopedProperties(PropertyScope.INVOCATION);
141 keySet.removeAll(props.keySet());
142 props.clear();
143 props = getScopedProperties(PropertyScope.OUTBOUND);
144 keySet.removeAll(props.keySet());
145 props.clear();
146 props = getScopedProperties(PropertyScope.SESSION);
147 keySet.removeAll(props.keySet());
148 props.clear();
149
150 }
151
152 public void clearProperties(PropertyScope scope)
153 {
154 checkScopeForWriteAccess(scope);
155 Map props = getScopedProperties(scope);
156 keySet.removeAll(props.keySet());
157 props.clear();
158 }
159
160
161
162
163
164
165
166 public Object removeProperty(String key)
167 {
168 Object value = getScopedProperties(PropertyScope.INVOCATION).remove(key);
169 if (value == null)
170 {
171 value = getScopedProperties(PropertyScope.OUTBOUND).remove(key);
172 }
173 if (value == null)
174 {
175 value = getScopedProperties(PropertyScope.SESSION).remove(key);
176 }
177 if (value != null)
178 {
179 keySet.remove(key);
180 }
181 return value;
182 }
183
184
185
186
187
188
189
190 public void setProperty(String key, Object value)
191 {
192 getScopedProperties(defaultScope).put(key, value);
193 keySet.add(key);
194 }
195
196
197
198
199
200
201
202
203
204 public void setProperty(String key, Object value, PropertyScope scope)
205 {
206 checkScopeForWriteAccess(scope);
207 getScopedProperties(scope).put(key, value);
208 keySet.add(key);
209 }
210
211
212 public Set getPropertyNames()
213 {
214 return Collections.unmodifiableSet(keySet);
215 }
216
217
218 public Set getPropertyNames(PropertyScope scope)
219 {
220 return Collections.unmodifiableSet(getScopedProperties(scope).keySet());
221 }
222
223 protected void checkScopeForWriteAccess(PropertyScope scope)
224 {
225 if (scope == null || PropertyScope.INBOUND.equals(scope) || PropertyScope.APPLICATION.equals(scope))
226 {
227 throw new IllegalArgumentException("Scope is invalid for writing properties: " + scope);
228 }
229 }
230
231
232 public Object getProperty(String key, Object defaultValue)
233 {
234 Object value = getProperty(key);
235 if (value == null)
236 {
237 value = defaultValue;
238 }
239 return value;
240 }
241
242 public byte getByteProperty(String name, byte defaultValue)
243 {
244 return ObjectUtils.getByte(getProperty(name), defaultValue);
245 }
246
247 public short getShortProperty(String name, short defaultValue)
248 {
249 return ObjectUtils.getShort(getProperty(name), defaultValue);
250 }
251
252 public int getIntProperty(String name, int defaultValue)
253 {
254 return ObjectUtils.getInt(getProperty(name), defaultValue);
255 }
256
257 public long getLongProperty(String name, long defaultValue)
258 {
259 return ObjectUtils.getLong(getProperty(name), defaultValue);
260 }
261
262 public float getFloatProperty(String name, float defaultValue)
263 {
264 return ObjectUtils.getFloat(getProperty(name), defaultValue);
265 }
266
267 public double getDoubleProperty(String name, double defaultValue)
268 {
269 return ObjectUtils.getDouble(getProperty(name), defaultValue);
270 }
271
272 public boolean getBooleanProperty(String name, boolean defaultValue)
273 {
274 return ObjectUtils.getBoolean(getProperty(name), defaultValue);
275 }
276
277 public String getStringProperty(String name, String defaultValue)
278 {
279 return ObjectUtils.getString(getProperty(name), defaultValue);
280 }
281
282 public String toString()
283 {
284 StringBuffer buf = new StringBuffer(128);
285 buf.append("Properites{");
286 for (Iterator iterator = scopedMap.entrySet().iterator(); iterator.hasNext();)
287 {
288 Map.Entry entry = (Map.Entry) iterator.next();
289 buf.append(entry.getKey()).append(":");
290 buf.append(MapUtils.toString((Map) entry.getValue(), false));
291 buf.append(", ");
292 }
293 buf.append("}");
294 return buf.toString();
295 }
296 }