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