1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing;
12
13 import org.mule.api.MessagingException;
14 import org.mule.api.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.MuleMessage;
17 import org.mule.api.lifecycle.InitialisationException;
18 import org.mule.api.processor.MessageProcessor;
19 import org.mule.api.routing.CouldNotRouteOutboundMessageException;
20 import org.mule.routing.filters.ExpressionFilter;
21 import org.mule.routing.outbound.AbstractOutboundRouter;
22
23
24
25
26
27
28
29 public class FirstSuccessful extends AbstractOutboundRouter
30 {
31
32 protected String failureExpression;
33 protected ExpressionFilter failureExpressionFilter;
34
35 @Override
36 public void initialise() throws InitialisationException
37 {
38 super.initialise();
39 if (failureExpression != null)
40 {
41 failureExpressionFilter = new ExpressionFilter(failureExpression);
42 }
43 else
44 {
45 failureExpressionFilter = new ExpressionFilter("exception-type:");
46 }
47 failureExpressionFilter.setMuleContext(muleContext);
48 }
49
50
51
52
53 @Override
54 public MuleEvent route(MuleEvent event) throws MessagingException
55 {
56 MuleEvent returnEvent = null;
57
58 boolean failed = true;
59 for (MessageProcessor mp : routes)
60 {
61 try
62 {
63 MuleEvent toProcess = cloneEventForRoutinng(event, mp);
64 returnEvent = mp.process(toProcess);
65
66 if (returnEvent == null)
67 {
68 failed = false;
69 }
70 else
71 {
72 MuleMessage msg = returnEvent.getMessage();
73 failed = msg == null || failureExpressionFilter.accept(msg);
74 }
75 }
76 catch (Exception ex)
77 {
78 failed = true;
79 }
80 if (!failed)
81 {
82 break;
83 }
84 }
85
86 if (failed)
87 {
88 throw new CouldNotRouteOutboundMessageException(event, this);
89 }
90
91 return returnEvent;
92 }
93
94 protected MuleEvent cloneEventForRoutinng(MuleEvent event, MessageProcessor mp)
95 {
96 return createEventToRoute(event, cloneMessage(event.getMessage()), mp);
97 }
98
99 @Override
100 public boolean isMatch(MuleMessage message) throws MuleException
101 {
102 return true;
103 }
104
105
106
107
108
109
110
111
112 public void setFailureExpression(String failureExpression)
113 {
114 this.failureExpression = failureExpression;
115 }
116 }