View Javadoc

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