View Javadoc

1   /*
2    * $Id: ChainingRouter.java 19739 2010-09-27 14:28:40Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.outbound;
12  
13  import org.mule.DefaultMuleEvent;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
20  import org.mule.api.routing.RoutePathNotFoundException;
21  import org.mule.api.routing.RoutingException;
22  import org.mule.config.i18n.CoreMessages;
23  import org.mule.transport.NullPayload;
24  
25  /**
26   * <code>ChainingRouter</code> is used to pass a Mule event through multiple
27   * targets using the result of the first as the input for the second.
28   */
29  public class ChainingRouter extends FilteringOutboundRouter
30  {
31      @Override
32      public void initialise() throws InitialisationException
33      {
34          super.initialise();
35          if (routes == null || routes.size() == 0)
36          {
37              throw new InitialisationException(CoreMessages.objectIsNull("targets"), this);
38          }
39      }
40  
41      @Override
42      public MuleEvent route(MuleEvent event) throws RoutingException
43      {
44          MuleEvent resultToReturn = null;
45          if (routes == null || routes.size() == 0)
46          {
47              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null);
48          }
49  
50          final int endpointsCount = routes.size();
51          if (logger.isDebugEnabled())
52          {
53              logger.debug("About to chain " + endpointsCount + " targets.");
54          }
55  
56          // need that ref for an error message
57          MessageProcessor endpoint = null;
58          try
59          {
60              MuleMessage intermediaryResult = event.getMessage();
61  
62              for (int i = 0; i < endpointsCount; i++)
63              {
64                  endpoint = getRoute(i, event);
65                  // if it's not the last endpoint in the chain,
66                  // enforce the synchronous call, otherwise we lose response
67                  boolean lastEndpointInChain = (i == endpointsCount - 1);
68  
69                  if (logger.isDebugEnabled())
70                  {
71                      logger.debug("Sending Chained message '" + i + "': "
72                                   + (intermediaryResult == null ? "null" : intermediaryResult.toString()));
73                  }
74  
75                  if (!lastEndpointInChain)
76                  {
77                      MuleEvent event1 = sendRequest(event, intermediaryResult, endpoint, true);
78                      MuleMessage localResult = event1 == null ? null : event1.getMessage();
79                      // Need to propagate correlation info and replyTo, because there
80                      // is no guarantee that an external system will preserve headers
81                      // (in fact most will not)
82                      if (localResult != null &&
83                          // null result can be wrapped in a NullPayload
84                          localResult.getPayload() != NullPayload.getInstance() &&
85                          intermediaryResult != null)
86                      {
87                          processIntermediaryResult(localResult, intermediaryResult);
88                      }
89                      intermediaryResult = localResult;
90  
91                      if (logger.isDebugEnabled())
92                      {
93                          logger.debug("Received Chain result '" + i + "': "
94                                       + (intermediaryResult != null ? intermediaryResult.toString() : "null"));
95                      }
96  
97                      if (intermediaryResult == null || intermediaryResult.getPayload() == NullPayload.getInstance())
98                      {
99                          // if there was an error in the first link of the chain, make sure we propagate back
100                         // any exception payloads alongside the NullPayload
101                         resultToReturn = intermediaryResult == null ? null : new DefaultMuleEvent(intermediaryResult, event);
102                         logger.warn("Chaining router cannot process any further targets. "
103                                     + "There was no result returned from endpoint invocation: " + endpoint);
104                         break;
105                     }
106                 }
107                 else
108                 {
109                     // ok, the last call,
110                     // use the 'sync/async' method parameter
111                     resultToReturn = sendRequest(event, intermediaryResult, endpoint, true);
112                     if (logger.isDebugEnabled())
113                     {
114                         MuleMessage resultMessage = resultToReturn == null ? null : resultToReturn.getMessage();
115                         logger.debug("Received final Chain result '" + i + "': "
116                             + (resultMessage == null ? "null" : resultMessage.toString()));
117                     }
118                 }
119             }
120 
121         }
122         catch (MuleException e)
123         {
124             throw new CouldNotRouteOutboundMessageException(event, endpoint, e);
125         }
126         return resultToReturn;
127     }
128 
129     /**
130      * Process intermediary result of invocation. The method will be invoked
131      * <strong>only</strong> if both local and intermediary results are available
132      * (not null).
133      * <p/>
134      * Overriding methods must call <code>super(localResult, intermediaryResult)</code>,
135      * unless they are modifying the correlation workflow (if you know what that means,
136      * you know what you are doing and when to do it).
137      * <p/>
138      * Default implementation propagates
139      * the following properties:
140      * <ul>
141      * <li>correlationId
142      * <li>correlationSequence
143      * <li>correlationGroupSize
144      * <li>replyTo
145      * </ul>
146      * @param localResult result of the last endpoint invocation
147      * @param intermediaryResult the message travelling across the targets
148      */
149     protected void processIntermediaryResult(MuleMessage localResult, MuleMessage intermediaryResult)
150     {
151         localResult.setCorrelationId(intermediaryResult.getCorrelationId());
152         localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence());
153         localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize());
154         localResult.setReplyTo(intermediaryResult.getReplyTo());
155     }
156 }