View Javadoc

1   /*
2    * $Id: DefaultServiceExceptionStrategy.java 19191 2010-08-25 21:05:23Z tcarlson $
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.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   * <code>DefaultServiceExceptionStrategy</code> is the default exception handler
26   * for components. The handler logs errors and will forward the message and exception
27   * to an exception endpointUri if one is set on this Exception strategy
28   */
29  public class DefaultServiceExceptionStrategy extends AbstractMessagingExceptionStrategy
30  {
31      /** 
32       * For IoC only 
33       * @deprecated Use DefaultServiceExceptionStrategy(MuleContext muleContext) instead
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              // very bad should not happen
92              logger.fatal("The Default Service Exception Strategy has been invoked but there is no current event on the context");
93              //logger.fatal("The error is: " + t.getMessage(), t);
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             //this will ever happen, but JIC
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 }