View Javadoc

1   /*
2    * $Id: AbstractCatchAllStrategy.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.routing;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.routing.OutboundRouterCatchAllStrategy;
15  import org.mule.api.routing.RouterStatisticsRecorder;
16  import org.mule.api.routing.RoutingException;
17  import org.mule.management.stats.RouterStatistics;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  
23  /**
24   * <code>RouterCatchAllStrategy</code> is a strategy interface that allows developers to hook in custom code when
25   * an event is being routed on the inbound or outbound but does not match any of the criteria defined for the routing.
26   *
27   * Think of catch all strategies as a safety net for your events to ensure that all events will get processed.  If you
28   * do not use conditional routing logic, you will not need a catch all strategy.
29   *
30   * Note that it is advised to use this base class over the {@link org.mule.api.routing.OutboundRouterCatchAllStrategy} interface
31   * so that the {@link org.mule.management.stats.RouterStatistics} are available.
32   *
33   * @see org.mule.routing.LoggingCatchAllStrategy
34   * @see org.mule.routing.ForwardingCatchAllStrategy
35   */
36  public abstract class AbstractCatchAllStrategy implements OutboundRouterCatchAllStrategy, RouterStatisticsRecorder
37  {
38      /**
39       * logger used by this class
40       */
41      protected transient Log logger = LogFactory.getLog(getClass());
42  
43      /** Router statistics used to monitor if a catch all strategy is invoked and if any events are dispatched
44       * from the strategy i.e. from the {@link org.mule.routing.ForwardingCatchAllStrategy}.
45       */
46      protected RouterStatistics statistics;
47  
48  
49      public RouterStatistics getRouterStatistics()
50      {
51          return statistics;
52      }
53  
54      public void setRouterStatistics(RouterStatistics statistics)
55      {
56          this.statistics = statistics;
57      }
58  
59      /**
60       * This method will be invoked when an event is received or being sent where the criteria of the router(s) do not
61       * match the current event.
62       *
63       * @return A result message from this processing. Depending on the messaging style being used this might become the
64       *         response message to a client or remote service call.
65       * @throws org.mule.api.routing.RoutingException
66       *          if there is a failure while processing this message.
67       */
68      public final MuleEvent process(MuleEvent event) throws RoutingException
69      {
70          if(getRouterStatistics()!=null)
71          {
72              getRouterStatistics().incrementCaughtMessage();
73          }
74          else
75          {
76              logger.warn("Routing statistics not set on catch all strategy, this invocation will not be recorded.");
77          }
78          return doCatchMessage(event);
79      }
80  
81      public abstract MuleEvent doCatchMessage(MuleEvent event) throws RoutingException;
82  
83  }