View Javadoc

1   /*
2    * $Id: ForwardingCatchAllStrategy.java 22156 2011-06-08 21:36:30Z dfeist $
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.endpoint.OutboundEndpoint;
15  import org.mule.api.processor.MessageProcessor;
16  import org.mule.api.routing.RoutingException;
17  import org.mule.config.i18n.CoreMessages;
18  
19  /**
20   * <code>ForwardingCatchAllStrategy</code> acts as a catch and forward router for
21   * any events not caught by the router this strategy is associated with. Users can
22   * assign an endpoint to this strategy to forward all events to. This can be used as
23   * a dead letter/error queue.
24   *
25   */
26  public class ForwardingCatchAllStrategy extends AbstractCatchAllStrategy
27  {
28      private boolean sendTransformed = false;
29  
30      protected OutboundEndpoint endpoint;
31  
32      public void setEndpoint(OutboundEndpoint endpoint)
33      {
34          this.endpoint = endpoint;
35      }
36      
37      public void setMessageProcessor(MessageProcessor processor)
38      {
39          this.endpoint = (OutboundEndpoint) processor;
40      }
41  
42      public OutboundEndpoint getEndpoint()
43      {
44          return endpoint;
45      }
46  
47      @Override
48      public MuleEvent doCatchMessage(MuleEvent event) throws RoutingException
49      {
50          if (getEndpoint() == null)
51          {
52              throw new RoutingException(CoreMessages.noCatchAllEndpointSet(), event, getEndpoint());
53          }
54          try
55          {
56              OutboundEndpoint endpoint = getEndpoint();
57              if (sendTransformed && endpoint.getTransformers() != null)
58              {
59                  event.getMessage().applyTransformers(event, endpoint.getTransformers());
60              }
61  
62              MuleEvent result = endpoint.process(event);
63              if (statistics != null && statistics.isEnabled())
64              {
65                  statistics.incrementRoutedMessage(getEndpoint());
66              }
67              return result;
68          }
69          catch (Exception e)
70          {
71              throw new RoutingException(event, getEndpoint(), e);
72  
73          }
74      }
75  
76      public boolean isSendTransformed()
77      {
78          return sendTransformed;
79      }
80  
81      public void setSendTransformed(boolean sendTransformed)
82      {
83          this.sendTransformed = sendTransformed;
84      }
85  }