Coverage Report - org.mule.routing.outbound.DefaultOutboundRouterCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultOutboundRouterCollection
67%
22/33
55%
12/22
3.8
DefaultOutboundRouterCollection$1
100%
2/2
N/A
3.8
 
 1  
 /*
 2  
  * $Id: DefaultOutboundRouterCollection.java 12181 2008-06-26 20:05:55Z dirk.olmes $
 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.outbound;
 12  
 
 13  
 import org.mule.api.MessagingException;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.MuleSession;
 16  
 import org.mule.api.routing.OutboundRouter;
 17  
 import org.mule.api.routing.OutboundRouterCollection;
 18  
 import org.mule.api.routing.RoutingException;
 19  
 import org.mule.api.transaction.TransactionCallback;
 20  
 import org.mule.management.stats.RouterStatistics;
 21  
 import org.mule.routing.AbstractRouterCollection;
 22  
 import org.mule.transaction.TransactionTemplate;
 23  
 
 24  
 import java.util.Iterator;
 25  
 
 26  
 /**
 27  
  * <code>DefaultOutboundRouterCollection</code> is a container of routers. An
 28  
  * DefaultOutboundRouterCollection must have atleast one router. By default the first matching
 29  
  * router is used to route an event though it is possible to match on all routers
 30  
  * meaning that the message will get sent over all matching routers.
 31  
  */
 32  
 
 33  
 public class DefaultOutboundRouterCollection extends AbstractRouterCollection implements OutboundRouterCollection
 34  
 {
 35  
 
 36  
     public DefaultOutboundRouterCollection()
 37  
     {
 38  426
         super(RouterStatistics.TYPE_OUTBOUND);
 39  426
     }
 40  
 
 41  
     public MuleMessage route(final MuleMessage message, final MuleSession session, final boolean synchronous)
 42  
             throws MessagingException
 43  
     {
 44  
         MuleMessage result;
 45  18
         boolean matchfound = false;
 46  
 
 47  18
         for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 48  
         {
 49  32
             OutboundRouter umoOutboundRouter = (OutboundRouter) iterator.next();
 50  32
             if (umoOutboundRouter.isMatch(message))
 51  
             {
 52  16
                 matchfound = true;
 53  
                 // Manage outbound only transactions here
 54  16
                 final OutboundRouter router = umoOutboundRouter;
 55  
 
 56  
                 
 57  16
                 TransactionTemplate tt = new TransactionTemplate(umoOutboundRouter.getTransactionConfig(),
 58  
                     session.getService().getExceptionListener(), muleContext);
 59  
                 
 60  16
                 TransactionCallback cb = new TransactionCallback()
 61  
                 {
 62  16
                     public Object doInTransaction() throws Exception
 63  
                     {
 64  16
                         return router.route(message, session, synchronous);
 65  
                     }
 66  
                 };
 67  
                 try
 68  
                 {
 69  16
                     result = (MuleMessage) tt.execute(cb);
 70  
                 }
 71  0
                 catch (Exception e)
 72  
                 {
 73  0
                     throw new RoutingException(message, null, e);
 74  16
                 }
 75  
 
 76  16
                 if (!isMatchAll())
 77  
                 {
 78  12
                     return result;
 79  
                 }
 80  
             }
 81  20
         }
 82  
 
 83  6
         if (!matchfound && getCatchAllStrategy() != null)
 84  
         {
 85  4
             if (logger.isDebugEnabled())
 86  
             {
 87  0
                 logger.debug("Message did not match any routers on: "
 88  
                         + session.getService().getName()
 89  
                         + " invoking catch all strategy");
 90  
             }
 91  4
             return catchAll(message, session, synchronous);
 92  
         }
 93  2
         else if (!matchfound)
 94  
         {
 95  0
             logger.warn("Message did not match any routers on: "
 96  
                     + session.getService().getName()
 97  
                     + " and there is no catch all strategy configured on this router.  Disposing message " + message);
 98  
         }
 99  2
         return message;
 100  
     }
 101  
 
 102  
     protected MuleMessage catchAll(MuleMessage message, MuleSession session, boolean synchronous)
 103  
             throws RoutingException
 104  
     {
 105  4
         if (getStatistics().isEnabled())
 106  
         {
 107  0
             getStatistics().incrementCaughtMessage();
 108  
         }
 109  
 
 110  4
         return getCatchAllStrategy().catchMessage(message, session, synchronous);
 111  
     }
 112  
 
 113  
     public boolean hasEndpoints()
 114  
     {
 115  0
         for (Iterator iterator = routers.iterator(); iterator.hasNext();)
 116  
         {
 117  0
             OutboundRouter router = (OutboundRouter) iterator.next();
 118  0
             if (router.getEndpoints().size() > 0 || router.isDynamicEndpoints())
 119  
             {
 120  0
                 return true;
 121  
             }
 122  0
         }
 123  0
         return false;
 124  
     }
 125  
 
 126  
 }