1 /* 2 * $Id: ResequenceCorrelatorCallback.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 package org.mule.routing.correlation; 11 12 import org.mule.DefaultMuleEvent; 13 import org.mule.DefaultMuleMessage; 14 import org.mule.api.MuleContext; 15 import org.mule.api.MuleEvent; 16 import org.mule.routing.AggregationException; 17 import org.mule.routing.EventGroup; 18 19 import java.util.Arrays; 20 import java.util.Comparator; 21 22 /** 23 * A Correlator that correlates messages based on Mule correlation settings 24 * Note that the {@link #aggregateEvents(org.mule.routing.EventGroup)} method only resequences the events and 25 * returns an MuleEvent[] wrapped in a MuleMessage impl. This means that this callback can ONLY be used with a 26 * {@link org.mule.routing.inbound.CorrelationEventResequencer} 27 */ 28 public class ResequenceCorrelatorCallback extends CollectionCorrelatorCallback 29 { 30 protected Comparator<MuleEvent> eventComparator; 31 32 public ResequenceCorrelatorCallback(Comparator<MuleEvent> eventComparator, MuleContext muleContext) 33 { 34 super(muleContext); 35 this.eventComparator = eventComparator; 36 this.muleContext = muleContext; 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 AggregationException 47 * if the aggregation fails. in this scenario the 48 * whole event group is removed and passed to the exception handler 49 * for this componenet 50 */ 51 @Override 52 public MuleEvent aggregateEvents(EventGroup events) throws AggregationException 53 { 54 MuleEvent results[]; 55 if (events == null || events.size() == 0) 56 { 57 results = EventGroup.EMPTY_EVENTS_ARRAY; 58 } 59 else 60 { 61 results = events.toArray(); 62 Arrays.sort(results, eventComparator); 63 } 64 //This is a bit of a hack since we wrap the the collection of events in a Mule Message to pass back 65 return new DefaultMuleEvent(new DefaultMuleMessage(results, muleContext), results[0]); 66 } 67 68 }