View Javadoc

1   /*
2    * $Id: DefaultComponentExceptionStrategy.java 11728 2008-05-13 07:31:11Z dirk.olmes $
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.impl;
12  
13  import org.mule.impl.model.AbstractComponent;
14  import org.mule.management.stats.ComponentStatistics;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.UMOEvent;
17  import org.mule.umo.UMOMessage;
18  import org.mule.umo.endpoint.UMOEndpoint;
19  import org.mule.umo.endpoint.UMOImmutableEndpoint;
20  
21  /**
22   * <code>DefaultComponentExceptionStrategy</code> is the default exception handler
23   * for components. The handler logs errors and will forward the message and exception
24   * to an exception endpointUri if one is set on this Exception strategy
25   */
26  public class DefaultComponentExceptionStrategy extends DefaultExceptionStrategy
27  {
28      /**
29       * The component to which the Exception handler belongs
30       */
31      protected UMOComponent component;
32  
33      protected ComponentStatistics statistics;
34  
35      public DefaultComponentExceptionStrategy()
36      {
37          super();
38      }
39  
40      /**
41       * Constructor
42       * 
43       * @param component the owner of this exception strategy
44       * @see DefaultLifecycleAdapter
45       */
46      public DefaultComponentExceptionStrategy(UMOComponent component)
47      {
48          super();
49          setComponent(component);
50      }
51  
52      /**
53       * @return the UniversalMessageObject to which this handler is attached
54       */
55      public UMOComponent getComponent()
56      {
57          return component;
58      }
59  
60      protected void defaultHandler(Throwable t)
61      {
62          // Lazy initialisation of the component
63          // This strategy should be associated with only one component
64          // and thus there is no concurrency problem
65          if (component == null)
66          {
67              UMOEvent event = RequestContext.getEvent();
68              if (event == null)
69              {
70                  // very bad should not happen
71                  logger.fatal("The Default Component Exception Strategy has been invoked but there is no current event on the context");
72                  logger.fatal("The error is: " + t.getMessage(), t);
73              }
74              else
75              {
76                  setComponent(event.getComponent());
77              }
78          }
79  
80          if (statistics != null)
81          {
82              statistics.incExecutionError();
83          }
84          
85          super.defaultHandler(t);
86      }
87  
88      protected void logFatal(UMOMessage message, Throwable t)
89      {
90          super.logFatal(message, t);
91          if (statistics != null)
92          {
93              statistics.incFatalError();
94          }
95      }
96  
97      protected void routeException(UMOMessage message, UMOImmutableEndpoint failedEndpoint, Throwable t)
98      {
99          UMOEndpoint ep = getEndpoint(t);
100         if (ep != null)
101         {
102             super.routeException(message, failedEndpoint, t);
103             if (statistics != null)
104             {
105                 statistics.getOutboundRouterStat().incrementRoutedMessage(ep);
106             }
107         }
108     }
109 
110     public void setComponent(UMOComponent component)
111     {
112         this.component = component;
113         if (component instanceof AbstractComponent)
114         {
115             this.statistics = ((AbstractComponent) component).getStatistics();
116         }
117     }
118 }