View Javadoc

1   /*
2    * $Id: ResequenceCorrelatorCallback.java 22506 2011-07-21 16:30:40Z stephen.fenech $
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.correlation;
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.store.ObjectStoreException;
18  import org.mule.routing.AggregationException;
19  import org.mule.routing.EventGroup;
20  import org.mule.routing.Resequencer;
21  
22  import java.util.Arrays;
23  import java.util.Comparator;
24  
25  /**
26   * A Correlator that correlates messages based on Mule correlation settings. Note
27   * that the {@link #aggregateEvents(org.mule.routing.EventGroup)} method only
28   * resequences the events and returns an MuleEvent[] wrapped in a MuleMessage impl.
29   * This means that this callback can ONLY be used with a {@link Resequencer}
30   */
31  public class ResequenceCorrelatorCallback extends CollectionCorrelatorCallback
32  {
33      protected Comparator<MuleEvent> eventComparator;
34  
35      public ResequenceCorrelatorCallback(Comparator<MuleEvent> eventComparator,
36                                          MuleContext muleContext,
37                                          boolean persistentStores,
38                                          String storePrefix)
39      {
40          super(muleContext, persistentStores, storePrefix);
41          this.eventComparator = eventComparator;
42          this.muleContext = muleContext;
43      }
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 AggregationException if the aggregation fails. in this scenario the
53       *             whole event group is removed and passed to the exception handler
54       *             for this componenet
55       */
56      @Override
57      public MuleEvent aggregateEvents(EventGroup events) throws AggregationException
58      {
59          MuleEvent results[];
60          if (events == null || events.size() == 0)
61          {
62              results = EventGroup.EMPTY_EVENTS_ARRAY;
63          }
64          else
65          {
66              try
67              {
68                  results = events.toArray();
69              }
70              catch (ObjectStoreException e)
71              {
72                  throw new AggregationException(events, null, e);
73              }
74              Arrays.sort(results, eventComparator);
75          }
76          // This is a bit of a hack since we wrap the the collection of events in a
77          // Mule Message to pass back
78          return new DefaultMuleEvent(new DefaultMuleMessage(results, muleContext), results[0]);
79      }
80  
81  }