View Javadoc

1   /*
2    * $Id: ResequenceMessagesCorrelatorCallback.java 22833 2011-09-02 16:07:37Z dfeist $
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 java.util.Arrays;
14  import java.util.Comparator;
15  
16  import org.mule.DefaultMuleEvent;
17  import org.mule.DefaultMuleMessage;
18  import org.mule.api.MuleContext;
19  import org.mule.api.MuleEvent;
20  import org.mule.api.ThreadSafeAccess;
21  import org.mule.api.store.ObjectStoreException;
22  import org.mule.routing.AggregationException;
23  import org.mule.routing.EventGroup;
24  import org.mule.routing.Resequencer;
25  
26  /**
27   * A Correlator that correlates messages based on Mule correlation settings Note that
28   * the {@link #aggregateEvents(org.mule.routing.EventGroup)} method only resequences
29   * the events and returns an MuleEvent[] wrapped in a MuleMessage impl. This means
30   * that this callback can ONLY be used with a {@link Resequencer}
31   */
32  public class ResequenceMessagesCorrelatorCallback extends CollectionCorrelatorCallback
33  {
34      protected Comparator eventComparator;
35      protected MuleContext muleContext;
36  
37      public ResequenceMessagesCorrelatorCallback(Comparator eventComparator,
38                                                  MuleContext muleContext,
39                                                  boolean persistantStore,
40                                                  String storePrefix)
41      {
42          super(muleContext, persistantStore, storePrefix);
43          this.eventComparator = eventComparator;
44          this.muleContext = muleContext;
45      }
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[] results;
62          try
63          {
64              results = (events == null) ? new MuleEvent[0] : events.toArray();
65          }
66          catch (ObjectStoreException e)
67          {
68              throw new AggregationException(events, null, e);
69          }
70          Arrays.sort(results, eventComparator);
71          // This is a bit of a hack since we return a collection of events on one
72          // message
73          for (int i = 0; i < results.length; i++)
74          {
75              if (results[i] instanceof ThreadSafeAccess)
76              {
77                  results[i] = (MuleEvent)((ThreadSafeAccess)results[i]).newThreadCopy();
78              }
79          }
80          return new DefaultMuleEvent(new DefaultMuleMessage(results, muleContext), results[0]);
81      }
82  
83  }