1
2
3
4
5
6
7 package org.mule.exception;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.MuleEvent;
11 import org.mule.api.MuleException;
12 import org.mule.api.construct.FlowConstruct;
13 import org.mule.api.lifecycle.Lifecycle;
14 import org.mule.api.lifecycle.Stoppable;
15 import org.mule.api.processor.MessageProcessor;
16 import org.mule.config.DefaultMuleConfiguration;
17 import org.mule.config.ExceptionHelper;
18 import org.mule.management.stats.FlowConstructStatistics;
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 private boolean stopMessageProcessing;
33
34
35
36
37
38 public DefaultServiceExceptionStrategy()
39 {
40 super();
41 }
42
43 public DefaultServiceExceptionStrategy(MuleContext muleContext)
44 {
45 super();
46 setMuleContext(muleContext);
47 }
48
49 @Override
50 protected void doHandleException(Exception e, MuleEvent event)
51 {
52 FlowConstructStatistics statistics = getFlowConstructStatistics(event.getFlowConstruct());
53
54 if (statistics != null && statistics.isEnabled())
55 {
56 statistics.incExecutionError();
57 }
58
59 super.doHandleException(DefaultMuleConfiguration.fullStackTraces ? e : (Exception) ExceptionHelper.sanitize(e), event);
60 }
61
62 @Override
63 protected void logFatal(MuleEvent event, Throwable t)
64 {
65 FlowConstructStatistics statistics = getFlowConstructStatistics(event.getFlowConstruct());
66 if (statistics != null && statistics.isEnabled())
67 {
68 statistics.incFatalError();
69 }
70
71 super.logFatal(event, t);
72 }
73
74 @Override
75 protected void routeException(MuleEvent event, MessageProcessor target, Throwable t)
76 {
77 super.routeException(event, target, t);
78 List<MessageProcessor> processors = getMessageProcessors();
79 if (CollectionUtils.isNotEmpty(processors) && getFlowConstructStatistics(event.getFlowConstruct()) instanceof ServiceStatistics)
80 {
81 ServiceStatistics statistics = getServiceStatistics(event.getFlowConstruct());
82 if (statistics.isEnabled())
83 {
84 for (MessageProcessor endpoint : processors)
85 {
86 statistics.getOutboundRouterStat().incrementRoutedMessage(endpoint);
87 }
88 }
89 }
90
91 if (stopMessageProcessing)
92 {
93 stopFlowConstruct();
94 }
95 }
96
97 private void stopFlowConstruct()
98 {
99 if (flowConstruct instanceof Stoppable)
100 {
101 logger.info("Stopping flow '" + flowConstruct.getName() + "' due to exception");
102
103 try
104 {
105 ((Lifecycle) flowConstruct).stop();
106 }
107 catch (MuleException e)
108 {
109 logger.error("Unable to stop flow '" + flowConstruct.getName() + "'", e);
110 }
111 }
112 else
113 {
114 logger.warn("Flow is not stoppable");
115 }
116 }
117
118 protected FlowConstructStatistics getFlowConstructStatistics(FlowConstruct flowConstruct)
119 {
120 if (flowConstruct != null )
121 {
122 return flowConstruct.getStatistics();
123 }
124 else
125 {
126
127
128 return null;
129 }
130 }
131
132 protected ServiceStatistics getServiceStatistics(FlowConstruct flowConstruct)
133 {
134 FlowConstructStatistics stats = getFlowConstructStatistics(flowConstruct);
135 if (!(stats instanceof ServiceStatistics))
136 {
137
138 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");
139 return null;
140 }
141 return (ServiceStatistics) stats;
142 }
143
144 public boolean isStopMessageProcessing()
145 {
146 return stopMessageProcessing;
147 }
148
149 public void setStopMessageProcessing(boolean stopMessageProcessing)
150 {
151 this.stopMessageProcessing = stopMessageProcessing;
152 }
153 }