Coverage Report - org.mule.routing.inbound.MessageChunkingAggregator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageChunkingAggregator
0%
0/20
0%
0/1
3.5
 
 1  
 /*
 2  
  * $Id: MessageChunkingAggregator.java 7976 2007-08-21 14:26:13Z 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  0
     protected final Comparator eventComparator = new CorrelationSequenceComparator();
 33  
 
 34  
     public MessageChunkingAggregator()
 35  
     {
 36  0
         super();
 37  0
     }
 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  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  
             }
 64  
 
 65  
             UMOMessage message;
 66  
 
 67  
             // try to deserialize message, since ChunkingRouter might have serialized
 68  
             // the object...
 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  
 }