1
2
3
4
5
6
7
8
9
10
11 package org.mule.exception;
12
13 import org.mule.RequestContext;
14 import org.mule.api.MuleContext;
15 import org.mule.api.exception.SystemExceptionHandler;
16 import org.mule.api.transport.Connectable;
17 import org.mule.context.notification.ExceptionNotification;
18 import org.mule.message.DefaultExceptionPayload;
19 import org.mule.transport.ConnectException;
20
21 import java.lang.reflect.InvocationTargetException;
22
23
24
25
26 public class DefaultSystemExceptionStrategy extends AbstractExceptionListener implements SystemExceptionHandler
27 {
28
29
30
31
32 public DefaultSystemExceptionStrategy()
33 {
34 super();
35 }
36
37 public DefaultSystemExceptionStrategy(MuleContext muleContext)
38 {
39 super();
40 setMuleContext(muleContext);
41 }
42
43 public void handleException(Exception e)
44 {
45 Connectable connectable = null;
46
47
48 if (e instanceof InvocationTargetException)
49 {
50 Throwable t = e.getCause();
51
52 e = t instanceof Exception ? (Exception) t : new Exception(t);
53 }
54
55 if (enableNotifications)
56 {
57 fireNotification(new ExceptionNotification(e));
58 }
59
60 if (e instanceof ConnectException)
61 {
62 logger.info("Exception caught is a ConnectException, attempting to reconnect...");
63 connectable = ((ConnectException) e).getFailed();
64 try
65 {
66 logger.debug("Disconnecting " + connectable.getClass().getName());
67 connectable.disconnect();
68 }
69 catch (Exception e1)
70 {
71 logger.error(e1.getMessage());
72 }
73 }
74
75 logException(e);
76
77 handleTransaction(e);
78
79 if (RequestContext.getEvent() != null)
80 {
81 RequestContext.setExceptionPayload(new DefaultExceptionPayload(e));
82 }
83
84 if (connectable != null)
85 {
86
87 try
88 {
89 logger.debug("Reconnecting " + connectable.getClass().getName());
90 connectable.connect();
91 }
92 catch (Exception e2)
93 {
94 logger.error(e2.getMessage());
95 }
96 }
97 }
98 }