View Javadoc

1   /*
2    * $Id: MessageChunkingAggregator.java 11531 2008-04-08 15:34:34Z rossmason $
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 CPAL v1.0
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.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      protected final Comparator eventComparator = new CorrelationSequenceComparator();
35  
36      public MessageChunkingAggregator()
37      {
38          super();
39      }
40  
41      protected EventCorrelatorCallback getCorrelatorCallback()
42      {
43          return new CollectionCorrelatorCallback()
44          {
45              /**
46               * This method is invoked if the shouldAggregate method is called and returns
47               * true. Once this method returns an aggregated message the event group is
48               * removed from the router
49               *
50               * @param events the event group for this request
51               * @return an aggregated message
52               * @throws org.mule.routing.AggregationException if the aggregation fails. in
53               *             this scenario the whole event group is removed and passed to the
54               *             exception handler for this componenet
55               */
56              public MuleMessage aggregateEvents(EventGroup events) throws AggregationException
57              {
58                  MuleEvent[] collectedEvents = events.toArray();
59                  MuleEvent firstEvent = collectedEvents[0];
60                  Arrays.sort(collectedEvents, eventComparator);
61                  ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
62  
63                  try
64                  {
65                      for (Iterator iterator = IteratorUtils.arrayIterator(collectedEvents); iterator.hasNext();)
66                      {
67                          MuleEvent event = (MuleEvent) iterator.next();
68                          baos.write(event.getMessageAsBytes());
69                      }
70  
71                      MuleMessage message;
72  
73                      // try to deserialize message, since ChunkingRouter might have serialized
74                      // the object...
75                      try
76                      {
77                          message = new DefaultMuleMessage(SerializationUtils.deserialize(baos.toByteArray()),
78                                  firstEvent.getMessage());
79  
80                      }
81                      catch (SerializationException e)
82                      {
83                          message = new DefaultMuleMessage(baos.toByteArray(), firstEvent.getMessage());
84                      }
85  
86                      message.setCorrelationGroupSize(-1);
87                      message.setCorrelationSequence(-1);
88  
89                      return message;
90                  }
91                  catch (Exception e)
92                  {
93                      throw new AggregationException(events, firstEvent.getEndpoint(), e);
94                  }
95                  finally
96                  {
97                      IOUtils.closeQuietly(baos);
98                  }
99              }
100         };
101     }
102 
103 }