View Javadoc

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