View Javadoc

1   /*
2    * $Id: MessageChunkAggregator.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleContext;
16  import org.mule.api.MuleEvent;
17  import org.mule.api.MuleMessage;
18  import org.mule.routing.correlation.CollectionCorrelatorCallback;
19  import org.mule.routing.correlation.CorrelationSequenceComparator;
20  import org.mule.routing.correlation.EventCorrelatorCallback;
21  import org.mule.util.SerializationUtils;
22  
23  import java.util.Arrays;
24  import java.util.Comparator;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.io.output.ByteArrayOutputStream;
28  import org.apache.commons.lang.SerializationException;
29  
30  public class MessageChunkAggregator extends AbstractAggregator
31  {
32      public static final int DEFAULT_BUFFER_SIZE = 4096;
33  
34      protected Comparator eventComparator;
35  
36      public MessageChunkAggregator()
37      {
38          super();
39          eventComparator = new CorrelationSequenceComparator();
40      }
41  
42      @Override
43      protected EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext)
44      {
45          return new CollectionCorrelatorCallback(muleContext)
46          {
47              /**
48               * This method is invoked if the shouldAggregate method is called and returns
49               * true. Once this method returns an aggregated message the event group is
50               * removed from the router
51               *
52               * @param events the event group for this request
53               * @return an aggregated message
54               * @throws org.mule.routing.AggregationException if the aggregation fails. in
55               *             this scenario the whole event group is removed and passed to the
56               *             exception handler for this componenet
57               */
58              @Override
59              public MuleEvent aggregateEvents(EventGroup events) throws AggregationException
60              {
61                  MuleEvent[] collectedEvents = events.toArray();
62                  MuleEvent firstEvent = collectedEvents[0];
63                  Arrays.sort(collectedEvents, eventComparator);
64                  ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
65  
66                  try
67                  {
68                      for (MuleEvent event : collectedEvents)
69                      {
70                          baos.write(event.getMessageAsBytes());
71                      }
72  
73                      MuleMessage message;
74  
75                      // try to deserialize message, since ChunkingRouter might have serialized
76                      // the object...
77                      try
78                      {
79                          // must deserialize in correct classloader
80                          final Object deserialized = SerializationUtils.deserialize(baos.toByteArray(), muleContext.getExecutionClassLoader());
81                          message = new DefaultMuleMessage(deserialized, firstEvent.getMessage(), muleContext);
82  
83                      }
84                      catch (SerializationException e)
85                      {
86                          message = new DefaultMuleMessage(baos.toByteArray(), firstEvent.getMessage(), muleContext);
87                      }
88  
89                      message.setCorrelationGroupSize(-1);
90                      message.setCorrelationSequence(-1);
91  
92                      return new DefaultMuleEvent(message, firstEvent);
93                  }
94                  catch (Exception e)
95                  {
96                      throw new AggregationException(events,MessageChunkAggregator.this, e);
97                  }
98                  finally
99                  {
100                     IOUtils.closeQuietly(baos);
101                 }
102             }
103         };
104     }
105 
106 }