View Javadoc

1   /*
2    * $Id: DefaultServiceExceptionStrategy.java 11893 2008-06-02 13:39:57Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.service;
12  
13  import org.mule.DefaultExceptionStrategy;
14  import org.mule.RequestContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.endpoint.ImmutableEndpoint;
18  import org.mule.management.stats.ServiceStatistics;
19  import org.mule.util.CollectionUtils;
20  
21  import java.util.List;
22  
23  /**
24   * <code>DefaultServiceExceptionStrategy</code> is the default exception handler
25   * for components. The handler logs errors and will forward the message and exception
26   * to an exception endpointUri if one is set on this Exception strategy
27   */
28  public class DefaultServiceExceptionStrategy extends DefaultExceptionStrategy
29  {
30      public DefaultServiceExceptionStrategy()
31      {
32          super();
33      }
34  
35      protected void defaultHandler(Throwable t)
36      {
37          ServiceStatistics statistics = getServiceStatistics();
38  
39          if (statistics != null)
40          {
41              statistics.incExecutionError();
42          }
43  
44          super.defaultHandler(t);
45      }
46  
47      protected void logFatal(MuleMessage message, Throwable t)
48      {
49          super.logFatal(message, t);
50  
51          ServiceStatistics statistics = getServiceStatistics();
52          if (statistics != null)
53          {
54              statistics.incFatalError();
55          }
56      }
57  
58      protected void routeException(MuleMessage message, ImmutableEndpoint failedEndpoint, Throwable t)
59      {
60          super.routeException(message, failedEndpoint, t);
61          List endpoints = getEndpoints(t);
62          if (CollectionUtils.isNotEmpty(endpoints) && getServiceStatistics() != null)
63          {
64              ServiceStatistics statistics = getServiceStatistics();
65              for (int i = 0; i < endpoints.size(); i++)
66              {
67                  statistics.getOutboundRouterStat().incrementRoutedMessage((ImmutableEndpoint) endpoints.get(i));
68              }
69          }
70      }
71  
72      protected ServiceStatistics getServiceStatistics()
73      {
74          MuleEvent event = RequestContext.getEvent();
75          if (event == null)
76          {
77              // very bad should not happen
78              logger.fatal("The Default Service Exception Strategy has been invoked but there is no current event on the context");
79              //logger.fatal("The error is: " + t.getMessage(), t);
80              return null;
81          }
82          else if(event.getService()==null)
83          {
84              //this will ever happen, but JIC
85              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");            
86              return null;
87          }
88          else
89          {
90              return ((AbstractService)event.getService()).getStatistics();
91          }
92      }
93  }