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