Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractMessageSplitter |
|
| 2.8333333333333335;2.833 |
1 | /* | |
2 | * $Id: AbstractMessageSplitter.java 11475 2008-03-21 22:46:33Z rossmason $ | |
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 | ||
11 | package org.mule.routing.outbound; | |
12 | ||
13 | import org.mule.api.MuleException; | |
14 | import org.mule.api.MuleMessage; | |
15 | import org.mule.api.MuleSession; | |
16 | import org.mule.api.config.MuleProperties; | |
17 | import org.mule.api.endpoint.OutboundEndpoint; | |
18 | import org.mule.api.routing.CouldNotRouteOutboundMessageException; | |
19 | import org.mule.api.routing.RoutingException; | |
20 | ||
21 | import java.util.Iterator; | |
22 | import java.util.List; | |
23 | ||
24 | /** | |
25 | * <code>AbstractMessageSplitter</code> is an outbound Message Splitter used to split | |
26 | * the contents of a received message into sub parts that can be processed by other | |
27 | * components. Each Part is fired as a separate event to each endpoint on the router. The | |
28 | * endpoints can have filters on them to receive only certain message parts. | |
29 | */ | |
30 | 6 | public abstract class AbstractMessageSplitter extends FilteringOutboundRouter |
31 | { | |
32 | // Determines if the same endpoint will be matched multiple times until a | |
33 | // match is not found. This should be set by overriding classes. | |
34 | 6 | protected boolean multimatch = true; |
35 | ||
36 | // flag which, if true, makes the splitter honour settings such as remoteSync and | |
37 | // synchronous on the endpoint | |
38 | 6 | protected boolean honorSynchronicity = false; |
39 | ||
40 | public MuleMessage route(MuleMessage message, MuleSession session, boolean synchronous) | |
41 | throws RoutingException | |
42 | { | |
43 | 10 | String correlationId = messageInfoMapping.getCorrelationId(message); |
44 | ||
45 | 10 | this.initialise(message); |
46 | ||
47 | OutboundEndpoint endpoint; | |
48 | 10 | MuleMessage result = null; |
49 | 10 | List list = getEndpoints(); |
50 | 10 | int correlationSequence = 1; |
51 | 10 | for (Iterator iterator = list.iterator(); iterator.hasNext();) |
52 | { | |
53 | 26 | endpoint = (OutboundEndpoint) iterator.next(); |
54 | 26 | message = getMessagePart(message, endpoint); |
55 | // TODO MULE-1378 | |
56 | 26 | if (message == null) |
57 | { | |
58 | // Log a warning if there are no messages for a given endpoint | |
59 | 0 | logger.warn("Message part is null for endpoint: " + endpoint.getEndpointURI().toString()); |
60 | } | |
61 | ||
62 | // We'll keep looping to get all messages for the current endpoint | |
63 | // before moving to the next endpoint | |
64 | // This can be turned off by setting the multimatch flag to false | |
65 | 50 | while (message != null) |
66 | { | |
67 | 36 | if (honorSynchronicity) |
68 | { | |
69 | 0 | synchronous = endpoint.isSynchronous(); |
70 | } | |
71 | try | |
72 | { | |
73 | 36 | if (enableCorrelation != ENABLE_CORRELATION_NEVER) |
74 | { | |
75 | 36 | boolean correlationSet = message.getCorrelationId() != null; |
76 | 36 | if (!correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET)) |
77 | { | |
78 | 36 | message.setCorrelationId(correlationId); |
79 | } | |
80 | ||
81 | // take correlation group size from the message | |
82 | // properties, set by concrete message splitter | |
83 | // implementations | |
84 | 36 | final int groupSize = message.getCorrelationGroupSize(); |
85 | 36 | message.setCorrelationGroupSize(groupSize); |
86 | 36 | message.setCorrelationSequence(correlationSequence++); |
87 | } | |
88 | ||
89 | 36 | if (honorSynchronicity) |
90 | { | |
91 | 0 | message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY, |
92 | endpoint.isRemoteSync()); | |
93 | } | |
94 | ||
95 | 36 | if (synchronous) |
96 | { | |
97 | 22 | result = send(session, message, endpoint); |
98 | } | |
99 | else | |
100 | { | |
101 | 14 | dispatch(session, message, endpoint); |
102 | } | |
103 | } | |
104 | 0 | catch (MuleException e) |
105 | { | |
106 | 0 | throw new CouldNotRouteOutboundMessageException(message, endpoint, e); |
107 | 36 | } |
108 | ||
109 | 36 | if (!multimatch) |
110 | { | |
111 | 12 | break; |
112 | } | |
113 | ||
114 | 24 | message = this.getMessagePart(message, endpoint); |
115 | } | |
116 | } | |
117 | ||
118 | // we are done with splitting & routing | |
119 | 10 | this.cleanup(); |
120 | ||
121 | 10 | return result; |
122 | } | |
123 | ||
124 | public boolean isHonorSynchronicity() | |
125 | { | |
126 | 0 | return honorSynchronicity; |
127 | } | |
128 | ||
129 | /** | |
130 | * Sets the flag indicating whether the splitter honurs endpoint settings | |
131 | * | |
132 | * @param honorSynchronicity flag setting | |
133 | */ | |
134 | public void setHonorSynchronicity(boolean honorSynchronicity) | |
135 | { | |
136 | 0 | this.honorSynchronicity = honorSynchronicity; |
137 | 0 | } |
138 | ||
139 | /** | |
140 | * This method can be implemented to split the message up before | |
141 | * {@link #getMessagePart(MuleMessage, OutboundEndpoint)} method is called. | |
142 | * | |
143 | * @param message the message being routed | |
144 | */ | |
145 | protected abstract void initialise(MuleMessage message); | |
146 | ||
147 | /** | |
148 | * Retrieves a specific message part for the given endpoint. the message will then be | |
149 | * routed via the provider. <p/> <strong>NOTE:</strong>Implementations must provide | |
150 | * proper synchronization for shared state (payload, properties, etc.) | |
151 | * | |
152 | * @param message the current message being processed | |
153 | * @param endpoint the endpoint that will be used to route the resulting message part | |
154 | * @return the message part to dispatch | |
155 | */ | |
156 | protected abstract MuleMessage getMessagePart(MuleMessage message, OutboundEndpoint endpoint); | |
157 | ||
158 | /** | |
159 | * This method is called after all parts of the original message have been processed; | |
160 | * typically this is the case after {@link #getMessagePart(MuleMessage, OutboundEndpoint)} | |
161 | * returned <code>null</code>. | |
162 | */ | |
163 | protected abstract void cleanup(); | |
164 | ||
165 | } |