KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > ForwardingCatchAllStrategy


1 /*
2  * $Id: ForwardingCatchAllStrategy.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.impl.MuleEvent;
16 import org.mule.impl.MuleMessage;
17 import org.mule.umo.UMOEvent;
18 import org.mule.umo.UMOMessage;
19 import org.mule.umo.UMOSession;
20 import org.mule.umo.provider.UMOMessageDispatcher;
21 import org.mule.umo.routing.ComponentRoutingException;
22 import org.mule.umo.routing.RoutingException;
23
24 /**
25  * <code>ForwardingCatchAllStrategy</code> acts as a catch and forward router for
26  * any events not caught by the router this strategy is associated with. Users can
27  * assign an endpoint to this strategy to forward all events to. This can be used as
28  * a dead letter/error queue.
29  *
30  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
31  * @version $Revision: 3798 $
32  */

33
34 public class ForwardingCatchAllStrategy extends AbstractCatchAllStrategy
35 {
36     private boolean sendTransformed = false;
37
38     public UMOMessage catchMessage(UMOMessage message, UMOSession session, boolean synchronous)
39         throws RoutingException
40     {
41         if (getEndpoint() == null)
42         {
43             throw new ComponentRoutingException(new Message(Messages.NO_CATCH_ALL_ENDPOINT_SET), message,
44                 getEndpoint(), session.getComponent());
45         }
46         try
47         {
48             UMOMessageDispatcher dispatcher = getEndpoint().getConnector().getDispatcher(getEndpoint());
49
50             if (sendTransformed && getEndpoint().getTransformer() != null)
51             {
52                 Object JavaDoc payload = message.getPayload();
53                 payload = getEndpoint().getTransformer().transform(payload);
54                 message = new MuleMessage(payload, message);
55             }
56             UMOEvent newEvent = new MuleEvent(message, getEndpoint(), session, synchronous);
57
58             if (synchronous)
59             {
60                 UMOMessage result = dispatcher.send(newEvent);
61                 if (statistics != null)
62                 {
63                     statistics.incrementRoutedMessage(getEndpoint());
64                 }
65                 return result;
66             }
67             else
68             {
69                 dispatcher.dispatch(newEvent);
70                 if (statistics != null)
71                 {
72                     statistics.incrementRoutedMessage(getEndpoint());
73                 }
74                 return null;
75             }
76         }
77         catch (Exception JavaDoc e)
78         {
79             throw new RoutingException(message, getEndpoint(), e);
80
81         }
82     }
83
84     public boolean isSendTransformed()
85     {
86         return sendTransformed;
87     }
88
89     public void setSendTransformed(boolean sendTransformed)
90     {
91         this.sendTransformed = sendTransformed;
92     }
93 }
94
Popular Tags