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