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