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