1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing; |
12 | |
|
13 | |
import org.mule.DefaultMessageCollection; |
14 | |
import org.mule.DefaultMuleEvent; |
15 | |
import org.mule.api.MuleEvent; |
16 | |
import org.mule.api.MuleMessageCollection; |
17 | |
import org.mule.util.ClassUtils; |
18 | |
|
19 | |
import java.io.Serializable; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.apache.commons.collections.IteratorUtils; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class EventGroup implements Comparable<EventGroup>, Serializable |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | |
private static final long serialVersionUID = 953739659615692697L; |
37 | |
|
38 | 0 | public static final MuleEvent[] EMPTY_EVENTS_ARRAY = new MuleEvent[0]; |
39 | |
|
40 | |
private final Object groupId; |
41 | |
|
42 | |
private final List<MuleEvent> events; |
43 | |
private final long created; |
44 | |
private final int expectedSize; |
45 | |
|
46 | |
public EventGroup(Object groupId) |
47 | |
{ |
48 | 0 | this(groupId, -1); |
49 | 0 | } |
50 | |
|
51 | |
public EventGroup(Object groupId, int expectedSize) |
52 | |
{ |
53 | 0 | super(); |
54 | 0 | this.created = System.nanoTime(); |
55 | 0 | this.events = new ArrayList<MuleEvent>(expectedSize > 0 ? expectedSize : 10); |
56 | 0 | this.expectedSize = expectedSize; |
57 | 0 | this.groupId = groupId; |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@SuppressWarnings("unchecked") |
69 | |
public int compareTo(EventGroup other) |
70 | |
{ |
71 | 0 | Object otherId = other.getGroupId(); |
72 | |
|
73 | 0 | if (groupId instanceof Comparable<?> && otherId instanceof Comparable<?>) |
74 | |
{ |
75 | 0 | return ((Comparable) groupId).compareTo(otherId); |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | 0 | long diff = created - other.getCreated(); |
80 | 0 | return (diff > 0 ? 1 : (diff < 0 ? -1 : 0)); |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
@Override |
91 | |
public boolean equals(Object obj) |
92 | |
{ |
93 | 0 | if (this == obj) |
94 | |
{ |
95 | 0 | return true; |
96 | |
} |
97 | |
|
98 | 0 | if (!(obj instanceof EventGroup)) |
99 | |
{ |
100 | 0 | return false; |
101 | |
} |
102 | |
|
103 | 0 | final EventGroup other = (EventGroup) obj; |
104 | 0 | if (groupId == null) |
105 | |
{ |
106 | 0 | return (other.groupId == null); |
107 | |
} |
108 | |
|
109 | 0 | return groupId.equals(other.groupId); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
@Override |
119 | |
public int hashCode() |
120 | |
{ |
121 | 0 | return groupId.hashCode(); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public Object getGroupId() |
131 | |
{ |
132 | 0 | return groupId; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
@SuppressWarnings("unchecked") |
145 | |
public Iterator<MuleEvent> iterator() |
146 | |
{ |
147 | 0 | synchronized (events) |
148 | |
{ |
149 | 0 | if (events.isEmpty()) |
150 | |
{ |
151 | 0 | return IteratorUtils.emptyIterator(); |
152 | |
} |
153 | |
else |
154 | |
{ |
155 | 0 | return IteratorUtils.arrayIterator(this.toArray()); |
156 | |
} |
157 | 0 | } |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public MuleEvent[] toArray() |
166 | |
{ |
167 | 0 | synchronized (events) |
168 | |
{ |
169 | 0 | if (events.isEmpty()) |
170 | |
{ |
171 | 0 | return EMPTY_EVENTS_ARRAY; |
172 | |
} |
173 | |
|
174 | 0 | return events.toArray(EMPTY_EVENTS_ARRAY); |
175 | 0 | } |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public void addEvent(MuleEvent event) |
184 | |
{ |
185 | 0 | synchronized (events) |
186 | |
{ |
187 | 0 | events.add(event); |
188 | 0 | } |
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public void removeEvent(MuleEvent event) |
197 | |
{ |
198 | 0 | synchronized (events) |
199 | |
{ |
200 | 0 | events.remove(event); |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public long getCreated() |
210 | |
{ |
211 | 0 | return created; |
212 | |
} |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public int size() |
220 | |
{ |
221 | 0 | synchronized (events) |
222 | |
{ |
223 | 0 | return events.size(); |
224 | 0 | } |
225 | |
} |
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public int expectedSize() |
234 | |
{ |
235 | 0 | return expectedSize; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
public void clear() |
242 | |
{ |
243 | 0 | synchronized (events) |
244 | |
{ |
245 | 0 | events.clear(); |
246 | 0 | } |
247 | 0 | } |
248 | |
|
249 | |
@Override |
250 | |
public String toString() |
251 | |
{ |
252 | 0 | StringBuffer buf = new StringBuffer(80); |
253 | 0 | buf.append(ClassUtils.getSimpleName(this.getClass())); |
254 | 0 | buf.append(" {"); |
255 | 0 | buf.append("id=").append(groupId); |
256 | 0 | buf.append(", expected size=").append(expectedSize); |
257 | |
|
258 | 0 | synchronized (events) |
259 | |
{ |
260 | 0 | int currentSize = events.size(); |
261 | 0 | buf.append(", current events=").append(currentSize); |
262 | |
|
263 | 0 | if (currentSize > 0) |
264 | |
{ |
265 | 0 | buf.append(" ["); |
266 | 0 | Iterator<MuleEvent> i = events.iterator(); |
267 | 0 | while (i.hasNext()) |
268 | |
{ |
269 | 0 | MuleEvent event = i.next(); |
270 | 0 | buf.append(event.getMessage().getUniqueId()); |
271 | 0 | if (i.hasNext()) |
272 | |
{ |
273 | 0 | buf.append(", "); |
274 | |
} |
275 | 0 | } |
276 | 0 | buf.append(']'); |
277 | |
} |
278 | 0 | } |
279 | |
|
280 | 0 | buf.append('}'); |
281 | |
|
282 | 0 | return buf.toString(); |
283 | |
} |
284 | |
|
285 | |
public MuleMessageCollection toMessageCollection() |
286 | |
{ |
287 | |
MuleMessageCollection col; |
288 | 0 | synchronized (events) |
289 | |
{ |
290 | 0 | if (events.isEmpty()) |
291 | |
{ |
292 | 0 | col = new DefaultMessageCollection(null); |
293 | |
} |
294 | 0 | col = new DefaultMessageCollection(events.get(0).getMuleContext()); |
295 | 0 | for (MuleEvent event : events) |
296 | |
{ |
297 | 0 | col.addMessage(event.getMessage()); |
298 | |
} |
299 | 0 | } |
300 | 0 | return col; |
301 | |
} |
302 | |
|
303 | |
public MuleEvent getMessageCollectionEvent() |
304 | |
{ |
305 | 0 | if (events.size() > 0) |
306 | |
{ |
307 | |
|
308 | 0 | return new DefaultMuleEvent(toMessageCollection(), events.get(0)); |
309 | |
} |
310 | |
else |
311 | |
{ |
312 | 0 | return null; |
313 | |
} |
314 | |
} |
315 | |
} |