1
2
3
4
5
6
7
8
9
10
11 package org.mule.tck.testmodels.mule;
12
13 import org.mule.api.MuleEvent;
14 import org.mule.api.exception.SystemExceptionHandler;
15 import org.mule.exception.AbstractMessagingExceptionStrategy;
16
17 import java.util.ArrayList;
18 import java.util.LinkedList;
19 import java.util.List;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25
26
27
28
29 public class TestExceptionStrategy extends AbstractMessagingExceptionStrategy
30 implements SystemExceptionHandler
31 {
32
33
34
35 protected final Log logger = LogFactory.getLog(getClass());
36
37
38
39
40
41 private Object callbackLock = new Object();
42
43
44 private ExceptionCallback callback;
45
46 private List<Exception> unhandled = new LinkedList<Exception>();
47
48 private volatile String testProperty;
49
50 public String getTestProperty()
51 {
52 return testProperty;
53 }
54
55 public void setTestProperty(String testProperty)
56 {
57 this.testProperty = testProperty;
58 }
59
60 @Override
61 public MuleEvent handleException(Exception exception, MuleEvent event)
62 {
63 ExceptionCallback callback = null;
64 synchronized (callbackLock)
65 {
66 if (this.callback != null)
67 {
68 callback = this.callback;
69 }
70 else
71 {
72 unhandled.add(exception);
73 }
74 }
75
76
77
78 logger.info("Handling exception: " + exception.getClass().getName());
79 if (callback != null)
80 {
81 logger.info("Exception caught on TestExceptionStrategy and was sent to callback.", exception);
82 callback.onException(exception);
83 }
84 else
85 {
86 logger.info("Exception caught on TestExceptionStrategy but there was no callback set.", exception);
87 }
88 return event;
89 }
90
91 public void handleException(Exception exception)
92 {
93 handleException(exception, null);
94 }
95
96 public interface ExceptionCallback
97 {
98 void onException(Throwable t);
99 }
100
101 public void setExceptionCallback(ExceptionCallback exceptionCallback)
102 {
103 synchronized (callbackLock)
104 {
105 this.callback = exceptionCallback;
106 }
107 processUnhandled();
108 }
109
110 protected void processUnhandled()
111 {
112 List<Exception> unhandledCopies = null;
113 ExceptionCallback callback = null;
114 synchronized (callbackLock)
115 {
116 if (this.callback != null)
117 {
118 callback = this.callback;
119 unhandledCopies = new ArrayList<Exception>(unhandled);
120 unhandled.clear();
121 }
122 }
123
124
125
126 if (callback != null && unhandledCopies != null)
127 {
128 for (Exception exception : unhandledCopies)
129 {
130 logger.info("Handling exception after setting the callback.", exception);
131 callback.onException(exception);
132 }
133 }
134 }
135 }