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