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