1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.model; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.i18n.CoreMessages; |
15 | |
import org.mule.config.i18n.MessageFactory; |
16 | |
import org.mule.impl.DefaultComponentExceptionStrategy; |
17 | |
import org.mule.impl.MuleDescriptor; |
18 | |
import org.mule.impl.OptimizedRequestContext; |
19 | |
import org.mule.impl.internal.notifications.ComponentNotification; |
20 | |
import org.mule.management.stats.ComponentStatistics; |
21 | |
import org.mule.providers.AbstractConnector; |
22 | |
import org.mule.umo.ComponentException; |
23 | |
import org.mule.umo.UMOComponent; |
24 | |
import org.mule.umo.UMODescriptor; |
25 | |
import org.mule.umo.UMOEvent; |
26 | |
import org.mule.umo.UMOException; |
27 | |
import org.mule.umo.UMOMessage; |
28 | |
import org.mule.umo.endpoint.UMOEndpoint; |
29 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
30 | |
import org.mule.umo.lifecycle.InitialisationException; |
31 | |
import org.mule.umo.model.ModelException; |
32 | |
import org.mule.umo.model.UMOModel; |
33 | |
import org.mule.umo.provider.DispatchException; |
34 | |
import org.mule.umo.provider.UMOMessageReceiver; |
35 | |
import org.mule.util.concurrent.WaitableBoolean; |
36 | |
|
37 | |
import java.beans.ExceptionListener; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Iterator; |
40 | |
import java.util.List; |
41 | |
|
42 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
43 | |
import org.apache.commons.logging.Log; |
44 | |
import org.apache.commons.logging.LogFactory; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public abstract class AbstractComponent implements UMOComponent |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | 0 | protected transient Log logger = LogFactory.getLog(getClass()); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | protected MuleDescriptor descriptor = null; |
60 | |
|
61 | 0 | protected ComponentStatistics stats = null; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | protected AtomicBoolean stopped = new AtomicBoolean(true); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | protected WaitableBoolean stopping = new WaitableBoolean(false); |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | protected AtomicBoolean poolInitialised = new AtomicBoolean(false); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 0 | protected ExceptionListener exceptionListener = null; |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | protected AtomicBoolean initialised = new AtomicBoolean(false); |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
protected UMOModel model; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | protected WaitableBoolean paused = new WaitableBoolean(false); |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public AbstractComponent(MuleDescriptor descriptor, UMOModel model) |
103 | 0 | { |
104 | 0 | if (descriptor == null) |
105 | |
{ |
106 | 0 | throw new IllegalArgumentException("Descriptor cannot be null"); |
107 | |
} |
108 | 0 | this.descriptor = descriptor; |
109 | 0 | this.model = model; |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public final synchronized void initialise() throws InitialisationException |
122 | |
{ |
123 | 0 | if (initialised.get()) |
124 | |
{ |
125 | 0 | throw new InitialisationException( |
126 | |
CoreMessages.objectAlreadyInitialised("Component '" + descriptor.getName() + "'"), this); |
127 | |
} |
128 | 0 | descriptor.initialise(); |
129 | |
|
130 | 0 | this.exceptionListener = descriptor.getExceptionListener(); |
131 | |
|
132 | |
|
133 | 0 | stats = createStatistics(); |
134 | |
|
135 | 0 | stats.setEnabled(((MuleManager) MuleManager.getInstance()).getStatistics().isEnabled()); |
136 | 0 | ((MuleManager) MuleManager.getInstance()).getStatistics().add(stats); |
137 | 0 | stats.setOutboundRouterStat(getDescriptor().getOutboundRouter().getStatistics()); |
138 | 0 | stats.setInboundRouterStat(getDescriptor().getInboundRouter().getStatistics()); |
139 | |
|
140 | 0 | doInitialise(); |
141 | 0 | initialised.set(true); |
142 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_INITIALISED); |
143 | |
|
144 | 0 | } |
145 | |
|
146 | |
protected ComponentStatistics createStatistics() |
147 | |
{ |
148 | 0 | return new ComponentStatistics(getName(), |
149 | |
descriptor.getThreadingProfile().getMaxThreadsActive()); |
150 | |
} |
151 | |
|
152 | |
protected void fireComponentNotification(int action) |
153 | |
{ |
154 | 0 | MuleManager.getInstance().fireNotification(new ComponentNotification(descriptor, action)); |
155 | 0 | } |
156 | |
|
157 | |
public void forceStop() throws UMOException |
158 | |
{ |
159 | 0 | if (!stopped.get()) |
160 | |
{ |
161 | 0 | logger.debug("Stopping UMOComponent"); |
162 | 0 | stopping.set(true); |
163 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_STOPPING); |
164 | 0 | doForceStop(); |
165 | 0 | stopped.set(true); |
166 | 0 | stopping.set(false); |
167 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_STOPPED); |
168 | |
} |
169 | 0 | } |
170 | |
|
171 | |
public void stop() throws UMOException |
172 | |
{ |
173 | 0 | if (!stopped.get()) |
174 | |
{ |
175 | 0 | logger.debug("Stopping UMOComponent"); |
176 | 0 | stopping.set(true); |
177 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_STOPPING); |
178 | |
|
179 | |
|
180 | 0 | unregisterListeners(); |
181 | 0 | if (MuleManager.getInstance().getQueueManager().getQueueSession().getQueue( |
182 | |
descriptor.getName() + ".component").size() > 0) |
183 | |
{ |
184 | |
try |
185 | |
{ |
186 | 0 | stopping.whenFalse(null); |
187 | |
} |
188 | 0 | catch (InterruptedException e) |
189 | |
{ |
190 | |
|
191 | |
|
192 | 0 | } |
193 | |
} |
194 | |
|
195 | 0 | doStop(); |
196 | 0 | stopped.set(true); |
197 | 0 | initialised.set(false); |
198 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_STOPPED); |
199 | |
} |
200 | 0 | } |
201 | |
|
202 | |
public void start() throws UMOException |
203 | |
{ |
204 | 0 | start(false); |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
protected void start(boolean startPaused) throws UMOException |
214 | |
{ |
215 | |
|
216 | |
|
217 | 0 | registerListeners(); |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | 0 | connectListeners(); |
226 | |
|
227 | |
|
228 | 0 | if (stopped.get()) |
229 | |
{ |
230 | 0 | stopped.set(false); |
231 | 0 | paused.set(false); |
232 | 0 | doStart(); |
233 | |
} |
234 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_STARTED); |
235 | 0 | if (startPaused) |
236 | |
{ |
237 | 0 | pause(); |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | 0 | startListeners(); |
245 | 0 | } |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public final void pause() throws UMOException |
253 | |
{ |
254 | |
|
255 | 0 | doPause(); |
256 | 0 | paused.set(true); |
257 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_PAUSED); |
258 | 0 | } |
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
public final void resume() throws UMOException |
265 | |
{ |
266 | 0 | doResume(); |
267 | 0 | paused.set(false); |
268 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_RESUMED); |
269 | 0 | } |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public boolean isPaused() |
277 | |
{ |
278 | 0 | return paused.get(); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
protected void doPause() throws UMOException |
289 | |
{ |
290 | |
|
291 | 0 | } |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
protected void doResume() throws UMOException |
301 | |
{ |
302 | |
|
303 | 0 | } |
304 | |
|
305 | |
public final void dispose() |
306 | |
{ |
307 | |
try |
308 | |
{ |
309 | 0 | if (!stopped.get()) |
310 | |
{ |
311 | 0 | stop(); |
312 | |
} |
313 | |
} |
314 | 0 | catch (UMOException e) |
315 | |
{ |
316 | |
|
317 | 0 | logger.error("Failed to stop component: " + descriptor.getName(), e); |
318 | 0 | } |
319 | 0 | doDispose(); |
320 | 0 | fireComponentNotification(ComponentNotification.COMPONENT_DISPOSED); |
321 | 0 | ((MuleManager) MuleManager.getInstance()).getStatistics().remove(stats); |
322 | 0 | } |
323 | |
|
324 | |
public ComponentStatistics getStatistics() |
325 | |
{ |
326 | 0 | return stats; |
327 | |
} |
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
public UMODescriptor getDescriptor() |
335 | |
{ |
336 | 0 | return descriptor; |
337 | |
} |
338 | |
|
339 | |
public void dispatchEvent(UMOEvent event) throws UMOException |
340 | |
{ |
341 | 0 | if (stopping.get() || stopped.get()) |
342 | |
{ |
343 | 0 | throw new ComponentException( |
344 | |
CoreMessages.componentIsStopped(this.getDescriptor().getName()), |
345 | |
event.getMessage(), this); |
346 | |
} |
347 | |
|
348 | |
try |
349 | |
{ |
350 | 0 | waitIfPaused(event); |
351 | |
} |
352 | 0 | catch (InterruptedException e) |
353 | |
{ |
354 | 0 | throw new ComponentException(event.getMessage(), this, e); |
355 | 0 | } |
356 | |
|
357 | |
|
358 | |
|
359 | 0 | UMOImmutableEndpoint endpoint = event.getEndpoint(); |
360 | |
|
361 | 0 | if (!endpoint.canReceive()) |
362 | |
{ |
363 | |
try |
364 | |
{ |
365 | 0 | endpoint.dispatch(event); |
366 | |
} |
367 | 0 | catch (Exception e) |
368 | |
{ |
369 | 0 | throw new DispatchException(event.getMessage(), event.getEndpoint(), e); |
370 | 0 | } |
371 | |
|
372 | 0 | return; |
373 | |
} |
374 | |
|
375 | |
|
376 | 0 | if (stats.isEnabled()) |
377 | |
{ |
378 | 0 | stats.incReceivedEventASync(); |
379 | |
} |
380 | |
|
381 | 0 | if (logger.isDebugEnabled()) |
382 | |
{ |
383 | 0 | logger.debug("Component: " + descriptor.getName() + " has received asynchronous event on: " |
384 | |
+ event.getEndpoint().getEndpointURI()); |
385 | |
} |
386 | |
|
387 | 0 | doDispatch(event); |
388 | 0 | } |
389 | |
|
390 | |
public UMOMessage sendEvent(UMOEvent event) throws UMOException |
391 | |
{ |
392 | 0 | if (stopping.get() || stopped.get()) |
393 | |
{ |
394 | 0 | throw new ComponentException( |
395 | |
CoreMessages.componentIsStopped(this.getDescriptor().getName()), |
396 | |
event.getMessage(), this); |
397 | |
} |
398 | |
|
399 | |
try |
400 | |
{ |
401 | 0 | waitIfPaused(event); |
402 | |
} |
403 | 0 | catch (InterruptedException e) |
404 | |
{ |
405 | 0 | throw new ComponentException(event.getMessage(), this, e); |
406 | 0 | } |
407 | |
|
408 | 0 | if (stats.isEnabled()) |
409 | |
{ |
410 | 0 | stats.incReceivedEventSync(); |
411 | |
} |
412 | 0 | if (logger.isDebugEnabled()) |
413 | |
{ |
414 | 0 | logger.debug("Component: " + descriptor.getName() + " has received synchronous event on: " |
415 | |
+ event.getEndpoint().getEndpointURI()); |
416 | |
} |
417 | 0 | event = OptimizedRequestContext.unsafeSetEvent(event); |
418 | 0 | return doSend(event); |
419 | |
} |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
protected void waitIfPaused(UMOEvent event) throws InterruptedException |
430 | |
{ |
431 | 0 | if (logger.isDebugEnabled() && paused.get()) |
432 | |
{ |
433 | 0 | logger.debug("Component: " + descriptor.getName() |
434 | |
+ " is paused. Blocking call until resume is called"); |
435 | |
} |
436 | 0 | paused.whenFalse(null); |
437 | 0 | } |
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
public String getName() |
443 | |
{ |
444 | 0 | return descriptor.getName(); |
445 | |
} |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
public String toString() |
453 | |
{ |
454 | 0 | return descriptor.getName(); |
455 | |
} |
456 | |
|
457 | |
public boolean isStopped() |
458 | |
{ |
459 | 0 | return stopped.get(); |
460 | |
} |
461 | |
|
462 | |
public boolean isStopping() |
463 | |
{ |
464 | 0 | return stopping.get(); |
465 | |
} |
466 | |
|
467 | |
protected void handleException(Exception e) |
468 | |
{ |
469 | 0 | if (exceptionListener instanceof DefaultComponentExceptionStrategy) |
470 | |
{ |
471 | 0 | if (((DefaultComponentExceptionStrategy) exceptionListener).getComponent() == null) |
472 | |
{ |
473 | 0 | ((DefaultComponentExceptionStrategy) exceptionListener).setComponent(this); |
474 | |
} |
475 | |
} |
476 | 0 | exceptionListener.exceptionThrown(e); |
477 | 0 | } |
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
protected Object lookupComponent() throws UMOException |
486 | |
{ |
487 | 0 | return ComponentFactory.createComponent(getDescriptor()); |
488 | |
} |
489 | |
|
490 | |
protected void doForceStop() throws UMOException |
491 | |
{ |
492 | |
|
493 | 0 | } |
494 | |
|
495 | |
protected void doStop() throws UMOException |
496 | |
{ |
497 | |
|
498 | 0 | } |
499 | |
|
500 | |
protected void doStart() throws UMOException |
501 | |
{ |
502 | |
|
503 | 0 | } |
504 | |
|
505 | |
protected void doDispose() |
506 | |
{ |
507 | |
|
508 | 0 | } |
509 | |
|
510 | |
protected void doInitialise() throws InitialisationException |
511 | |
{ |
512 | |
|
513 | 0 | } |
514 | |
|
515 | |
public boolean isStarted() |
516 | |
{ |
517 | 0 | return !stopped.get(); |
518 | |
} |
519 | |
|
520 | |
protected abstract UMOMessage doSend(UMOEvent event) throws UMOException; |
521 | |
|
522 | |
protected abstract void doDispatch(UMOEvent event) throws UMOException; |
523 | |
|
524 | |
public Object getInstance() throws UMOException |
525 | |
{ |
526 | 0 | return lookupComponent(); |
527 | |
} |
528 | |
|
529 | |
protected void registerListeners() throws UMOException |
530 | |
{ |
531 | |
UMOEndpoint endpoint; |
532 | 0 | List endpoints = getIncomingEndpoints(); |
533 | |
|
534 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
535 | |
{ |
536 | 0 | endpoint = (UMOEndpoint) it.next(); |
537 | |
try |
538 | |
{ |
539 | 0 | endpoint.getConnector().registerListener(this, endpoint); |
540 | |
} |
541 | 0 | catch (UMOException e) |
542 | |
{ |
543 | 0 | throw e; |
544 | |
} |
545 | 0 | catch (Exception e) |
546 | |
{ |
547 | 0 | throw new ModelException( |
548 | |
CoreMessages.failedtoRegisterOnEndpoint(this.getDescriptor().getName(), |
549 | |
endpoint.getEndpointURI()), e); |
550 | 0 | } |
551 | |
} |
552 | 0 | } |
553 | |
|
554 | |
protected void unregisterListeners() throws UMOException |
555 | |
{ |
556 | |
UMOEndpoint endpoint; |
557 | 0 | List endpoints = getIncomingEndpoints(); |
558 | |
|
559 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
560 | |
{ |
561 | 0 | endpoint = (UMOEndpoint) it.next(); |
562 | |
try |
563 | |
{ |
564 | 0 | endpoint.getConnector().unregisterListener(this, endpoint); |
565 | |
} |
566 | 0 | catch (UMOException e) |
567 | |
{ |
568 | 0 | throw e; |
569 | |
} |
570 | 0 | catch (Exception e) |
571 | |
{ |
572 | 0 | throw new ModelException( |
573 | |
CoreMessages.failedToUnregister(this.getDescriptor().getName(), |
574 | |
endpoint.getEndpointURI()), e); |
575 | 0 | } |
576 | |
} |
577 | 0 | } |
578 | |
|
579 | |
protected void startListeners() throws UMOException |
580 | |
{ |
581 | |
UMOEndpoint endpoint; |
582 | 0 | List endpoints = getIncomingEndpoints(); |
583 | |
|
584 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
585 | |
{ |
586 | 0 | endpoint = (UMOEndpoint) it.next(); |
587 | 0 | UMOMessageReceiver receiver = ((AbstractConnector) endpoint.getConnector()).getReceiver(this, |
588 | |
endpoint); |
589 | 0 | if (receiver != null && endpoint.getConnector().isStarted() |
590 | |
&& endpoint.getInitialState().equals(UMOEndpoint.INITIAL_STATE_STARTED)) |
591 | |
{ |
592 | 0 | receiver.start(); |
593 | |
} |
594 | |
} |
595 | 0 | } |
596 | |
|
597 | |
protected void stopListeners() throws UMOException |
598 | |
{ |
599 | |
UMOEndpoint endpoint; |
600 | 0 | List endpoints = getIncomingEndpoints(); |
601 | |
|
602 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
603 | |
{ |
604 | 0 | endpoint = (UMOEndpoint) it.next(); |
605 | 0 | UMOMessageReceiver receiver = ((AbstractConnector) endpoint.getConnector()).getReceiver(this, |
606 | |
endpoint); |
607 | 0 | if (receiver != null) |
608 | |
{ |
609 | 0 | receiver.stop(); |
610 | |
} |
611 | |
} |
612 | 0 | } |
613 | |
|
614 | |
protected void connectListeners() throws UMOException |
615 | |
{ |
616 | |
UMOEndpoint endpoint; |
617 | 0 | List endpoints = getIncomingEndpoints(); |
618 | |
|
619 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
620 | |
{ |
621 | 0 | endpoint = (UMOEndpoint) it.next(); |
622 | 0 | UMOMessageReceiver receiver = ((AbstractConnector) endpoint.getConnector()).getReceiver(this, |
623 | |
endpoint); |
624 | 0 | if (receiver != null) |
625 | |
{ |
626 | |
try |
627 | |
{ |
628 | 0 | receiver.connect(); |
629 | |
} |
630 | 0 | catch (Exception e) |
631 | |
{ |
632 | 0 | throw new ModelException( |
633 | |
MessageFactory.createStaticMessage("Failed to connect listener " |
634 | |
+ receiver + " for endpoint " + endpoint.getName()), |
635 | |
e); |
636 | 0 | } |
637 | |
} |
638 | |
} |
639 | 0 | } |
640 | |
|
641 | |
protected void disconnectListeners() throws UMOException |
642 | |
{ |
643 | |
UMOEndpoint endpoint; |
644 | 0 | List endpoints = getIncomingEndpoints(); |
645 | |
|
646 | 0 | for (Iterator it = endpoints.iterator(); it.hasNext();) |
647 | |
{ |
648 | 0 | endpoint = (UMOEndpoint) it.next(); |
649 | 0 | UMOMessageReceiver receiver = ((AbstractConnector) endpoint.getConnector()).getReceiver(this, |
650 | |
endpoint); |
651 | 0 | if (receiver != null) |
652 | |
{ |
653 | |
try |
654 | |
{ |
655 | 0 | receiver.disconnect(); |
656 | |
} |
657 | 0 | catch (Exception e) |
658 | |
{ |
659 | 0 | throw new ModelException( |
660 | |
MessageFactory.createStaticMessage("Failed to disconnect listener " |
661 | |
+ receiver + " for endpoint " + endpoint.getName()), |
662 | |
e); |
663 | 0 | } |
664 | |
} |
665 | |
} |
666 | 0 | } |
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
protected List getIncomingEndpoints() |
672 | |
{ |
673 | 0 | List endpoints = new ArrayList(); |
674 | |
|
675 | |
|
676 | 0 | endpoints.addAll(getDescriptor().getInboundRouter().getEndpoints()); |
677 | |
|
678 | 0 | if (getDescriptor().getInboundEndpoint() != null) |
679 | |
{ |
680 | 0 | endpoints.add(getDescriptor().getInboundEndpoint()); |
681 | |
} |
682 | |
|
683 | |
|
684 | 0 | if (getDescriptor().getResponseRouter() != null |
685 | |
&& getDescriptor().getResponseRouter().getEndpoints() != null) |
686 | |
{ |
687 | 0 | endpoints.addAll(getDescriptor().getResponseRouter().getEndpoints()); |
688 | |
} |
689 | 0 | return endpoints; |
690 | |
} |
691 | |
|
692 | |
} |