1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.spring.events; |
8 | |
|
9 | |
import org.mule.DefaultMuleEvent; |
10 | |
import org.mule.DefaultMuleMessage; |
11 | |
import org.mule.MessageExchangePattern; |
12 | |
import org.mule.RequestContext; |
13 | |
import org.mule.api.MuleContext; |
14 | |
import org.mule.api.MuleEventContext; |
15 | |
import org.mule.api.MuleException; |
16 | |
import org.mule.api.MuleRuntimeException; |
17 | |
import org.mule.api.MuleSession; |
18 | |
import org.mule.api.config.MuleProperties; |
19 | |
import org.mule.api.config.ThreadingProfile; |
20 | |
import org.mule.api.context.MuleContextAware; |
21 | |
import org.mule.api.endpoint.EndpointBuilder; |
22 | |
import org.mule.api.endpoint.EndpointFactory; |
23 | |
import org.mule.api.endpoint.EndpointURI; |
24 | |
import org.mule.api.endpoint.InboundEndpoint; |
25 | |
import org.mule.api.endpoint.MalformedEndpointException; |
26 | |
import org.mule.api.endpoint.OutboundEndpoint; |
27 | |
import org.mule.api.lifecycle.Callable; |
28 | |
import org.mule.api.lifecycle.Initialisable; |
29 | |
import org.mule.api.lifecycle.InitialisationException; |
30 | |
import org.mule.api.model.Model; |
31 | |
import org.mule.api.routing.filter.ObjectFilter; |
32 | |
import org.mule.api.service.Service; |
33 | |
import org.mule.api.source.CompositeMessageSource; |
34 | |
import org.mule.api.transformer.Transformer; |
35 | |
import org.mule.api.transformer.TransformerException; |
36 | |
import org.mule.api.transport.Connector; |
37 | |
import org.mule.component.DefaultJavaComponent; |
38 | |
import org.mule.config.QueueProfile; |
39 | |
import org.mule.endpoint.MuleEndpointURI; |
40 | |
import org.mule.model.seda.SedaModel; |
41 | |
import org.mule.model.seda.SedaService; |
42 | |
import org.mule.module.spring.i18n.SpringMessages; |
43 | |
import org.mule.object.SingletonObjectFactory; |
44 | |
import org.mule.routing.filters.WildcardFilter; |
45 | |
import org.mule.service.ServiceCompositeMessageSource; |
46 | |
import org.mule.session.DefaultMuleSession; |
47 | |
import org.mule.util.ClassUtils; |
48 | |
|
49 | |
import java.beans.ExceptionListener; |
50 | |
import java.util.ArrayList; |
51 | |
import java.util.Iterator; |
52 | |
import java.util.List; |
53 | |
import java.util.Map; |
54 | |
import java.util.Set; |
55 | |
|
56 | |
import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArraySet; |
57 | |
import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService; |
58 | |
|
59 | |
import org.apache.commons.logging.Log; |
60 | |
import org.apache.commons.logging.LogFactory; |
61 | |
import org.springframework.beans.BeansException; |
62 | |
import org.springframework.context.ApplicationContext; |
63 | |
import org.springframework.context.ApplicationContextAware; |
64 | |
import org.springframework.context.ApplicationEvent; |
65 | |
import org.springframework.context.ApplicationListener; |
66 | |
import org.springframework.context.event.ApplicationEventMulticaster; |
67 | |
import org.springframework.context.event.ContextClosedEvent; |
68 | |
import org.springframework.context.event.ContextRefreshedEvent; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 0 | public class MuleEventMulticaster |
118 | |
implements ApplicationEventMulticaster, ApplicationContextAware, MuleContextAware, Callable, Initialisable |
119 | |
{ |
120 | |
public static final String EVENT_MULTICASTER_DESCRIPTOR_NAME = "muleEventMulticasterDescriptor"; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 0 | protected static final Log logger = LogFactory.getLog(MuleEventMulticaster.class); |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | 0 | protected final Set listeners = new CopyOnWriteArraySet(); |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | 0 | protected boolean asynchronous = false; |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 0 | protected ExecutorService asyncPool = null; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | 0 | protected String[] subscriptions = null; |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
protected ApplicationContext applicationContext; |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
protected Service service; |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | 0 | protected Class subscriptionFilter = WildcardFilter.class; |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | 0 | protected ExceptionListener exceptionListener = new LoggingExceptionListener(); |
170 | |
|
171 | |
protected MuleContext muleContext; |
172 | |
|
173 | |
public void setMuleContext(MuleContext context) |
174 | |
{ |
175 | 0 | this.muleContext = context; |
176 | 0 | } |
177 | |
|
178 | |
public void initialise() throws InitialisationException |
179 | |
{ |
180 | 0 | if (asynchronous) |
181 | |
{ |
182 | 0 | if (asyncPool == null) |
183 | |
{ |
184 | 0 | asyncPool = muleContext.getDefaultThreadingProfile().createPool("spring-events"); |
185 | |
} |
186 | |
} |
187 | |
else |
188 | |
{ |
189 | 0 | if (asyncPool != null) |
190 | |
{ |
191 | 0 | asyncPool.shutdown(); |
192 | 0 | asyncPool = null; |
193 | |
} |
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public void addApplicationListener(ApplicationListener listener) |
209 | |
{ |
210 | 0 | Object listenerToAdd = listener; |
211 | |
|
212 | 0 | if (asynchronous) |
213 | |
{ |
214 | 0 | listenerToAdd = new AsynchronousEventListener(asyncPool, listener); |
215 | |
} |
216 | |
|
217 | 0 | listeners.add(listenerToAdd); |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public void removeApplicationListener(ApplicationListener listener) |
226 | |
{ |
227 | 0 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) |
228 | |
{ |
229 | 0 | ApplicationListener applicationListener = (ApplicationListener) iterator.next(); |
230 | 0 | if (applicationListener instanceof AsynchronousEventListener) |
231 | |
{ |
232 | 0 | if (((AsynchronousEventListener) applicationListener).getListener().equals(listener)) |
233 | |
{ |
234 | 0 | listeners.remove(applicationListener); |
235 | 0 | return; |
236 | |
} |
237 | |
} |
238 | |
else |
239 | |
{ |
240 | 0 | if (applicationListener.equals(listener)) |
241 | |
{ |
242 | 0 | listeners.remove(applicationListener); |
243 | 0 | return; |
244 | |
} |
245 | |
} |
246 | 0 | } |
247 | 0 | listeners.remove(listener); |
248 | 0 | } |
249 | |
|
250 | |
|
251 | |
public void addApplicationListenerBean(String s) |
252 | |
{ |
253 | 0 | Object listener = applicationContext.getBean(s); |
254 | 0 | if(listener instanceof ApplicationListener) |
255 | |
{ |
256 | 0 | addApplicationListener((ApplicationListener)listener); |
257 | |
} |
258 | |
else |
259 | |
{ |
260 | 0 | throw new IllegalArgumentException(SpringMessages.beanNotInstanceOfApplicationListener(s).getMessage()); |
261 | |
} |
262 | 0 | } |
263 | |
|
264 | |
public void removeApplicationListenerBean(String s) |
265 | |
{ |
266 | 0 | Object listener = applicationContext.getBean(s); |
267 | 0 | if(listener instanceof ApplicationListener) |
268 | |
{ |
269 | 0 | removeApplicationListener((ApplicationListener)listener); |
270 | |
} |
271 | |
else |
272 | |
{ |
273 | 0 | throw new IllegalArgumentException(SpringMessages.beanNotInstanceOfApplicationListener(s).getMessage()); |
274 | |
} |
275 | 0 | } |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
public void removeAllListeners() |
281 | |
{ |
282 | 0 | listeners.clear(); |
283 | 0 | } |
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public void multicastEvent(ApplicationEvent e) |
299 | |
{ |
300 | 0 | MuleApplicationEvent muleEvent = null; |
301 | |
|
302 | 0 | if (e instanceof ContextRefreshedEvent) |
303 | |
{ |
304 | |
try |
305 | |
{ |
306 | 0 | registerMulticasterComponent(); |
307 | |
} |
308 | 0 | catch (MuleException ex) |
309 | |
{ |
310 | 0 | throw new MuleRuntimeException(SpringMessages.failedToReinitMule(), ex); |
311 | 0 | } |
312 | |
} |
313 | 0 | else if (e instanceof ContextClosedEvent) |
314 | |
{ |
315 | 0 | if (!muleContext.isDisposing() && !muleContext.isDisposed()) |
316 | |
{ |
317 | 0 | muleContext.dispose(); |
318 | |
} |
319 | 0 | return; |
320 | |
} |
321 | 0 | else if (e instanceof MuleApplicationEvent) |
322 | |
{ |
323 | 0 | muleEvent = (MuleApplicationEvent) e; |
324 | |
|
325 | |
|
326 | 0 | if (muleEvent.getMuleEventContext() == null) |
327 | |
{ |
328 | |
try |
329 | |
{ |
330 | 0 | dispatchEvent(muleEvent); |
331 | |
} |
332 | 0 | catch (ApplicationEventException e1) |
333 | |
{ |
334 | 0 | exceptionListener.exceptionThrown(e1); |
335 | 0 | } |
336 | 0 | return; |
337 | |
} |
338 | |
} |
339 | |
|
340 | 0 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) |
341 | |
{ |
342 | 0 | ApplicationListener listener = (ApplicationListener) iterator.next(); |
343 | 0 | if (muleEvent != null) |
344 | |
{ |
345 | |
|
346 | |
|
347 | |
|
348 | 0 | if (listener instanceof AsynchronousEventListener) |
349 | |
{ |
350 | 0 | AsynchronousEventListener asyncListener = (AsynchronousEventListener) listener; |
351 | 0 | if (asyncListener.getListener() instanceof MuleSubscriptionEventListener) |
352 | |
{ |
353 | 0 | if (isSubscriptionMatch(muleEvent.getEndpoint(), |
354 | |
((MuleSubscriptionEventListener) asyncListener.getListener()).getSubscriptions())) |
355 | |
{ |
356 | 0 | asyncListener.onApplicationEvent(muleEvent); |
357 | |
} |
358 | |
} |
359 | 0 | else if (asyncListener.getListener() instanceof MuleEventListener) |
360 | |
{ |
361 | 0 | asyncListener.onApplicationEvent(muleEvent); |
362 | |
} |
363 | 0 | else if (!(asyncListener.getListener() instanceof MuleEventListener)) |
364 | |
{ |
365 | 0 | asyncListener.onApplicationEvent(e); |
366 | |
} |
367 | |
|
368 | 0 | } |
369 | 0 | else if (listener instanceof MuleSubscriptionEventListener) |
370 | |
{ |
371 | 0 | if (isSubscriptionMatch(muleEvent.getEndpoint(), |
372 | |
((MuleSubscriptionEventListener) listener).getSubscriptions())) |
373 | |
{ |
374 | 0 | listener.onApplicationEvent(muleEvent); |
375 | |
} |
376 | |
} |
377 | 0 | else if (listener instanceof MuleEventListener) |
378 | |
{ |
379 | 0 | listener.onApplicationEvent(muleEvent); |
380 | |
} |
381 | |
} |
382 | 0 | else if (listener instanceof AsynchronousEventListener |
383 | |
&& !(((AsynchronousEventListener) listener).getListener() instanceof MuleEventListener)) |
384 | |
{ |
385 | 0 | listener.onApplicationEvent(e); |
386 | |
} |
387 | 0 | else if (!(listener instanceof MuleEventListener)) |
388 | |
{ |
389 | 0 | listener.onApplicationEvent(e); |
390 | |
} |
391 | |
else |
392 | |
{ |
393 | |
|
394 | |
|
395 | 0 | for (int i = 0; i < listener.getClass().getInterfaces().length; i++) |
396 | |
{ |
397 | 0 | if (listener.getClass().getInterfaces()[i].equals(ApplicationListener.class)) |
398 | |
{ |
399 | 0 | listener.onApplicationEvent(e); |
400 | 0 | break; |
401 | |
} |
402 | |
} |
403 | |
|
404 | |
} |
405 | 0 | } |
406 | 0 | } |
407 | |
|
408 | |
|
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
private boolean isSubscriptionMatch(String endpoint, String[] subscriptions) |
416 | |
{ |
417 | 0 | for (int i = 0; i < subscriptions.length; i++) |
418 | |
{ |
419 | 0 | String subscription = subscriptions[i]; |
420 | |
|
421 | |
|
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | 0 | ObjectFilter filter = createFilter(subscription); |
439 | 0 | if (filter.accept(endpoint)) |
440 | |
{ |
441 | 0 | return true; |
442 | |
} |
443 | |
} |
444 | 0 | return false; |
445 | |
} |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
public boolean isAsynchronous() |
453 | |
{ |
454 | 0 | return asynchronous; |
455 | |
} |
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
public void setAsynchronous(boolean asynchronous) |
463 | |
{ |
464 | 0 | this.asynchronous = asynchronous; |
465 | 0 | } |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
public Object onCall(MuleEventContext context) throws TransformerException, MalformedEndpointException |
474 | |
{ |
475 | 0 | multicastEvent(new MuleApplicationEvent(context.getMessage().getPayload(), context, applicationContext)); |
476 | 0 | context.setStopFurtherProcessing(true); |
477 | 0 | return null; |
478 | |
} |
479 | |
|
480 | |
|
481 | |
|
482 | |
|
483 | |
|
484 | |
|
485 | |
|
486 | |
|
487 | |
protected void dispatchEvent(MuleApplicationEvent applicationEvent) throws ApplicationEventException |
488 | |
{ |
489 | |
OutboundEndpoint endpoint; |
490 | |
try |
491 | |
{ |
492 | 0 | endpoint = muleContext.getEndpointFactory().getOutboundEndpoint( |
493 | |
applicationEvent.getEndpoint()); |
494 | |
} |
495 | 0 | catch (MuleException e) |
496 | |
{ |
497 | 0 | throw new ApplicationEventException("Failed to get endpoint for endpointUri: " |
498 | |
+ applicationEvent.getEndpoint(), e); |
499 | 0 | } |
500 | 0 | if (endpoint != null) |
501 | |
{ |
502 | |
try |
503 | |
{ |
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | 0 | DefaultMuleMessage message = new DefaultMuleMessage(applicationEvent.getSource(), |
509 | |
applicationEvent.getProperties(), muleContext); |
510 | |
|
511 | |
|
512 | 0 | if (applicationEvent.getMuleEventContext() != null) |
513 | |
{ |
514 | |
|
515 | 0 | applicationEvent.getMuleEventContext().setStopFurtherProcessing(true); |
516 | 0 | applicationEvent.getMuleEventContext().dispatchEvent(message, endpoint); |
517 | |
} |
518 | |
else |
519 | |
{ |
520 | 0 | MuleSession session = new DefaultMuleSession(service, muleContext); |
521 | 0 | DefaultMuleEvent event = new DefaultMuleEvent(message, endpoint, session); |
522 | 0 | RequestContext.setEvent(event); |
523 | |
|
524 | 0 | if (endpoint.getTransformers() != null) |
525 | |
{ |
526 | 0 | message = new DefaultMuleMessage(applicationEvent.getSource(), |
527 | |
applicationEvent.getProperties(), muleContext); |
528 | 0 | message.applyTransformers(event, endpoint.getTransformers()); |
529 | |
} |
530 | 0 | endpoint.process(new DefaultMuleEvent(message, endpoint, session)); |
531 | |
} |
532 | |
} |
533 | 0 | catch (Exception e1) |
534 | |
{ |
535 | 0 | throw new ApplicationEventException("Failed to dispatch event: " + e1.getMessage(), e1); |
536 | 0 | } |
537 | |
} |
538 | |
else |
539 | |
{ |
540 | 0 | throw new ApplicationEventException("Failed endpoint using name: " |
541 | |
+ applicationEvent.getEndpoint()); |
542 | |
} |
543 | 0 | } |
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException |
552 | |
{ |
553 | 0 | this.applicationContext = applicationContext; |
554 | 0 | } |
555 | |
|
556 | |
protected void registerMulticasterComponent() throws MuleException |
557 | |
{ |
558 | |
|
559 | 0 | if (service == null) |
560 | |
{ |
561 | 0 | service = getDefaultService(); |
562 | 0 | setSubscriptionsOnService(service); |
563 | 0 | muleContext.getRegistry().registerService(service); |
564 | |
} |
565 | 0 | } |
566 | |
|
567 | |
protected void setSubscriptionsOnService(Service service) throws MuleException |
568 | |
{ |
569 | |
String[] subscriptions; |
570 | 0 | List endpoints = new ArrayList(); |
571 | 0 | for (Iterator iterator = listeners.iterator(); iterator.hasNext();) |
572 | |
{ |
573 | 0 | ApplicationListener listener = (ApplicationListener) iterator.next(); |
574 | 0 | if (listener instanceof AsynchronousEventListener) |
575 | |
{ |
576 | 0 | listener = ((AsynchronousEventListener) listener).getListener(); |
577 | |
} |
578 | 0 | if (listener instanceof MuleSubscriptionEventListener) |
579 | |
{ |
580 | 0 | subscriptions = ((MuleSubscriptionEventListener) listener).getSubscriptions(); |
581 | 0 | for (int i = 0; i < subscriptions.length; i++) |
582 | |
{ |
583 | 0 | if (subscriptions[i].indexOf("*") == -1 && MuleEndpointURI.isMuleUri(subscriptions[i])) |
584 | |
{ |
585 | 0 | boolean isSoap = registerAsSoap(subscriptions[i], listener); |
586 | |
|
587 | 0 | if (!isSoap) |
588 | |
{ |
589 | 0 | endpoints.add(subscriptions[i]); |
590 | |
} |
591 | |
} |
592 | |
} |
593 | |
} |
594 | 0 | } |
595 | 0 | if (endpoints.size() > 0) |
596 | |
{ |
597 | |
String endpoint; |
598 | 0 | for (Iterator iterator = endpoints.iterator(); iterator.hasNext();) |
599 | |
{ |
600 | 0 | endpoint = (String) iterator.next(); |
601 | |
|
602 | 0 | InboundEndpoint ep = muleContext.getEndpointFactory().getInboundEndpoint( |
603 | |
endpoint); |
604 | |
|
605 | |
|
606 | |
|
607 | 0 | if (((ServiceCompositeMessageSource) service.getMessageSource()).getEndpoint(ep.getName()) == null) |
608 | |
{ |
609 | 0 | ((ServiceCompositeMessageSource) service.getMessageSource()).addSource(ep); |
610 | |
} |
611 | 0 | } |
612 | |
} |
613 | 0 | } |
614 | |
|
615 | |
private boolean registerAsSoap(String endpoint, Object listener) throws MuleException |
616 | |
{ |
617 | 0 | if (endpoint.startsWith("soap") || endpoint.startsWith("axis") || endpoint.startsWith("cxf")) |
618 | |
{ |
619 | 0 | EndpointURI ep = new MuleEndpointURI(endpoint, muleContext); |
620 | |
|
621 | |
|
622 | 0 | String serviceName = null; |
623 | 0 | if (ep.getPath() != null) |
624 | |
{ |
625 | 0 | String path = ep.getPath(); |
626 | 0 | if (path.endsWith("/")) |
627 | |
{ |
628 | 0 | path = path.substring(0, path.length() - 1); |
629 | |
} |
630 | 0 | int i = path.lastIndexOf("/"); |
631 | 0 | if (i > -1) |
632 | |
{ |
633 | 0 | serviceName = path.substring(i + 1); |
634 | |
} |
635 | 0 | } |
636 | |
else |
637 | |
{ |
638 | 0 | serviceName = service.getName(); |
639 | |
} |
640 | |
|
641 | 0 | String newEndpoint = endpoint; |
642 | 0 | int i = newEndpoint.indexOf(serviceName); |
643 | 0 | newEndpoint = newEndpoint.substring(0, i - 1); |
644 | 0 | SedaService s = new SedaService(muleContext); |
645 | 0 | s.setName(serviceName); |
646 | 0 | s.setModel(muleContext.getRegistry().lookupSystemModel()); |
647 | 0 | s.setQueueProfile(new QueueProfile()); |
648 | 0 | ((CompositeMessageSource) s.getMessageSource()).addSource( |
649 | |
muleContext.getEndpointFactory().getInboundEndpoint(newEndpoint)); |
650 | 0 | final DefaultJavaComponent component = new DefaultJavaComponent(new SingletonObjectFactory(listener)); |
651 | 0 | component.setMuleContext(muleContext); |
652 | 0 | s.setComponent(component); |
653 | 0 | muleContext.getRegistry().registerService(s); |
654 | 0 | return true; |
655 | |
} |
656 | |
else |
657 | |
{ |
658 | 0 | return false; |
659 | |
} |
660 | |
} |
661 | |
|
662 | |
protected void registerConnectors() throws MuleException |
663 | |
{ |
664 | 0 | if (!muleContext.isInitialised()) |
665 | |
{ |
666 | |
|
667 | 0 | Map connectors = applicationContext.getBeansOfType(Connector.class, true, true); |
668 | 0 | if (connectors.size() > 0) |
669 | |
{ |
670 | |
Map.Entry entry; |
671 | |
Connector c; |
672 | 0 | for (Iterator iterator = connectors.entrySet().iterator(); iterator.hasNext();) |
673 | |
{ |
674 | 0 | entry = (Map.Entry) iterator.next(); |
675 | 0 | c = (Connector) entry.getValue(); |
676 | 0 | if (c.getName() == null) |
677 | |
{ |
678 | 0 | c.setName(entry.getKey().toString()); |
679 | |
} |
680 | 0 | muleContext.getRegistry().registerConnector(c); |
681 | |
} |
682 | |
} |
683 | |
} |
684 | 0 | } |
685 | |
|
686 | |
protected void registerTransformers() throws MuleException |
687 | |
{ |
688 | 0 | if (!muleContext.isInitialised()) |
689 | |
{ |
690 | |
|
691 | 0 | Map transformers = applicationContext.getBeansOfType(Transformer.class, true, true); |
692 | 0 | if (transformers.size() > 0) |
693 | |
{ |
694 | |
Map.Entry entry; |
695 | |
Transformer t; |
696 | 0 | for (Iterator iterator = transformers.entrySet().iterator(); iterator.hasNext();) |
697 | |
{ |
698 | 0 | entry = (Map.Entry) iterator.next(); |
699 | 0 | t = (Transformer) entry.getValue(); |
700 | 0 | if (t.getName() == null) |
701 | |
{ |
702 | 0 | t.setName(entry.getKey().toString()); |
703 | |
} |
704 | 0 | muleContext.getRegistry().registerTransformer(t); |
705 | |
} |
706 | |
} |
707 | |
} |
708 | 0 | } |
709 | |
|
710 | |
protected Service getDefaultService() throws MuleException |
711 | |
{ |
712 | |
|
713 | |
|
714 | 0 | Model model = muleContext.getRegistry().lookupModel(MuleProperties.OBJECT_SYSTEM_MODEL); |
715 | 0 | if (model == null) |
716 | |
{ |
717 | 0 | model = new SedaModel(); |
718 | 0 | model.setName(MuleProperties.OBJECT_SYSTEM_MODEL); |
719 | 0 | muleContext.getRegistry().registerModel(model); |
720 | |
} |
721 | 0 | Service service = muleContext.getRegistry().lookupService(EVENT_MULTICASTER_DESCRIPTOR_NAME); |
722 | 0 | if (service != null) |
723 | |
{ |
724 | 0 | muleContext.getRegistry().unregisterService(service.getName()); |
725 | |
} |
726 | 0 | service = new SedaService(muleContext); |
727 | 0 | service.setName(EVENT_MULTICASTER_DESCRIPTOR_NAME); |
728 | 0 | service.setModel(model); |
729 | 0 | if (subscriptions == null) |
730 | |
{ |
731 | 0 | logger.info("No receive endpoints have been set, using default '*'"); |
732 | 0 | ((CompositeMessageSource) service.getMessageSource()).addSource( |
733 | |
muleContext.getEndpointFactory().getInboundEndpoint("vm://*")); |
734 | |
} |
735 | |
else |
736 | |
{ |
737 | |
|
738 | 0 | ServiceCompositeMessageSource messageRouter = (ServiceCompositeMessageSource) service.getMessageSource(); |
739 | |
|
740 | 0 | for (int i = 0; i < subscriptions.length; i++) |
741 | |
{ |
742 | 0 | String subscription = subscriptions[i]; |
743 | |
|
744 | 0 | EndpointFactory endpointFactory = muleContext.getEndpointFactory(); |
745 | 0 | EndpointBuilder endpointBuilder = endpointFactory.getEndpointBuilder(subscription); |
746 | 0 | endpointBuilder.setExchangePattern(MessageExchangePattern.fromSyncFlag(!asynchronous)); |
747 | 0 | InboundEndpoint endpoint = endpointFactory.getInboundEndpoint(endpointBuilder); |
748 | |
|
749 | 0 | messageRouter.addSource(endpoint); |
750 | |
} |
751 | |
} |
752 | 0 | DefaultJavaComponent component = new DefaultJavaComponent(new SingletonObjectFactory(this)); |
753 | 0 | component.setMuleContext(muleContext); |
754 | 0 | service.setComponent(component); |
755 | 0 | return service; |
756 | |
} |
757 | |
|
758 | |
protected ObjectFilter createFilter(String pattern) |
759 | |
{ |
760 | |
try |
761 | |
{ |
762 | 0 | if (getSubscriptionFilter() == null) |
763 | |
{ |
764 | 0 | setSubscriptionFilter(WildcardFilter.class); |
765 | |
} |
766 | 0 | return (ObjectFilter) ClassUtils.instanciateClass(getSubscriptionFilter(), pattern); |
767 | |
} |
768 | 0 | catch (Exception e) |
769 | |
{ |
770 | 0 | exceptionListener.exceptionThrown(e); |
771 | 0 | return new WildcardFilter(pattern); |
772 | |
} |
773 | |
} |
774 | |
|
775 | |
|
776 | |
|
777 | |
|
778 | |
|
779 | |
|
780 | |
|
781 | |
public Class getSubscriptionFilter() |
782 | |
{ |
783 | 0 | return subscriptionFilter; |
784 | |
} |
785 | |
|
786 | |
|
787 | |
|
788 | |
|
789 | |
|
790 | |
|
791 | |
public void setSubscriptionFilter(Class subscriptionFilter) |
792 | |
{ |
793 | 0 | this.subscriptionFilter = subscriptionFilter; |
794 | 0 | } |
795 | |
|
796 | |
|
797 | |
|
798 | |
|
799 | |
|
800 | |
|
801 | |
|
802 | |
|
803 | |
|
804 | |
|
805 | |
public String[] getSubscriptions() |
806 | |
{ |
807 | 0 | return subscriptions; |
808 | |
} |
809 | |
|
810 | |
|
811 | |
|
812 | |
|
813 | |
|
814 | |
|
815 | |
|
816 | |
|
817 | |
|
818 | |
|
819 | |
public void setSubscriptions(String[] subscriptions) |
820 | |
{ |
821 | 0 | this.subscriptions = subscriptions; |
822 | 0 | } |
823 | |
|
824 | |
protected void setExceptionListener(ExceptionListener listener) |
825 | |
{ |
826 | 0 | if (listener != null) |
827 | |
{ |
828 | 0 | this.exceptionListener = listener; |
829 | |
} |
830 | |
else |
831 | |
{ |
832 | 0 | throw new IllegalArgumentException("exceptionListener may not be null"); |
833 | |
} |
834 | 0 | } |
835 | |
|
836 | 0 | private static class LoggingExceptionListener implements ExceptionListener |
837 | |
{ |
838 | |
public LoggingExceptionListener() |
839 | |
{ |
840 | 0 | super(); |
841 | 0 | } |
842 | |
|
843 | |
public void exceptionThrown(Exception e) |
844 | |
{ |
845 | 0 | logger.error(e.getMessage(), e); |
846 | 0 | } |
847 | |
} |
848 | |
} |