Coverage Report - org.mule.routing.FirstSuccessful
 
Classes in this File Line Coverage Branch Coverage Complexity
FirstSuccessful
0%
0/28
0%
0/14
0
 
 1  
 /*
 2  
  * $Id: FirstSuccessful.java 20471 2010-12-06 15:45:03Z 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;
 12  
 
 13  
 import org.mule.api.MessagingException;
 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.routing.filters.ExpressionFilter;
 21  
 import org.mule.routing.outbound.AbstractOutboundRouter;
 22  
 
 23  
 /**
 24  
  * FirstSuccessful routes an event to the first target route that can accept it
 25  
  * without throwing or returning an exception. If no such route can be found, an
 26  
  * exception is thrown. Note that this works more reliable with synchronous targets,
 27  
  * but no such restriction is imposed.
 28  
  */
 29  0
 public class FirstSuccessful extends AbstractOutboundRouter
 30  
 {
 31  
 
 32  
     protected String failureExpression;
 33  
     protected ExpressionFilter failureExpressionFilter;
 34  
 
 35  
     @Override
 36  
     public void initialise() throws InitialisationException
 37  
     {
 38  0
         super.initialise();
 39  0
         if (failureExpression != null)
 40  
         {
 41  0
             failureExpressionFilter = new ExpressionFilter(failureExpression);
 42  
         }
 43  
         else
 44  
         {
 45  0
             failureExpressionFilter = new ExpressionFilter("exception-type:");
 46  
         }
 47  0
         failureExpressionFilter.setMuleContext(muleContext);
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Route the given event to one of our targets
 52  
      */
 53  
     @Override
 54  
     public MuleEvent route(MuleEvent event) throws MessagingException
 55  
     {
 56  0
         MuleEvent returnEvent = null;
 57  
 
 58  0
         boolean failed = true;
 59  0
         for (MessageProcessor mp : routes)
 60  
         {
 61  
             try
 62  
             {
 63  0
                 MuleMessage clonedMessage = cloneMessage(event.getMessage());
 64  0
                 MuleEvent toProcess = createEventToRoute(event, clonedMessage, mp);
 65  0
                 returnEvent = mp.process(toProcess);
 66  
 
 67  0
                 if (returnEvent == null)
 68  
                 {
 69  0
                     failed = false;
 70  
                 }
 71  
                 else
 72  
                 {
 73  0
                     MuleMessage msg = returnEvent.getMessage();
 74  0
                     failed = msg == null || failureExpressionFilter.accept(msg);
 75  
                 }
 76  
             }
 77  0
             catch (Exception ex)
 78  
             {
 79  0
                 failed = true;
 80  0
             }
 81  0
             if (!failed)
 82  
             {
 83  0
                 break;
 84  
             }
 85  
         }
 86  
 
 87  0
         if (failed)
 88  
         {
 89  0
             throw new CouldNotRouteOutboundMessageException(event, this);
 90  
         }
 91  
 
 92  0
         return returnEvent;
 93  
     }
 94  
 
 95  
     public boolean isMatch(MuleMessage message) throws MuleException
 96  
     {
 97  0
         return true;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Specifies an expression that when evaluated as determines if the processing of
 102  
      * one a route was a failure or not.
 103  
      * 
 104  
      * @param failureExpression
 105  
      * @see ExpressionFilter
 106  
      */
 107  
     public void setFailureExpression(String failureExpression)
 108  
     {
 109  0
         this.failureExpression = failureExpression;
 110  0
     }
 111  
 }