1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.service; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.construct.FlowConstructAware; |
16 | |
import org.mule.api.endpoint.InboundEndpoint; |
17 | |
import org.mule.api.lifecycle.Disposable; |
18 | |
import org.mule.api.lifecycle.Initialisable; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.lifecycle.Startable; |
21 | |
import org.mule.api.lifecycle.Stoppable; |
22 | |
import org.mule.api.processor.InterceptingMessageProcessor; |
23 | |
import org.mule.api.processor.MessageProcessor; |
24 | |
import org.mule.api.routing.RouterStatisticsRecorder; |
25 | |
import org.mule.api.source.MessageSource; |
26 | |
import org.mule.management.stats.RouterStatistics; |
27 | |
import org.mule.processor.AbstractInterceptingMessageProcessor; |
28 | |
import org.mule.processor.StopFurtherMessageProcessingMessageProcessor; |
29 | |
import org.mule.processor.builder.InterceptingChainMessageProcessorBuilder; |
30 | |
import org.mule.routing.AbstractCatchAllStrategy; |
31 | |
import org.mule.routing.MessageFilter; |
32 | |
import org.mule.source.StartableCompositeMessageSource; |
33 | |
import org.mule.util.StringMessageUtils; |
34 | |
|
35 | |
import java.util.ArrayList; |
36 | |
import java.util.LinkedList; |
37 | |
import java.util.List; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class ServiceCompositeMessageSource extends StartableCompositeMessageSource implements Initialisable, RouterStatisticsRecorder |
44 | |
{ |
45 | |
|
46 | 0 | protected List<MessageProcessor> processors = new LinkedList<MessageProcessor>(); |
47 | |
protected RouterStatistics statistics; |
48 | 0 | protected List<InboundEndpoint> endpoints = new ArrayList<InboundEndpoint>(); |
49 | |
protected MessageProcessor catchAllStrategy; |
50 | 0 | private final InterceptingMessageProcessor internalCatchAllStrategy = new InternalCatchAllMessageProcessor(); |
51 | |
|
52 | |
public ServiceCompositeMessageSource() |
53 | 0 | { |
54 | 0 | statistics = new RouterStatistics(RouterStatistics.TYPE_INBOUND); |
55 | 0 | } |
56 | |
|
57 | |
public void initialise() throws InitialisationException |
58 | |
{ |
59 | 0 | super.initialise(); |
60 | 0 | if (catchAllStrategy != null) |
61 | |
{ |
62 | 0 | for (MessageProcessor processor : processors) |
63 | |
{ |
64 | 0 | if (processor instanceof MessageFilter |
65 | |
&& ((MessageFilter) processor).getUnacceptedMessageProcessor() == null) |
66 | |
{ |
67 | 0 | ((MessageFilter) processor).setUnacceptedMessageProcessor(catchAllStrategy); |
68 | |
} |
69 | |
} |
70 | |
} |
71 | |
|
72 | |
try |
73 | |
{ |
74 | 0 | createMessageProcessorChain(); |
75 | |
} |
76 | 0 | catch (MuleException e) |
77 | |
{ |
78 | 0 | throw new InitialisationException(e, this); |
79 | 0 | } |
80 | |
|
81 | 0 | for (MessageProcessor processor : processors) |
82 | |
{ |
83 | 0 | if (processor instanceof FlowConstructAware) |
84 | |
{ |
85 | 0 | ((FlowConstructAware) processor).setFlowConstruct(flowConstruct); |
86 | |
} |
87 | |
} |
88 | 0 | for (MessageProcessor processor : processors) |
89 | |
{ |
90 | 0 | if (processor instanceof Initialisable) |
91 | |
{ |
92 | 0 | ((Initialisable) processor).initialise(); |
93 | |
} |
94 | |
} |
95 | 0 | } |
96 | |
|
97 | |
@Override |
98 | |
public void dispose() |
99 | |
{ |
100 | 0 | for (MessageProcessor processor : processors) |
101 | |
{ |
102 | 0 | if (processor instanceof Disposable) |
103 | |
{ |
104 | 0 | ((Disposable) processor).dispose(); |
105 | |
} |
106 | |
} |
107 | 0 | super.dispose(); |
108 | 0 | } |
109 | |
|
110 | |
protected void createMessageProcessorChain() throws MuleException |
111 | |
{ |
112 | 0 | InterceptingChainMessageProcessorBuilder builder = new InterceptingChainMessageProcessorBuilder(flowConstruct); |
113 | 0 | builder.chain(processors); |
114 | 0 | builder.chain(new StopFurtherMessageProcessingMessageProcessor()); |
115 | |
|
116 | 0 | builder.chain(new AbstractInterceptingMessageProcessor() |
117 | 0 | { |
118 | |
public MuleEvent process(MuleEvent event) throws MuleException |
119 | |
{ |
120 | 0 | if (getRouterStatistics().isEnabled()) |
121 | |
{ |
122 | 0 | getRouterStatistics().incrementRoutedMessage(event.getEndpoint()); |
123 | |
} |
124 | 0 | return processNext(event); |
125 | |
} |
126 | |
}); |
127 | 0 | builder.chain(listener); |
128 | 0 | listener = builder.build(); |
129 | 0 | } |
130 | |
|
131 | |
@Override |
132 | |
public void start() throws MuleException |
133 | |
{ |
134 | 0 | for (MessageProcessor processor : processors) |
135 | |
{ |
136 | 0 | if (processor instanceof Startable) |
137 | |
{ |
138 | 0 | ((Startable) processor).start(); |
139 | |
} |
140 | |
} |
141 | 0 | super.start(); |
142 | 0 | } |
143 | |
|
144 | |
@Override |
145 | |
public void stop() throws MuleException |
146 | |
{ |
147 | 0 | super.stop(); |
148 | 0 | for (MessageProcessor processor : processors) |
149 | |
{ |
150 | 0 | if (processor instanceof Stoppable) |
151 | |
{ |
152 | 0 | ((Stoppable) processor).stop(); |
153 | |
} |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
public void setMessageProcessors(List<MessageProcessor> processors) |
158 | |
{ |
159 | 0 | this.processors = processors; |
160 | 0 | } |
161 | |
|
162 | |
public void addMessageProcessor(MessageProcessor processor) |
163 | |
{ |
164 | 0 | this.processors.add(processor); |
165 | 0 | } |
166 | |
|
167 | |
@Override |
168 | |
public void addSource(MessageSource source) throws MuleException |
169 | |
{ |
170 | 0 | super.addSource(source); |
171 | 0 | if (source instanceof InboundEndpoint) |
172 | |
{ |
173 | 0 | endpoints.add((InboundEndpoint) source); |
174 | |
} |
175 | 0 | } |
176 | |
|
177 | |
@Override |
178 | |
public void removeSource(MessageSource source) throws MuleException |
179 | |
{ |
180 | 0 | super.removeSource(source); |
181 | 0 | if (source instanceof InboundEndpoint) |
182 | |
{ |
183 | 0 | endpoints.remove(source); |
184 | |
} |
185 | 0 | } |
186 | |
|
187 | |
@Override |
188 | |
public void setMessageSources(List<MessageSource> sources) throws MuleException |
189 | |
{ |
190 | 0 | this.endpoints.clear(); |
191 | 0 | super.setMessageSources(sources); |
192 | 0 | } |
193 | |
|
194 | |
public List<InboundEndpoint> getEndpoints() |
195 | |
{ |
196 | 0 | return endpoints; |
197 | |
} |
198 | |
|
199 | |
public List<MessageProcessor> getMessageProcessors() |
200 | |
{ |
201 | 0 | return processors; |
202 | |
} |
203 | |
|
204 | |
public RouterStatistics getRouterStatistics() |
205 | |
{ |
206 | 0 | return statistics; |
207 | |
} |
208 | |
|
209 | |
public void setRouterStatistics(RouterStatistics statistics) |
210 | |
{ |
211 | 0 | this.statistics = statistics;; |
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public InboundEndpoint getEndpoint(String name) |
221 | |
{ |
222 | 0 | for (InboundEndpoint endpoint : endpoints) |
223 | |
{ |
224 | 0 | if (endpoint.getName().equals(name)) |
225 | |
{ |
226 | 0 | return endpoint; |
227 | |
} |
228 | |
} |
229 | 0 | return null; |
230 | |
} |
231 | |
|
232 | |
public void setCatchAllStrategy(MessageProcessor catchAllStrategy) |
233 | |
{ |
234 | 0 | if (catchAllStrategy instanceof AbstractCatchAllStrategy) |
235 | |
{ |
236 | 0 | ((AbstractCatchAllStrategy) catchAllStrategy).setRouterStatistics(statistics); |
237 | |
} |
238 | 0 | this.catchAllStrategy = catchAllStrategy; |
239 | 0 | this.internalCatchAllStrategy.setListener(catchAllStrategy); |
240 | 0 | } |
241 | |
|
242 | |
public MessageProcessor getCatchAllStrategy() |
243 | |
{ |
244 | 0 | return catchAllStrategy; |
245 | |
} |
246 | |
|
247 | 0 | class InternalCatchAllMessageProcessor extends AbstractInterceptingMessageProcessor |
248 | |
{ |
249 | |
public MuleEvent process(MuleEvent event) throws MuleException |
250 | |
{ |
251 | 0 | if (getRouterStatistics().isEnabled()) |
252 | |
{ |
253 | 0 | getRouterStatistics().incrementNoRoutedMessage(); |
254 | |
} |
255 | 0 | if (next != null) |
256 | |
{ |
257 | 0 | if (logger.isDebugEnabled()) |
258 | |
{ |
259 | 0 | logger.debug("Message did not match any routers on: " |
260 | |
+ event.getFlowConstruct().getName() + " - invoking catch all strategy"); |
261 | |
} |
262 | 0 | if (getRouterStatistics().isEnabled()) |
263 | |
{ |
264 | 0 | getRouterStatistics().incrementCaughtMessage(); |
265 | |
} |
266 | 0 | return processNext(event); |
267 | |
} |
268 | |
else |
269 | |
{ |
270 | 0 | logger.warn("Message did not match any routers on: " |
271 | |
+ event.getFlowConstruct().getName() |
272 | |
+ " and there is no catch all strategy configured on this router. Disposing message: " |
273 | |
+ event); |
274 | 0 | if (logger.isDebugEnabled()) |
275 | |
{ |
276 | |
try |
277 | |
{ |
278 | 0 | logger.warn("Message fragment is: " |
279 | |
+ StringMessageUtils.truncate(event.getMessageAsString(), 100, true)); |
280 | |
} |
281 | 0 | catch (MuleException e) |
282 | |
{ |
283 | |
|
284 | 0 | } |
285 | |
} |
286 | 0 | return null; |
287 | |
} |
288 | |
} |
289 | |
} |
290 | |
|
291 | |
} |