Coverage Report - org.mule.routing.inbound.MessageChunkingAggregator
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageChunkingAggregator
0%
0/4
N/A
2.667
MessageChunkingAggregator$1
0%
0/19
0%
0/2
2.667
 
 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  0
     protected final Comparator eventComparator = new CorrelationSequenceComparator();
 35  
 
 36  
     public MessageChunkingAggregator()
 37  
     {
 38  0
         super();
 39  0
     }
 40  
 
 41  
     protected EventCorrelatorCallback getCorrelatorCallback()
 42  
     {
 43  0
         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  0
             public MuleMessage aggregateEvents(EventGroup events) throws AggregationException
 57  
             {
 58  0
                 MuleEvent[] collectedEvents = events.toArray();
 59  0
                 MuleEvent firstEvent = collectedEvents[0];
 60  0
                 Arrays.sort(collectedEvents, eventComparator);
 61  0
                 ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
 62  
 
 63  
                 try
 64  
                 {
 65  0
                     for (Iterator iterator = IteratorUtils.arrayIterator(collectedEvents); iterator.hasNext();)
 66  
                     {
 67  0
                         MuleEvent event = (MuleEvent) iterator.next();
 68  0
                         baos.write(event.getMessageAsBytes());
 69  0
                     }
 70  
 
 71  
                     MuleMessage message;
 72  
 
 73  
                     // try to deserialize message, since ChunkingRouter might have serialized
 74  
                     // the object...
 75  
                     try
 76  
                     {
 77  0
                         message = new DefaultMuleMessage(SerializationUtils.deserialize(baos.toByteArray()),
 78  
                                 firstEvent.getMessage());
 79  
 
 80  
                     }
 81  0
                     catch (SerializationException e)
 82  
                     {
 83  0
                         message = new DefaultMuleMessage(baos.toByteArray(), firstEvent.getMessage());
 84  0
                     }
 85  
 
 86  0
                     message.setCorrelationGroupSize(-1);
 87  0
                     message.setCorrelationSequence(-1);
 88  
 
 89  0
                     return message;
 90  
                 }
 91  0
                 catch (Exception e)
 92  
                 {
 93  0
                     throw new AggregationException(events, firstEvent.getEndpoint(), e);
 94  
                 }
 95  
                 finally
 96  
                 {
 97  0
                     IOUtils.closeQuietly(baos);
 98  
                 }
 99  
             }
 100  
         };
 101  
     }
 102  
 
 103  
 }