1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleSession; |
11 | |
import org.mule.api.transport.PropertyScope; |
12 | |
import org.mule.util.CaseInsensitiveHashMap; |
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.HashSet; |
20 | |
import java.util.Map; |
21 | |
import java.util.Set; |
22 | |
import java.util.TreeMap; |
23 | |
import java.util.TreeSet; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class MessagePropertiesContext implements Serializable |
44 | |
{ |
45 | |
private static final long serialVersionUID = -5230693402768953742L; |
46 | 0 | private static final PropertyScope DEFAULT_SCOPE = PropertyScope.OUTBOUND; |
47 | |
|
48 | 0 | private static Log logger = LogFactory.getLog(MessagePropertiesContext.class); |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
protected Map<PropertyScope, Map<String, Object>> scopedMap; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
protected Set<String> keySet; |
59 | |
|
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | |
public MessagePropertiesContext() |
63 | 0 | { |
64 | 0 | keySet = new TreeSet<String>(); |
65 | 0 | scopedMap = new TreeMap<PropertyScope, Map<String, Object>>(new PropertyScope.ScopeComparator()); |
66 | |
|
67 | 0 | scopedMap.put(PropertyScope.INVOCATION, new CaseInsensitiveHashMap(6)); |
68 | 0 | scopedMap.put(PropertyScope.INBOUND, new CaseInsensitiveHashMap(6)); |
69 | 0 | scopedMap.put(PropertyScope.OUTBOUND, new CaseInsensitiveHashMap(6)); |
70 | 0 | } |
71 | |
|
72 | |
protected Map<String, Object> getScopedProperties(PropertyScope scope) |
73 | |
{ |
74 | 0 | Map<String, Object> map = scopedMap.get(scope); |
75 | 0 | if (map == null) |
76 | |
{ |
77 | 0 | throw new IllegalArgumentException("Scope not registered: " + scope); |
78 | |
} |
79 | 0 | return map; |
80 | |
} |
81 | |
|
82 | |
public PropertyScope getDefaultScope() |
83 | |
{ |
84 | 0 | return DEFAULT_SCOPE; |
85 | |
} |
86 | |
|
87 | |
protected void addInboundProperties(Map<String, Object> properties) |
88 | |
{ |
89 | 0 | if (properties != null) |
90 | |
{ |
91 | 0 | Map<String, Object> props = new HashMap<String, Object>(properties.size()); |
92 | 0 | for (String key : properties.keySet()) |
93 | |
{ |
94 | 0 | props.put(key, properties.get(key)); |
95 | |
} |
96 | 0 | getScopedProperties(PropertyScope.INBOUND).putAll(props); |
97 | 0 | keySet.addAll(props.keySet()); |
98 | |
} |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Deprecated |
107 | |
public Object getProperty(String key) |
108 | |
{ |
109 | 0 | return getProperty(key, PropertyScope.OUTBOUND); |
110 | |
} |
111 | |
|
112 | |
@SuppressWarnings("unchecked") |
113 | |
public <T> T getProperty(String key, PropertyScope scope) |
114 | |
{ |
115 | 0 | if (scope == null) |
116 | |
{ |
117 | 0 | scope = PropertyScope.OUTBOUND; |
118 | |
} |
119 | |
|
120 | 0 | T value = null; |
121 | 0 | if (PropertyScope.SESSION.equals(scope)) |
122 | |
{ |
123 | 0 | if (RequestContext.getEvent() != null) |
124 | |
{ |
125 | 0 | value = (T) RequestContext.getEvent().getSession().getProperty(key); |
126 | |
} |
127 | |
} |
128 | |
else |
129 | |
{ |
130 | 0 | value = (T) scopedMap.get(scope).get(key); |
131 | |
} |
132 | 0 | return value; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
public void clearProperties() |
140 | |
{ |
141 | 0 | Map<String, Object> props = getScopedProperties(PropertyScope.INVOCATION); |
142 | 0 | keySet.removeAll(props.keySet()); |
143 | 0 | props.clear(); |
144 | 0 | props = getScopedProperties(PropertyScope.OUTBOUND); |
145 | 0 | keySet.removeAll(props.keySet()); |
146 | 0 | props.clear(); |
147 | 0 | } |
148 | |
|
149 | |
public void clearProperties(PropertyScope scope) |
150 | |
{ |
151 | 0 | if (scope == null) |
152 | |
{ |
153 | 0 | clearProperties(); |
154 | 0 | return; |
155 | |
} |
156 | |
|
157 | |
|
158 | 0 | if (PropertyScope.SESSION.equals(scope)) |
159 | |
{ |
160 | 0 | if (RequestContext.getEvent() != null) |
161 | |
{ |
162 | 0 | MuleSession session = RequestContext.getEvent().getSession(); |
163 | 0 | for (Object key : session.getPropertyNamesAsSet()) |
164 | |
{ |
165 | 0 | session.removeProperty(key); |
166 | |
} |
167 | 0 | } |
168 | |
} |
169 | |
else |
170 | |
{ |
171 | 0 | Map<String, Object> props = getScopedProperties(scope); |
172 | 0 | keySet.removeAll(props.keySet()); |
173 | 0 | props.clear(); |
174 | |
} |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public Object removeProperty(String key) |
185 | |
{ |
186 | 0 | Object value = getScopedProperties(PropertyScope.OUTBOUND).remove(key); |
187 | 0 | Object inv = getScopedProperties(PropertyScope.INVOCATION).remove(key); |
188 | |
|
189 | 0 | keySet.remove(key); |
190 | |
|
191 | 0 | if (value == null) |
192 | |
{ |
193 | 0 | value = inv; |
194 | |
} |
195 | |
|
196 | 0 | return value; |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public Object removeProperty(String key, PropertyScope scope) |
206 | |
{ |
207 | 0 | if (scope == null) |
208 | |
{ |
209 | 0 | return removeProperty(key); |
210 | |
} |
211 | |
|
212 | 0 | Object value = null; |
213 | 0 | if (PropertyScope.SESSION.equals(scope)) |
214 | |
{ |
215 | 0 | if (RequestContext.getEvent() != null) |
216 | |
{ |
217 | 0 | value = RequestContext.getEvent().getSession().removeProperty(key); |
218 | |
} |
219 | |
} |
220 | |
else |
221 | |
{ |
222 | 0 | value = getScopedProperties(scope).remove(key); |
223 | |
} |
224 | |
|
225 | |
|
226 | 0 | if (getProperty(key, PropertyScope.OUTBOUND) == null |
227 | |
&& getProperty(key, PropertyScope.INVOCATION) == null |
228 | |
&& getProperty(key, PropertyScope.INBOUND) == null) |
229 | |
{ |
230 | 0 | keySet.remove(key); |
231 | |
} |
232 | |
|
233 | 0 | return value; |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
@Deprecated |
244 | |
public void setProperty(String key, Object value) |
245 | |
{ |
246 | 0 | getScopedProperties(DEFAULT_SCOPE).put(key, value); |
247 | 0 | keySet.add(key); |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public void setProperty(String key, Object value, PropertyScope scope) |
259 | |
{ |
260 | |
|
261 | 0 | if (PropertyScope.SESSION.equals(scope)) |
262 | |
{ |
263 | 0 | if (RequestContext.getEvent() != null) |
264 | |
{ |
265 | 0 | RequestContext.getEvent().getSession().setProperty(key, value); |
266 | |
} |
267 | |
else |
268 | |
{ |
269 | 0 | logger.warn(String.format("Detected an attempt to set a session property, " + |
270 | |
"but MuleEvent isn't available in this thread. Key/value: %s=%s %s", |
271 | |
key, value, Thread.currentThread())); |
272 | |
} |
273 | |
} |
274 | |
else |
275 | |
{ |
276 | 0 | getScopedProperties(scope).put(key, value); |
277 | 0 | keySet.add(key); |
278 | |
} |
279 | 0 | } |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
@Deprecated |
285 | |
public Set<String> getPropertyNames() |
286 | |
{ |
287 | 0 | Set<String> allProps = new HashSet<String>(); |
288 | 0 | allProps.addAll(keySet); |
289 | 0 | if (RequestContext.getEvent() != null) |
290 | |
{ |
291 | 0 | allProps.addAll(RequestContext.getEvent().getSession().getPropertyNamesAsSet()); |
292 | |
} |
293 | 0 | return allProps; |
294 | |
} |
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
public Set<String> getPropertyNames(PropertyScope scope) |
300 | |
{ |
301 | 0 | if (PropertyScope.SESSION.equals(scope)) |
302 | |
{ |
303 | 0 | if (RequestContext.getEvent() != null) |
304 | |
{ |
305 | 0 | return RequestContext.getEvent().getSession().getPropertyNamesAsSet(); |
306 | |
} |
307 | |
else |
308 | |
{ |
309 | 0 | return Collections.emptySet(); |
310 | |
} |
311 | |
} |
312 | |
else |
313 | |
{ |
314 | 0 | return Collections.unmodifiableSet(getScopedProperties(scope).keySet()); |
315 | |
} |
316 | |
} |
317 | |
|
318 | |
protected void checkScopeForWriteAccess(PropertyScope scope) |
319 | |
{ |
320 | 0 | if (scope == null || PropertyScope.INBOUND.equals(scope)) |
321 | |
{ |
322 | 0 | throw new IllegalArgumentException("Scope is invalid for writing properties: " + scope); |
323 | |
} |
324 | 0 | } |
325 | |
|
326 | |
public Object getProperty(String key, Object defaultValue) |
327 | |
{ |
328 | 0 | Object value = getProperty(key); |
329 | 0 | if (value == null) |
330 | |
{ |
331 | 0 | value = defaultValue; |
332 | |
} |
333 | 0 | return value; |
334 | |
} |
335 | |
|
336 | |
public byte getByteProperty(String name, byte defaultValue) |
337 | |
{ |
338 | 0 | return ObjectUtils.getByte(getProperty(name), defaultValue); |
339 | |
} |
340 | |
|
341 | |
public short getShortProperty(String name, short defaultValue) |
342 | |
{ |
343 | 0 | return ObjectUtils.getShort(getProperty(name), defaultValue); |
344 | |
} |
345 | |
|
346 | |
public int getIntProperty(String name, int defaultValue) |
347 | |
{ |
348 | 0 | return ObjectUtils.getInt(getProperty(name), defaultValue); |
349 | |
} |
350 | |
|
351 | |
public long getLongProperty(String name, long defaultValue) |
352 | |
{ |
353 | 0 | return ObjectUtils.getLong(getProperty(name), defaultValue); |
354 | |
} |
355 | |
|
356 | |
public float getFloatProperty(String name, float defaultValue) |
357 | |
{ |
358 | 0 | return ObjectUtils.getFloat(getProperty(name), defaultValue); |
359 | |
} |
360 | |
|
361 | |
public double getDoubleProperty(String name, double defaultValue) |
362 | |
{ |
363 | 0 | return ObjectUtils.getDouble(getProperty(name), defaultValue); |
364 | |
} |
365 | |
|
366 | |
public boolean getBooleanProperty(String name, boolean defaultValue) |
367 | |
{ |
368 | 0 | return ObjectUtils.getBoolean(getProperty(name), defaultValue); |
369 | |
} |
370 | |
|
371 | |
@Deprecated |
372 | |
public String getStringProperty(String name, String defaultValue) |
373 | |
{ |
374 | 0 | return getStringProperty(name, PropertyScope.OUTBOUND, defaultValue); |
375 | |
} |
376 | |
|
377 | |
public String getStringProperty(String name, PropertyScope scope, String defaultValue) |
378 | |
{ |
379 | 0 | return ObjectUtils.getString(getProperty(name, scope), defaultValue); |
380 | |
} |
381 | |
|
382 | |
@Override |
383 | |
public String toString() |
384 | |
{ |
385 | 0 | StringBuffer buf = new StringBuffer(128); |
386 | 0 | buf.append("Properties{"); |
387 | 0 | for (Map.Entry<PropertyScope, Map<String, Object>> entry : scopedMap.entrySet()) |
388 | |
{ |
389 | 0 | buf.append(entry.getKey()).append(":"); |
390 | 0 | buf.append(MapUtils.toString(entry.getValue(), false)); |
391 | 0 | buf.append(", "); |
392 | |
} |
393 | 0 | buf.append("}"); |
394 | 0 | return buf.toString(); |
395 | |
} |
396 | |
} |