View Javadoc

1   /*
2    * $Id: CollectionCorrelatorCallback.java 20321 2010-11-24 15:21:24Z 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  package org.mule.routing.correlation;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.api.MuleEvent;
14  import org.mule.api.MuleMessage;
15  import org.mule.routing.AggregationException;
16  import org.mule.routing.EventGroup;
17  
18  import java.text.MessageFormat;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * A Correlator that correlates messages based on Mule correlation settings
25   */
26  public class CollectionCorrelatorCallback implements EventCorrelatorCallback
27  {
28      /**
29       * logger used by this class
30       */
31      protected transient final Log logger = LogFactory.getLog(getClass());
32  
33      protected MuleContext muleContext;
34  
35      public CollectionCorrelatorCallback(MuleContext muleContext)
36      {
37          this.muleContext = muleContext;
38      }
39  
40      /**
41       * This method is invoked if the shouldAggregate method is called and returns
42       * true. Once this method returns an aggregated message, the event group is
43       * removed from the router.
44       *
45       * @param events the event group for this request
46       * @return an aggregated message
47       * @throws org.mule.routing.AggregationException
48       *          if the aggregation fails. in this scenario the
49       *          whole event group is removed and passed to the exception handler
50       *          for this componenet
51       */
52      public MuleEvent aggregateEvents(EventGroup events) throws AggregationException
53      {
54          return events.getMessageCollectionEvent();
55      }
56  
57      /**
58       * Creates a new EventGroup that will expect the number of events as returned by
59       * {@link MuleMessage#getCorrelationGroupSize()}.
60       */
61      public EventGroup createEventGroup(MuleEvent event, Object groupId)
62      {
63          return new EventGroup(groupId, event.getMessage().getCorrelationGroupSize());
64      }
65  
66      /**
67       * @return <code>true</code> if the correlation size is not set or exactly the
68       *         expected size of the event group.
69       * @see org.mule.routing.correlation.EventCorrelatorCallback#shouldAggregateEvents(org.mule.routing.EventGroup) 
70       */
71      public boolean shouldAggregateEvents(EventGroup events)
72      {
73          int size = events.expectedSize();
74  
75          if (size == -1)
76          {
77              logger.warn("Correlation Group Size not set, but correlation aggregator is being used."
78                      + " Message is being forwarded as is");
79              return true;
80          }
81  
82          if (logger.isDebugEnabled())
83          {
84              logger.debug(MessageFormat.format("Correlation group size is {0}. Current event group size is {1} for group ID: {2}",
85                                                size, events.size(), events.getGroupId()));
86          }
87  
88          return size == events.size();
89      }
90  }