KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > MessageChunkingAggregator


1 /*
2  * $Id: MessageChunkingAggregator.java 4323 2006-12-19 15:55:15Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
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.io.ByteArrayOutputStream JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Comparator JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.commons.collections.IteratorUtils;
25 import org.apache.commons.io.IOUtils;
26 import org.apache.commons.lang.SerializationException;
27 import org.apache.commons.lang.SerializationUtils;
28
29 /**
30  * todo document
31  *
32  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
33  * @version $Revision: 4323 $
34  */

35 public class MessageChunkingAggregator extends CorrelationAggregator
36 {
37
38     /**
39      * This method is invoked if the shouldAggregate method is called and returns
40      * true. Once this method returns an aggregated message the event group is
41      * removed from the router
42      *
43      * @param events the event group for this request
44      * @return an aggregated message
45      * @throws org.mule.routing.AggregationException if the aggregation fails. in
46      * this scenario the whole event group is removed and passed to the
47      * exception handler for this componenet
48      */

49     protected UMOMessage aggregateEvents(EventGroup events) throws AggregationException
50     {
51         List JavaDoc eventList = IteratorUtils.toList(events.iterator(), events.size());
52         UMOEvent firstEvent = (UMOEvent)eventList.get(0);
53         Collections.sort(eventList, SequenceComparator.getInstance());
54         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(4096);
55         try
56         {
57             for (Iterator JavaDoc iterator = eventList.iterator(); iterator.hasNext();)
58             {
59                 UMOEvent event = (UMOEvent)iterator.next();
60                 baos.write(event.getMessageAsBytes());
61             }
62             UMOMessage message;
63             // try to deserialize message, since ChunkingRouter might have serialized
64
// the object...
65
try
66             {
67                 message = new MuleMessage(SerializationUtils.deserialize(baos.toByteArray()),
68                     firstEvent.getMessage());
69
70             }
71             catch (SerializationException e)
72             {
73                 message = new MuleMessage(baos.toByteArray(), firstEvent.getMessage());
74             }
75             message.setCorrelationGroupSize(-1);
76             message.setCorrelationSequence(-1);
77             return message;
78         }
79         catch (Exception JavaDoc e)
80         {
81             throw new AggregationException(events, firstEvent.getEndpoint(), e);
82         }
83         finally
84         {
85             IOUtils.closeQuietly(baos);
86         }
87     }
88
89     public static class SequenceComparator implements Comparator JavaDoc
90     {
91         private static SequenceComparator _instance = new SequenceComparator();
92
93         public static SequenceComparator getInstance()
94         {
95             return _instance;
96         }
97
98         private SequenceComparator()
99         {
100             super();
101         }
102
103         public int compare(Object JavaDoc o1, Object JavaDoc o2)
104         {
105             UMOEvent event1 = (UMOEvent)o1;
106             UMOEvent event2 = (UMOEvent)o2;
107             if (event1.getMessage().getCorrelationSequence() > event2.getMessage()
108                 .getCorrelationSequence())
109             {
110                 return 1;
111             }
112             else
113             {
114                 return -1;
115             }
116         }
117     }
118 }
119
Popular Tags