1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.inbound; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.routing.AggregationException; |
17 | |
import org.mule.routing.EventCorrelatorCallback; |
18 | |
import org.mule.routing.CollectionCorrelatorCallback; |
19 | |
|
20 | |
import java.util.Arrays; |
21 | |
import java.util.Comparator; |
22 | |
import java.util.Iterator; |
23 | |
|
24 | |
import org.apache.commons.collections.IteratorUtils; |
25 | |
import org.apache.commons.io.IOUtils; |
26 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
27 | |
import org.apache.commons.lang.SerializationException; |
28 | |
import org.apache.commons.lang.SerializationUtils; |
29 | |
|
30 | |
public class MessageChunkingAggregator extends AbstractEventAggregator |
31 | |
{ |
32 | |
public static final int DEFAULT_BUFFER_SIZE = 4096; |
33 | |
|
34 | 0 | protected final Comparator eventComparator = new CorrelationSequenceComparator(); |
35 | |
|
36 | |
public MessageChunkingAggregator() |
37 | |
{ |
38 | 0 | super(); |
39 | 0 | } |
40 | |
|
41 | |
protected EventCorrelatorCallback getCorrelatorCallback() |
42 | |
{ |
43 | 0 | return new CollectionCorrelatorCallback() |
44 | |
{ |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | public MuleMessage aggregateEvents(EventGroup events) throws AggregationException |
57 | |
{ |
58 | 0 | MuleEvent[] collectedEvents = events.toArray(); |
59 | 0 | MuleEvent firstEvent = collectedEvents[0]; |
60 | 0 | Arrays.sort(collectedEvents, eventComparator); |
61 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE); |
62 | |
|
63 | |
try |
64 | |
{ |
65 | 0 | for (Iterator iterator = IteratorUtils.arrayIterator(collectedEvents); iterator.hasNext();) |
66 | |
{ |
67 | 0 | MuleEvent event = (MuleEvent) iterator.next(); |
68 | 0 | baos.write(event.getMessageAsBytes()); |
69 | 0 | } |
70 | |
|
71 | |
MuleMessage message; |
72 | |
|
73 | |
|
74 | |
|
75 | |
try |
76 | |
{ |
77 | 0 | message = new DefaultMuleMessage(SerializationUtils.deserialize(baos.toByteArray()), |
78 | |
firstEvent.getMessage()); |
79 | |
|
80 | |
} |
81 | 0 | catch (SerializationException e) |
82 | |
{ |
83 | 0 | message = new DefaultMuleMessage(baos.toByteArray(), firstEvent.getMessage()); |
84 | 0 | } |
85 | |
|
86 | 0 | message.setCorrelationGroupSize(-1); |
87 | 0 | message.setCorrelationSequence(-1); |
88 | |
|
89 | 0 | return message; |
90 | |
} |
91 | 0 | catch (Exception e) |
92 | |
{ |
93 | 0 | throw new AggregationException(events, firstEvent.getEndpoint(), e); |
94 | |
} |
95 | |
finally |
96 | |
{ |
97 | 0 | IOUtils.closeQuietly(baos); |
98 | |
} |
99 | |
} |
100 | |
}; |
101 | |
} |
102 | |
|
103 | |
} |