View Javadoc

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