View Javadoc

1   /*
2    * $Id: DefaultServiceExceptionStrategy.java 20465 2010-12-05 18:14:15Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * <code>DefaultServiceExceptionStrategy</code> is the default exception handler
28   * for components. The handler logs errors and will forward the message and exception
29   * to an exception endpointUri if one is set on this Exception strategy
30   */
31  public class DefaultServiceExceptionStrategy extends AbstractMessagingExceptionStrategy
32  {
33      /** 
34       * For IoC only 
35       * @deprecated Use DefaultServiceExceptionStrategy(MuleContext muleContext) instead
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              // very bad should not happen
97              logger.fatal("The Default Service Exception Strategy has been invoked but there is no current event on the context");
98              //logger.fatal("The error is: " + t.getMessage(), t);
99              return null;
100         }
101         else if(event.getFlowConstruct()!= null )
102         {
103             return event.getFlowConstruct().getStatistics();
104         }
105         else
106         {
107             //this can happen, e.g. with event constructed to handle exceptions
108             // logger.fatal("The Default Service Exception Strategy has been invoked but there is no current flow construct on the context. Please report this to dev@mule.codehaus.org");
109             return null;
110         }
111     }
112 
113     protected ServiceStatistics getServiceStatistics()
114     {
115         FlowConstructStatistics stats = getFlowConstructStatistics();
116         if (!(stats instanceof ServiceStatistics))
117         {
118             //this should never happen, but JIC
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 }