01: /*
02: * $Id: ForwardingConsumer.java 10529 2008-01-25 05:58:36Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: public MuleEvent[] process(MuleEvent event)
30: throws MessagingException {
31: if (super .process(event) != null) {
32: OutboundRouterCollection router = event.getService()
33: .getOutboundRouter();
34:
35: // Set the stopFurtherProcessing flag to true to inform the
36: // DefaultInboundRouterCollection not to route these events to the service
37: event.setStopFurtherProcessing(true);
38:
39: if (router == null) {
40: logger
41: .debug("Descriptor has no outbound router configured to forward to, continuing with normal processing");
42: return new MuleEvent[] { event };
43: } else {
44: try {
45: MuleMessage message = new DefaultMuleMessage(event
46: .transformMessage(), event.getMessage());
47:
48: MuleMessage response = router.route(message, event
49: .getSession(), event.isSynchronous());
50: // TODO What's the correct behaviour for async endpoints?
51: // maybe let router.route() return a Future for the returned msg?
52: if (response != null) {
53: return new MuleEvent[] { new DefaultMuleEvent(
54: response, event) };
55: } else {
56: return null;
57: }
58:
59: } catch (MuleException e) {
60: throw new RoutingException(event.getMessage(),
61: event.getEndpoint(), e);
62: }
63: }
64: }
65: return null;
66: }
67: }
|