1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.construct; |
12 | |
|
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.config.ThreadingProfile; |
17 | |
import org.mule.api.construct.FlowConstruct; |
18 | |
import org.mule.api.construct.FlowConstructAware; |
19 | |
import org.mule.api.construct.FlowConstructInvalidException; |
20 | |
import org.mule.api.context.MuleContextAware; |
21 | |
import org.mule.api.exception.MessagingExceptionHandler; |
22 | |
import org.mule.api.lifecycle.Disposable; |
23 | |
import org.mule.api.lifecycle.Initialisable; |
24 | |
import org.mule.api.lifecycle.InitialisationException; |
25 | |
import org.mule.api.lifecycle.Lifecycle; |
26 | |
import org.mule.api.lifecycle.LifecycleCallback; |
27 | |
import org.mule.api.lifecycle.LifecycleState; |
28 | |
import org.mule.api.lifecycle.Startable; |
29 | |
import org.mule.api.lifecycle.Stoppable; |
30 | |
import org.mule.api.processor.MessageProcessor; |
31 | |
import org.mule.api.processor.MessageProcessorBuilder; |
32 | |
import org.mule.api.processor.MessageProcessorChain; |
33 | |
import org.mule.api.processor.MessageProcessorChainBuilder; |
34 | |
import org.mule.api.routing.MessageInfoMapping; |
35 | |
import org.mule.api.source.MessageSource; |
36 | |
import org.mule.exception.DefaultServiceExceptionStrategy; |
37 | |
import org.mule.management.stats.FlowConstructStatistics; |
38 | |
import org.mule.processor.AbstractInterceptingMessageProcessor; |
39 | |
import org.mule.processor.chain.DefaultMessageProcessorChainBuilder; |
40 | |
import org.mule.routing.MuleMessageInfoMapping; |
41 | |
import org.mule.util.ClassUtils; |
42 | |
|
43 | |
import java.beans.ExceptionListener; |
44 | |
|
45 | |
import org.apache.commons.logging.Log; |
46 | |
import org.apache.commons.logging.LogFactory; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public abstract class AbstractFlowConstruct implements FlowConstruct, Lifecycle |
69 | |
{ |
70 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
71 | |
|
72 | |
protected String name; |
73 | |
protected MessageSource messageSource; |
74 | |
protected MessageProcessorChain messageProcessorChain; |
75 | |
protected MessagingExceptionHandler exceptionListener; |
76 | |
protected final FlowConstructLifecycleManager lifecycleManager; |
77 | |
protected final MuleContext muleContext; |
78 | |
protected FlowConstructStatistics statistics; |
79 | 0 | protected MessageInfoMapping messageInfoMapping = new MuleMessageInfoMapping(); |
80 | |
protected ThreadingProfile threadingProfile; |
81 | |
|
82 | |
public AbstractFlowConstruct(String name, MuleContext muleContext) |
83 | 0 | { |
84 | 0 | this.muleContext = muleContext; |
85 | 0 | this.name = name; |
86 | 0 | this.lifecycleManager = new FlowConstructLifecycleManager(this, muleContext); |
87 | 0 | this.exceptionListener = new DefaultServiceExceptionStrategy(muleContext); |
88 | 0 | } |
89 | |
|
90 | |
public final void initialise() throws InitialisationException |
91 | |
{ |
92 | |
try |
93 | |
{ |
94 | 0 | lifecycleManager.fireInitialisePhase(new LifecycleCallback<FlowConstruct>() |
95 | 0 | { |
96 | |
public void onTransition(String phaseName, FlowConstruct object) throws MuleException |
97 | |
{ |
98 | 0 | createMessageProcessor(); |
99 | |
|
100 | 0 | if (messageSource != null) |
101 | |
{ |
102 | |
|
103 | 0 | messageSource.setListener(new AbstractInterceptingMessageProcessor() |
104 | 0 | { |
105 | |
public MuleEvent process(MuleEvent event) throws MuleException |
106 | |
{ |
107 | 0 | return messageProcessorChain.process(event); |
108 | |
} |
109 | |
}); |
110 | |
} |
111 | |
|
112 | 0 | injectFlowConstructMuleContext(messageSource); |
113 | 0 | injectFlowConstructMuleContext(messageProcessorChain); |
114 | 0 | injectFlowConstructMuleContext(exceptionListener); |
115 | 0 | initialiseIfInitialisable(messageSource); |
116 | 0 | initialiseIfInitialisable(messageProcessorChain); |
117 | 0 | initialiseIfInitialisable(exceptionListener); |
118 | |
|
119 | 0 | doInitialise(); |
120 | |
|
121 | 0 | validateConstruct(); |
122 | 0 | } |
123 | |
}); |
124 | |
|
125 | |
} |
126 | 0 | catch (InitialisationException e) |
127 | |
{ |
128 | 0 | throw e; |
129 | |
} |
130 | 0 | catch (MuleException e) |
131 | |
{ |
132 | 0 | throw new InitialisationException(e, this); |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | |
public final void start() throws MuleException |
137 | |
{ |
138 | 0 | lifecycleManager.fireStartPhase(new LifecycleCallback<FlowConstruct>() |
139 | 0 | { |
140 | |
public void onTransition(String phaseName, FlowConstruct object) throws MuleException |
141 | |
{ |
142 | 0 | startIfStartable(messageProcessorChain); |
143 | 0 | startIfStartable(exceptionListener); |
144 | 0 | startIfStartable(messageSource); |
145 | 0 | doStart(); |
146 | 0 | } |
147 | |
}); |
148 | 0 | } |
149 | |
|
150 | |
public final void stop() throws MuleException |
151 | |
{ |
152 | 0 | lifecycleManager.fireStopPhase(new LifecycleCallback<FlowConstruct>() |
153 | 0 | { |
154 | |
public void onTransition(String phaseName, FlowConstruct object) throws MuleException |
155 | |
{ |
156 | 0 | stopIfStoppable(messageSource); |
157 | 0 | stopIfStoppable(messageProcessorChain); |
158 | 0 | stopIfStoppable(exceptionListener); |
159 | 0 | doStop(); |
160 | 0 | } |
161 | |
}); |
162 | 0 | } |
163 | |
|
164 | |
public final void dispose() |
165 | |
{ |
166 | |
try |
167 | |
{ |
168 | 0 | if (isStarted()) |
169 | |
{ |
170 | 0 | stop(); |
171 | |
} |
172 | |
|
173 | 0 | lifecycleManager.fireDisposePhase(new LifecycleCallback<FlowConstruct>() |
174 | 0 | { |
175 | |
public void onTransition(String phaseName, FlowConstruct object) throws MuleException |
176 | |
{ |
177 | 0 | disposeIfDisposable(messageProcessorChain); |
178 | 0 | disposeIfDisposable(exceptionListener); |
179 | 0 | disposeIfDisposable(messageSource); |
180 | 0 | doDispose(); |
181 | 0 | } |
182 | |
}); |
183 | |
} |
184 | 0 | catch (MuleException e) |
185 | |
{ |
186 | 0 | logger.error("Failed to stop service: " + name, e); |
187 | 0 | } |
188 | 0 | } |
189 | |
|
190 | |
public ThreadingProfile getThreadingProfile() |
191 | |
{ |
192 | 0 | return threadingProfile; |
193 | |
} |
194 | |
|
195 | |
public boolean isStarted() |
196 | |
{ |
197 | 0 | return lifecycleManager.getState().isStarted(); |
198 | |
} |
199 | |
|
200 | |
public boolean isStopped() |
201 | |
{ |
202 | 0 | return lifecycleManager.getState().isStopped(); |
203 | |
} |
204 | |
|
205 | |
public boolean isStopping() |
206 | |
{ |
207 | 0 | return lifecycleManager.getState().isStopping(); |
208 | |
} |
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
protected void createMessageProcessor() throws MuleException |
225 | |
{ |
226 | 0 | DefaultMessageProcessorChainBuilder builder = new DefaultMessageProcessorChainBuilder(this); |
227 | 0 | configureMessageProcessors(builder); |
228 | 0 | messageProcessorChain = builder.build(); |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
protected abstract void configureMessageProcessors(MessageProcessorChainBuilder builder) throws MuleException; |
246 | |
|
247 | |
public String getName() |
248 | |
{ |
249 | 0 | return name; |
250 | |
} |
251 | |
|
252 | |
public MessagingExceptionHandler getExceptionListener() |
253 | |
{ |
254 | 0 | return exceptionListener; |
255 | |
} |
256 | |
|
257 | |
public void setExceptionListener(MessagingExceptionHandler exceptionListener) |
258 | |
{ |
259 | 0 | this.exceptionListener = exceptionListener; |
260 | 0 | } |
261 | |
|
262 | |
public LifecycleState getLifecycleState() |
263 | |
{ |
264 | 0 | return lifecycleManager.getState(); |
265 | |
} |
266 | |
|
267 | |
public MuleContext getMuleContext() |
268 | |
{ |
269 | 0 | return muleContext; |
270 | |
} |
271 | |
|
272 | |
public MessageSource getMessageSource() |
273 | |
{ |
274 | 0 | return messageSource; |
275 | |
} |
276 | |
|
277 | |
public void setMessageSource(MessageSource messageSource) |
278 | |
{ |
279 | 0 | this.messageSource = messageSource; |
280 | 0 | } |
281 | |
|
282 | |
public FlowConstructStatistics getStatistics() |
283 | |
{ |
284 | 0 | return statistics; |
285 | |
} |
286 | |
|
287 | |
public MessageInfoMapping getMessageInfoMapping() |
288 | |
{ |
289 | 0 | return messageInfoMapping; |
290 | |
} |
291 | |
|
292 | |
public void setMessageInfoMapping(MessageInfoMapping messageInfoMapping) |
293 | |
{ |
294 | 0 | this.messageInfoMapping = messageInfoMapping; |
295 | 0 | } |
296 | |
|
297 | |
protected void doInitialise() throws InitialisationException |
298 | |
{ |
299 | 0 | int threadPoolSize = threadingProfile == null ? 0 : threadingProfile.getMaxThreadsActive(); |
300 | 0 | statistics = new FlowConstructStatistics(getConstructType(), name, threadPoolSize); |
301 | 0 | statistics.setEnabled(muleContext.getStatistics().isEnabled()); |
302 | 0 | muleContext.getStatistics().add(statistics); |
303 | 0 | } |
304 | |
|
305 | |
protected void doStart() throws MuleException |
306 | |
{ |
307 | |
|
308 | 0 | } |
309 | |
|
310 | |
protected void doStop() throws MuleException |
311 | |
{ |
312 | |
|
313 | 0 | } |
314 | |
|
315 | |
protected void doDispose() |
316 | |
{ |
317 | 0 | muleContext.getStatistics().remove(statistics); |
318 | 0 | } |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
protected void validateConstruct() throws FlowConstructInvalidException |
327 | |
{ |
328 | |
|
329 | 0 | } |
330 | |
|
331 | |
protected void injectFlowConstructMuleContext(Object candidate) |
332 | |
{ |
333 | 0 | if (candidate instanceof FlowConstructAware) |
334 | |
{ |
335 | 0 | ((FlowConstructAware) candidate).setFlowConstruct(this); |
336 | |
} |
337 | 0 | if (candidate instanceof MuleContextAware) |
338 | |
{ |
339 | 0 | ((MuleContextAware) candidate).setMuleContext(muleContext); |
340 | |
} |
341 | 0 | } |
342 | |
|
343 | |
@Override |
344 | |
public String toString() |
345 | |
{ |
346 | 0 | return String.format("%s{%s}", ClassUtils.getSimpleName(this.getClass()), getName()); |
347 | |
} |
348 | |
|
349 | |
protected void initialiseIfInitialisable(Object candidate) throws InitialisationException |
350 | |
{ |
351 | 0 | if (candidate instanceof Initialisable) |
352 | |
{ |
353 | 0 | ((Initialisable) candidate).initialise(); |
354 | |
} |
355 | 0 | } |
356 | |
|
357 | |
protected void startIfStartable(Object candidate) throws MuleException |
358 | |
{ |
359 | 0 | if (candidate instanceof Startable) |
360 | |
{ |
361 | 0 | ((Startable) candidate).start(); |
362 | |
} |
363 | 0 | } |
364 | |
|
365 | |
protected void stopIfStoppable(Object candidate) throws MuleException |
366 | |
{ |
367 | 0 | if (candidate instanceof Stoppable) |
368 | |
{ |
369 | 0 | ((Stoppable) candidate).stop(); |
370 | |
} |
371 | 0 | } |
372 | |
|
373 | |
protected void disposeIfDisposable(Object candidate) |
374 | |
{ |
375 | 0 | if (candidate instanceof Disposable) |
376 | |
{ |
377 | 0 | ((Disposable) candidate).dispose(); |
378 | |
} |
379 | 0 | } |
380 | |
|
381 | |
public MessageProcessorChain getMessageProcessorChain() |
382 | |
{ |
383 | 0 | return this.messageProcessorChain; |
384 | |
} |
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
|
390 | |
public abstract String getConstructType(); |
391 | |
} |