View Javadoc

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