View Javadoc

1   /*
2    * $Id: ForwardingConsumer.java 10529 2008-01-25 05:58:36Z 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.inbound;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.DefaultMuleMessage;
15  import org.mule.api.MuleException;
16  import org.mule.api.MessagingException;
17  import org.mule.api.MuleEvent;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.routing.OutboundRouterCollection;
20  import org.mule.api.routing.RoutingException;
21  
22  /**
23   * <code>ForwardingConsumer</code> is used to forward an incoming event over
24   * another transport without invoking a service. This can be used to implement a
25   * bridge accross different transports.
26   */
27  public class ForwardingConsumer extends SelectiveConsumer
28  {
29  
30      public MuleEvent[] process(MuleEvent event) throws MessagingException
31      {
32          if (super.process(event) != null)
33          {
34              OutboundRouterCollection router = event.getService().getOutboundRouter();
35  
36              // Set the stopFurtherProcessing flag to true to inform the
37              // DefaultInboundRouterCollection not to route these events to the service
38              event.setStopFurtherProcessing(true);
39  
40              if (router == null)
41              {
42                  logger.debug("Descriptor has no outbound router configured to forward to, continuing with normal processing");
43                  return new MuleEvent[]{event};
44              }
45              else
46              {
47                  try
48                  {
49                      MuleMessage message = new DefaultMuleMessage(event.transformMessage(), event.getMessage());
50  
51                      MuleMessage response = router.route(message, event.getSession(), event.isSynchronous());
52                      // TODO What's the correct behaviour for async endpoints?
53                      // maybe let router.route() return a Future for the returned msg?
54                      if (response != null)
55                      {
56                          return new MuleEvent[]{new DefaultMuleEvent(response, event)};
57                      }
58                      else
59                      {
60                          return null;
61                      }
62  
63                  }
64                  catch (MuleException e)
65                  {
66                      throw new RoutingException(event.getMessage(), event.getEndpoint(), e);
67                  }
68              }
69          }
70          return null;
71      }
72  }