View Javadoc

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