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