View Javadoc

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