1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule; |
8 | |
|
9 | |
import org.mule.api.MuleContext; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleException; |
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.construct.FlowConstruct; |
14 | |
import org.mule.api.construct.FlowConstructAware; |
15 | |
import org.mule.api.context.MuleContextAware; |
16 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
17 | |
import org.mule.api.lifecycle.Disposable; |
18 | |
import org.mule.api.lifecycle.Initialisable; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.lifecycle.Lifecycle; |
21 | |
import org.mule.api.lifecycle.Stoppable; |
22 | |
import org.mule.api.routing.OutboundRouter; |
23 | |
import org.mule.exception.AbstractMessagingExceptionStrategy; |
24 | |
import org.mule.message.DefaultExceptionPayload; |
25 | |
import org.mule.session.DefaultMuleSession; |
26 | |
import org.mule.transport.NullPayload; |
27 | |
import org.mule.util.ObjectUtils; |
28 | |
|
29 | |
import org.apache.commons.lang.exception.ExceptionUtils; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class RouteableExceptionStrategy extends AbstractMessagingExceptionStrategy implements FlowConstructAware, Lifecycle |
40 | |
{ |
41 | |
|
42 | |
private OutboundRouter router; |
43 | |
|
44 | 0 | private boolean stopFurtherProcessing = true; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
public MuleEvent handleException(Exception e, MuleEvent event) |
51 | |
{ |
52 | 0 | int currentRootExceptionHashCode = 0; |
53 | 0 | int originalRootExceptionHashCode = 0; |
54 | 0 | MuleMessage msg = null; |
55 | |
|
56 | 0 | StringBuffer logInfo = new StringBuffer(); |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 0 | logInfo.append("****++******Alternate Exception Strategy******++*******\n"); |
61 | 0 | logInfo.append("Current Thread = " + Thread.currentThread().toString() + "\n"); |
62 | |
|
63 | 0 | if (event != null && event.getFlowConstruct() != null) |
64 | |
{ |
65 | 0 | String serviceName = event.getFlowConstruct().getName(); |
66 | 0 | logInfo.append("serviceName = " + serviceName + "\n"); |
67 | |
|
68 | 0 | int eventHashCode = event.hashCode(); |
69 | 0 | logInfo.append("eventHashCode = " + eventHashCode + "\n"); |
70 | |
} |
71 | |
|
72 | 0 | if (event != null && event.isStopFurtherProcessing()) |
73 | |
{ |
74 | 0 | logInfo.append("MuleEvent stop further processing has been set, This is probably the same exception being routed again. no Exception routing will be performed.\n" |
75 | |
+ e |
76 | |
+ "\n"); |
77 | 0 | event.getMessage().setPayload(NullPayload.getInstance()); |
78 | 0 | event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e)); |
79 | 0 | return event; |
80 | |
} |
81 | |
|
82 | 0 | Throwable root = ExceptionUtils.getRootCause(e); |
83 | 0 | currentRootExceptionHashCode = root == null ? -1 : root.hashCode(); |
84 | |
|
85 | 0 | msg = event == null ? null : event.getMessage(); |
86 | |
|
87 | 0 | if (msg != null) |
88 | |
{ |
89 | 0 | int msgHashCode = msg.hashCode(); |
90 | 0 | logInfo.append("msgHashCode = " + msgHashCode + "\n"); |
91 | |
|
92 | 0 | if (msg.getExceptionPayload() != null) |
93 | |
{ |
94 | 0 | Throwable t = msg.getExceptionPayload().getRootException(); |
95 | 0 | if (t != null && t.hashCode() == currentRootExceptionHashCode) |
96 | |
{ |
97 | 0 | logInfo.append("*#*#*#*#*\n"); |
98 | 0 | logInfo.append("This error has already been handeled, returning without doing anything: " |
99 | |
+ e.getMessage() |
100 | |
+ "\n"); |
101 | 0 | logInfo.append("*#*#*#*#*\n"); |
102 | 0 | originalRootExceptionHashCode = currentRootExceptionHashCode; |
103 | 0 | event.getMessage().setPayload(NullPayload.getInstance()); |
104 | 0 | event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e)); |
105 | 0 | return event; |
106 | |
} |
107 | |
} |
108 | |
|
109 | 0 | originalRootExceptionHashCode = msg.getIntProperty("RootExceptionHashCode", 0); |
110 | |
|
111 | 0 | logInfo.append("Original RootExceptionHashCode: " + originalRootExceptionHashCode + "\n"); |
112 | 0 | logInfo.append("Current RootExceptionHashCode: " + currentRootExceptionHashCode + "\n"); |
113 | |
|
114 | 0 | if (originalRootExceptionHashCode == 0) |
115 | |
{ |
116 | 0 | msg.setIntProperty("RootExceptionHashCode", currentRootExceptionHashCode); |
117 | 0 | originalRootExceptionHashCode = currentRootExceptionHashCode; |
118 | |
} |
119 | 0 | else if (originalRootExceptionHashCode == currentRootExceptionHashCode) |
120 | |
{ |
121 | 0 | logInfo.append("*#*#*#*#*\n"); |
122 | 0 | logInfo.append("This error has already been handeled, returning without doing anything: " |
123 | |
+ e.getMessage() |
124 | |
+ "\n"); |
125 | 0 | logInfo.append("*#*#*#*#*\n"); |
126 | 0 | event.getMessage().setPayload(NullPayload.getInstance()); |
127 | 0 | event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e)); |
128 | 0 | return event; |
129 | |
} |
130 | |
else |
131 | |
{ |
132 | 0 | msg.setIntProperty("RootExceptionHashCode", currentRootExceptionHashCode); |
133 | |
} |
134 | |
} |
135 | |
|
136 | 0 | logInfo.append(e.getMessage()); |
137 | |
|
138 | 0 | StackTraceElement[] st = e.getStackTrace(); |
139 | 0 | for (int i = 0; i < st.length; i++) |
140 | |
{ |
141 | 0 | if (st[i].getClassName().equals("org.mule.AlternateExceptionStrategy")) |
142 | |
{ |
143 | 0 | logger.warn("*#*#*#*#*\n" |
144 | |
+ "Recursive error in AlternateExceptionStrategy " |
145 | |
+ e |
146 | |
+ "\n" |
147 | |
+ "*#*#*#*#*"); |
148 | 0 | event.getMessage().setPayload(NullPayload.getInstance()); |
149 | 0 | event.getMessage().setExceptionPayload(new DefaultExceptionPayload(e)); |
150 | 0 | return event; |
151 | |
} |
152 | 0 | logger.debug(st[i].toString()); |
153 | |
} |
154 | 0 | return super.handleException(e, event); |
155 | |
} |
156 | |
finally |
157 | |
{ |
158 | 0 | if (event != null && this.stopFurtherProcessing) event.setStopFurtherProcessing(true); |
159 | |
|
160 | 0 | if (msg != null && currentRootExceptionHashCode != 0 |
161 | |
&& currentRootExceptionHashCode != originalRootExceptionHashCode) |
162 | 0 | msg.setIntProperty("RootExceptionHashCode", currentRootExceptionHashCode); |
163 | |
|
164 | 0 | logInfo.append("****__******Alternate Exception Strategy******__*******\n"); |
165 | 0 | logger.debug(logInfo.toString()); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public void handleMessagingException(MuleMessage message, Throwable t) |
173 | |
{ |
174 | 0 | defaultHandler(message, t); |
175 | 0 | routeException(getMessageFromContext(message), (ImmutableEndpoint) null, t); |
176 | 0 | } |
177 | |
|
178 | |
public void handleRoutingException(MuleMessage message, ImmutableEndpoint endpoint, Throwable t) |
179 | |
{ |
180 | 0 | defaultHandler(message, t); |
181 | 0 | routeException(getMessageFromContext(message), endpoint, t); |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public void handleLifecycleException(Object component, Throwable t) |
188 | |
{ |
189 | 0 | logger.error("The object that failed is: \n" + ObjectUtils.toString(component, "null")); |
190 | 0 | handleStandardException(t); |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
public void handleStandardException(Throwable t) |
197 | |
{ |
198 | 0 | handleTransaction(t); |
199 | 0 | if (RequestContext.getEvent() != null) |
200 | |
{ |
201 | 0 | handleMessagingException(RequestContext.getEvent().getMessage(), t); |
202 | |
} |
203 | |
else |
204 | |
{ |
205 | 0 | logger.info("There is no current event available, routing Null message with the exception"); |
206 | 0 | handleMessagingException(new DefaultMuleMessage(NullPayload.getInstance(), muleContext), t); |
207 | |
} |
208 | 0 | } |
209 | |
|
210 | |
protected void defaultHandler(MuleMessage message, Throwable t) |
211 | |
{ |
212 | 0 | if (RequestContext.getEvent() != null && RequestContext.getEvent().getMessage() != null) |
213 | |
{ |
214 | 0 | RequestContext.getEvent().getMessage().setExceptionPayload(new DefaultExceptionPayload(t)); |
215 | |
} |
216 | |
|
217 | 0 | if (message != null) message.setExceptionPayload(new DefaultExceptionPayload(t)); |
218 | 0 | } |
219 | |
|
220 | |
protected MuleMessage getMessageFromContext(MuleMessage message) |
221 | |
{ |
222 | 0 | if (RequestContext.getEvent() != null) |
223 | |
{ |
224 | 0 | return RequestContext.getEvent().getMessage(); |
225 | |
} |
226 | 0 | else if (message != null) |
227 | |
{ |
228 | 0 | return message; |
229 | |
} |
230 | |
else |
231 | |
{ |
232 | 0 | return new DefaultMuleMessage(NullPayload.getInstance(), muleContext); |
233 | |
} |
234 | |
} |
235 | |
|
236 | |
protected void routeException(MuleMessage msg, ImmutableEndpoint failedEndpoint, Throwable t) |
237 | |
{ |
238 | 0 | MuleMessage contextMsg = null; |
239 | 0 | MuleEvent exceptionEvent = RequestContext.getEvent(); |
240 | 0 | contextMsg = exceptionEvent == null ? msg : exceptionEvent.getMessage(); |
241 | |
|
242 | 0 | if (contextMsg == null) |
243 | |
{ |
244 | 0 | contextMsg = new DefaultMuleMessage(NullPayload.getInstance(), muleContext); |
245 | 0 | contextMsg.setExceptionPayload(new DefaultExceptionPayload(t)); |
246 | |
} |
247 | |
|
248 | 0 | if (exceptionEvent == null) |
249 | |
{ |
250 | 0 | exceptionEvent = new DefaultMuleEvent(contextMsg, failedEndpoint, new DefaultMuleSession(muleContext)); |
251 | |
} |
252 | |
|
253 | |
|
254 | 0 | DefaultMuleMessage messageCopy = new DefaultMuleMessage(contextMsg.getPayload(), contextMsg, muleContext); |
255 | |
|
256 | |
|
257 | |
try |
258 | |
{ |
259 | 0 | router.process(exceptionEvent); |
260 | |
} |
261 | 0 | catch (MuleException e) |
262 | |
{ |
263 | 0 | logFatal(exceptionEvent, e); |
264 | 0 | } |
265 | 0 | } |
266 | |
|
267 | |
public OutboundRouter getRouter() |
268 | |
{ |
269 | 0 | return router; |
270 | |
} |
271 | |
|
272 | |
public void setRouter(OutboundRouter router) |
273 | |
{ |
274 | 0 | this.router = router; |
275 | 0 | } |
276 | |
|
277 | |
public boolean isStopFurtherProcessing() |
278 | |
{ |
279 | 0 | return stopFurtherProcessing; |
280 | |
} |
281 | |
|
282 | |
public void setStopFurtherProcessing(boolean stopFurtherProcessing) |
283 | |
{ |
284 | 0 | this.stopFurtherProcessing = stopFurtherProcessing; |
285 | 0 | } |
286 | |
|
287 | |
@Override |
288 | |
public void setFlowConstruct(FlowConstruct flowConstruct) |
289 | |
{ |
290 | 0 | if (router instanceof FlowConstructAware) |
291 | |
{ |
292 | 0 | router.setFlowConstruct(flowConstruct); |
293 | |
} |
294 | 0 | } |
295 | |
|
296 | |
@Override |
297 | |
public void setMuleContext(MuleContext context) |
298 | |
{ |
299 | 0 | super.setMuleContext(context); |
300 | 0 | if (router instanceof MuleContextAware) |
301 | |
{ |
302 | 0 | router.setMuleContext(context); |
303 | |
} |
304 | 0 | } |
305 | |
|
306 | |
@Override |
307 | |
protected void doInitialise(MuleContext muleContext) throws InitialisationException |
308 | |
{ |
309 | 0 | super.doInitialise(muleContext); |
310 | 0 | if (router instanceof Initialisable) |
311 | |
{ |
312 | 0 | router.initialise(); |
313 | |
} |
314 | 0 | } |
315 | |
|
316 | |
@Override |
317 | |
public void dispose() |
318 | |
{ |
319 | 0 | super.dispose(); |
320 | 0 | if (router instanceof Disposable) |
321 | |
{ |
322 | 0 | router.dispose(); |
323 | |
} |
324 | 0 | } |
325 | |
|
326 | |
@Override |
327 | |
public void stop() throws MuleException |
328 | |
{ |
329 | 0 | if (router instanceof Stoppable) |
330 | |
{ |
331 | 0 | router.stop(); |
332 | |
} |
333 | 0 | } |
334 | |
|
335 | |
@Override |
336 | |
public void start() throws MuleException |
337 | |
{ |
338 | 0 | if (router instanceof Stoppable) |
339 | |
{ |
340 | 0 | router.stop(); |
341 | |
} |
342 | 0 | } |
343 | |
} |