View Javadoc

1   /*
2    * $Id: ChainingRouter.java 22686 2011-08-16 19:39:20Z pablo.lagreca $
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 = event;
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, resultToReturn);
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 endpointResult = sendRequest(resultToReturn, intermediaryResult, endpoint, true);
78                      resultToReturn = endpointResult != null ? endpointResult : resultToReturn;
79                      MuleMessage localResult = endpointResult == null ? null : endpointResult.getMessage();
80                      // Need to propagate correlation info and replyTo, because there
81                      // is no guarantee that an external system will preserve headers
82                      // (in fact most will not)
83                      if (localResult != null &&
84                          // null result can be wrapped in a NullPayload
85                          localResult.getPayload() != NullPayload.getInstance() &&
86                          intermediaryResult != null)
87                      {
88                          processIntermediaryResult(localResult, intermediaryResult);
89                      }
90                      intermediaryResult = localResult;
91  
92                      if (logger.isDebugEnabled())
93                      {
94                          logger.debug("Received Chain result '" + i + "': "
95                                       + (intermediaryResult != null ? intermediaryResult.toString() : "null"));
96                      }
97  
98                      if (intermediaryResult == null || intermediaryResult.getPayload() == NullPayload.getInstance())
99                      {
100                         // if there was an error in the first link of the chain, make sure we propagate back
101                         // any exception payloads alongside the NullPayload
102                         resultToReturn = intermediaryResult == null ? null : new DefaultMuleEvent(intermediaryResult, resultToReturn);
103                         logger.warn("Chaining router cannot process any further targets. "
104                                     + "There was no result returned from endpoint invocation: " + endpoint);
105                         break;
106                     }
107                 }
108                 else
109                 {
110                     // ok, the last call,
111                     // use the 'sync/async' method parameter
112                     resultToReturn = sendRequest(resultToReturn, intermediaryResult, endpoint, true);
113                     if (logger.isDebugEnabled())
114                     {
115                         MuleMessage resultMessage = resultToReturn == null ? null : resultToReturn.getMessage();
116                         logger.debug("Received final Chain result '" + i + "': "
117                             + (resultMessage == null ? "null" : resultMessage.toString()));
118                     }
119                 }
120             }
121 
122         }
123         catch (MuleException e)
124         {
125             throw new CouldNotRouteOutboundMessageException(resultToReturn, endpoint, e);
126         }
127         return resultToReturn;
128     }
129 
130     /**
131      * Process intermediary result of invocation. The method will be invoked
132      * <strong>only</strong> if both local and intermediary results are available
133      * (not null).
134      * <p/>
135      * Overriding methods must call <code>super(localResult, intermediaryResult)</code>,
136      * unless they are modifying the correlation workflow (if you know what that means,
137      * you know what you are doing and when to do it).
138      * <p/>
139      * Default implementation propagates
140      * the following properties:
141      * <ul>
142      * <li>correlationId
143      * <li>correlationSequence
144      * <li>correlationGroupSize
145      * <li>replyTo
146      * </ul>
147      * @param localResult result of the last endpoint invocation
148      * @param intermediaryResult the message travelling across the targets
149      */
150     protected void processIntermediaryResult(MuleMessage localResult, MuleMessage intermediaryResult)
151     {
152         localResult.setCorrelationId(intermediaryResult.getCorrelationId());
153         localResult.setCorrelationSequence(intermediaryResult.getCorrelationSequence());
154         localResult.setCorrelationGroupSize(intermediaryResult.getCorrelationGroupSize());
155         localResult.setReplyTo(intermediaryResult.getReplyTo());
156     }
157 }