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