1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleMessage; |
15 | |
import org.mule.api.endpoint.OutboundEndpoint; |
16 | |
|
17 | |
import java.util.HashMap; |
18 | |
import java.util.Iterator; |
19 | |
import java.util.LinkedList; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 4 | public class FilteringListMessageSplitter extends AbstractMessageSplitter |
29 | |
{ |
30 | 4 | private final ThreadLocal payloadContext = new ThreadLocal(); |
31 | 4 | private final ThreadLocal propertiesContext = new ThreadLocal(); |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
protected void initialise(MuleMessage message) |
41 | |
{ |
42 | 6 | if (message.getPayload() instanceof List) |
43 | |
{ |
44 | |
|
45 | 6 | List payload = new LinkedList((List) message.getPayload()); |
46 | 6 | payloadContext.set(payload); |
47 | |
|
48 | 6 | if (enableCorrelation != ENABLE_CORRELATION_NEVER) |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | 6 | final int groupSize = payload.size(); |
54 | 6 | message.setCorrelationGroupSize(groupSize); |
55 | 6 | if (logger.isDebugEnabled()) |
56 | |
{ |
57 | 0 | logger.debug("java.util.List payload detected, setting correlation group size to " |
58 | |
+ groupSize); |
59 | |
} |
60 | |
} |
61 | 6 | } |
62 | |
else |
63 | |
{ |
64 | 0 | throw new IllegalArgumentException("The payload for this router must be of type java.util.List"); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | 6 | Map props = new HashMap(); |
70 | 6 | for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) |
71 | |
{ |
72 | 6 | String propertyKey = (String) iterator.next(); |
73 | 6 | props.put(propertyKey, message.getProperty(propertyKey)); |
74 | 6 | } |
75 | |
|
76 | 6 | propertiesContext.set(props); |
77 | 6 | } |
78 | |
|
79 | |
|
80 | |
protected void cleanup() |
81 | |
{ |
82 | 6 | payloadContext.set(null); |
83 | 6 | propertiesContext.set(null); |
84 | 6 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
protected MuleMessage getMessagePart(MuleMessage message, OutboundEndpoint endpoint) |
90 | |
{ |
91 | 38 | List payloads = (List) payloadContext.get(); |
92 | |
|
93 | 38 | for (Iterator i = payloads.iterator(); i.hasNext();) |
94 | |
{ |
95 | 36 | Object payload = i.next(); |
96 | 36 | MuleMessage result = new DefaultMuleMessage(payload, (Map) propertiesContext.get()); |
97 | |
|
98 | |
|
99 | |
|
100 | 36 | if (endpoint.getFilter() == null || endpoint.getFilter().accept(result)) |
101 | |
{ |
102 | 24 | if (logger.isDebugEnabled()) |
103 | |
{ |
104 | 0 | logger.debug("Endpoint filter matched. Routing message over: " |
105 | |
+ endpoint.getEndpointURI().toString()); |
106 | |
} |
107 | 24 | i.remove(); |
108 | 24 | return result; |
109 | |
} |
110 | 12 | } |
111 | |
|
112 | 14 | return null; |
113 | |
} |
114 | |
} |