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.MuleEvent;
16 import org.mule.api.MuleMessage;
17 import org.mule.api.processor.MessageProcessor;
18 import org.mule.api.service.Service;
19 import org.mule.management.stats.ServiceStatistics;
20 import org.mule.util.CollectionUtils;
21
22 import java.util.List;
23
24
25
26
27
28
29 public class DefaultServiceExceptionStrategy extends AbstractMessagingExceptionStrategy
30 {
31
32
33
34
35 public DefaultServiceExceptionStrategy()
36 {
37 super();
38 }
39
40 public DefaultServiceExceptionStrategy(MuleContext muleContext)
41 {
42 super();
43 setMuleContext(muleContext);
44 }
45
46 @Override
47 protected void defaultHandler(Throwable t)
48 {
49 ServiceStatistics statistics = getServiceStatistics();
50
51 if (statistics != null)
52 {
53 statistics.incExecutionError();
54 }
55
56 super.defaultHandler(t);
57 }
58
59 @Override
60 protected void logFatal(MuleMessage message, Throwable t)
61 {
62 ServiceStatistics statistics = getServiceStatistics();
63 if (statistics != null)
64 {
65 statistics.incFatalError();
66 }
67
68 super.logFatal(message, t);
69 }
70
71 @Override
72 protected void routeException(MuleMessage message, MessageProcessor target, Throwable t)
73 {
74 super.routeException(message, target, t);
75 List<MessageProcessor> processors = getMessageProcessors(t);
76 if (CollectionUtils.isNotEmpty(processors) && getServiceStatistics() != null)
77 {
78 ServiceStatistics statistics = getServiceStatistics();
79 for (MessageProcessor endpoint : processors)
80 {
81 statistics.getOutboundRouterStat().incrementRoutedMessage(endpoint);
82 }
83 }
84 }
85
86 protected ServiceStatistics getServiceStatistics()
87 {
88 MuleEvent event = RequestContext.getEvent();
89 if (event == null)
90 {
91
92 logger.fatal("The Default Service Exception Strategy has been invoked but there is no current event on the context");
93
94 return null;
95 }
96 else if(event.getFlowConstruct()!=null && event.getFlowConstruct() instanceof Service)
97 {
98 return ((Service) event.getFlowConstruct()).getStatistics();
99 }
100 else
101 {
102
103 logger.fatal("The Default Service Exception Strategy has been invoked but there is no current service on the context. Please report this to dev@mule.codehaus.org");
104 return null;
105 }
106 }
107 }