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