Coverage Report - org.mule.routing.inbound.DefaultInboundRouterCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultInboundRouterCollection
69%
62/90
52%
31/60
4.364
 
 1  
 /*
 2  
  * $Id: DefaultInboundRouterCollection.java 12111 2008-06-19 14:38:08Z 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.inbound;
 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.config.MuleProperties;
 18  
 import org.mule.api.endpoint.ImmutableEndpoint;
 19  
 import org.mule.api.endpoint.InboundEndpoint;
 20  
 import org.mule.api.endpoint.InvalidEndpointTypeException;
 21  
 import org.mule.api.routing.InboundRouter;
 22  
 import org.mule.api.routing.InboundRouterCollection;
 23  
 import org.mule.api.routing.RoutingException;
 24  
 import org.mule.config.i18n.CoreMessages;
 25  
 import org.mule.management.stats.RouterStatistics;
 26  
 import org.mule.routing.AbstractRouterCollection;
 27  
 import org.mule.util.StringMessageUtils;
 28  
 import org.mule.util.StringUtils;
 29  
 
 30  
 import java.util.Iterator;
 31  
 import java.util.List;
 32  
 
 33  
 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
 34  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 35  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap;
 36  
 
 37  
 /**
 38  
  * <code>DefaultInboundRouterCollection</code> is a collection of routers that will be
 39  
  * invoked when an event is received. It is responsible for managing a collection of
 40  
  * routers and also executing the routing logic. Each router must match against the
 41  
  * current event for the event to be routed.
 42  
  */
 43  
 
 44  
 public class DefaultInboundRouterCollection extends AbstractRouterCollection implements InboundRouterCollection
 45  
 {
 46  428
     private final List endpoints = new CopyOnWriteArrayList();
 47  
 
 48  
     public DefaultInboundRouterCollection()
 49  
     {
 50  428
         super(RouterStatistics.TYPE_INBOUND);
 51  
         //default for inbound routing
 52  428
         setMatchAll(true);
 53  428
     }
 54  
 
 55  
     public MuleMessage route(MuleEvent event) throws MessagingException
 56  
     {
 57  
         // If the endpoint has a logical name, use it, otherwise use the URI.
 58  18
         String inboundEndpoint = 
 59  
             // Endpoint identifier (deprecated)
 60  
             event.getEndpoint().getEndpointURI().getEndpointName();
 61  
 
 62  18
         if (StringUtils.isBlank(inboundEndpoint))
 63  
         {
 64  
             // Global endpoint
 65  18
             inboundEndpoint = event.getEndpoint().getName();
 66  
         }
 67  18
         if (StringUtils.isBlank(inboundEndpoint))
 68  
         {
 69  
             // URI
 70  0
             inboundEndpoint = event.getEndpoint().getEndpointURI().getUri().toString();
 71  
         }
 72  18
         event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint);
 73  
 
 74  18
         if (endpoints.size() > 0 && routers.size() == 0)
 75  
         {
 76  0
             addRouter(new InboundPassThroughRouter());
 77  
         }
 78  
 
 79  18
         String componentName = event.getSession().getService().getName();
 80  
 
 81  18
         ConcurrentMap eventsToRoute = new ConcurrentHashMap(2);
 82  18
         boolean noRoute = true;
 83  18
         boolean match = false;
 84  
         InboundRouter umoInboundRouter;
 85  18
         MuleEvent lastEvent= null;
 86  
 
 87  18
         for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
 88  
         {
 89  18
             umoInboundRouter = (InboundRouter) iterator.next();
 90  
 
 91  18
             if (umoInboundRouter.isMatch(event))
 92  
             {
 93  12
                 match = true;
 94  12
                 MuleEvent[] events = umoInboundRouter.process(event);
 95  12
                 if(events!=null)
 96  
                 {
 97  24
                     for (int i = 0; i < events.length; i++)
 98  
                     {
 99  12
                         lastEvent = events[i];
 100  
                         //Only add the event if it's a new event
 101  12
                         eventsToRoute.putIfAbsent(lastEvent.getId(), lastEvent);
 102  
                     }
 103  
                 }
 104  
 
 105  12
                 noRoute = (events == null);
 106  12
                 if (!isMatchAll())
 107  
                 {
 108  0
                     break;
 109  
                 }
 110  12
             }
 111  
         }
 112  
 
 113  
         // If the stopFurtherProcessing flag has been set
 114  
         // do not route events to the service.
 115  
         // This is the case when using a ForwardingConsumer
 116  
         // inbound router for example.
 117  18
         if (!event.isStopFurtherProcessing())
 118  
         {
 119  18
             if (noRoute)
 120  
             {
 121  
                 // Update stats
 122  6
                 if (getStatistics().isEnabled())
 123  
                 {
 124  0
                     getStatistics().incrementNoRoutedMessage();
 125  
                 }
 126  6
                 if (!match)
 127  
                 {
 128  6
                     if (getCatchAllStrategy() != null)
 129  
                     {
 130  6
                         if (logger.isDebugEnabled())
 131  
                         {
 132  0
                             logger.debug("Message did not match any routers on: " + componentName
 133  
                                          + " - invoking catch all strategy");
 134  
                         }
 135  6
                         if (getStatistics().isEnabled())
 136  
                         {
 137  0
                             getStatistics().incrementCaughtMessage();
 138  
                         }
 139  6
                         return getCatchAllStrategy().catchMessage(event.getMessage(), event.getSession(),
 140  
                             event.isSynchronous());
 141  
 
 142  
                     }
 143  
                     else
 144  
                     {
 145  0
                         logger.warn("Message did not match any routers on: "
 146  
                                     + componentName
 147  
                                     + " and there is no catch all strategy configured on this router.  Disposing message: " + event);
 148  0
                         if (logger.isDebugEnabled())
 149  
                         {
 150  
                             try
 151  
                             {
 152  0
                                 logger.warn("Message fragment is: "
 153  
                                             + StringMessageUtils.truncate(event.getMessageAsString(), 100,
 154  
                                                 true));
 155  
                             }
 156  0
                             catch (MuleException e)
 157  
                             {
 158  
                                 // ignore
 159  0
                             }
 160  
                         }
 161  
                     }
 162  
                 }
 163  
             }
 164  
             else
 165  
             {
 166  
                 try
 167  
                 {
 168  12
                     MuleMessage messageResult = null;
 169  12
                     for (Iterator iterator = eventsToRoute.values().iterator(); iterator.hasNext();)
 170  
                     {
 171  12
                         MuleEvent eventToRoute = (MuleEvent) iterator.next();
 172  
 
 173  
                         // Set the originating endpoint so we'll know where this event came from further down the pipeline.
 174  12
                         if (event.getMessage().getProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY) == null)
 175  
                         {
 176  0
                             event.getMessage().setProperty(MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, inboundEndpoint);
 177  
                         }
 178  
 
 179  12
                         if (event.isSynchronous())
 180  
                         {
 181  6
                             messageResult = send(eventToRoute);
 182  
                         }
 183  
                         else
 184  
                         {
 185  6
                             dispatch(eventToRoute);
 186  
                         }
 187  
                         // Update stats
 188  12
                         if (getStatistics().isEnabled())
 189  
                         {
 190  0
                             getStatistics().incrementRoutedMessage(eventToRoute.getEndpoint());
 191  
                         }
 192  12
                     }
 193  12
                     return messageResult;
 194  
                 }
 195  0
                 catch (MuleException e)
 196  
                 {
 197  0
                     throw new RoutingException(event.getMessage(), event.getEndpoint(), e);
 198  
                 }
 199  
             }
 200  
         }
 201  0
         if(event.isSynchronous())
 202  
         {
 203  
             //This is required if the Router short-circuits the service and diverts processing elsewhere
 204  
             //The only example of this right now is the FowardingConsumer (<forwarding-router/>)
 205  0
             return (lastEvent == null ? null : lastEvent.getMessage());
 206  
         }
 207  
         else
 208  
         {
 209  0
             return null;
 210  
         }
 211  
 
 212  
     }
 213  
 
 214  
     public void dispatch(MuleEvent event) throws MuleException
 215  
     {
 216  6
         event.getSession().dispatchEvent(event);
 217  6
     }
 218  
 
 219  
     public MuleMessage send(MuleEvent event) throws MuleException
 220  
     {
 221  6
         return event.getSession().sendEvent(event);
 222  
     }
 223  
 
 224  
     public void addRouter(InboundRouter router)
 225  
     {
 226  10
         routers.add(router);
 227  10
     }
 228  
 
 229  
     public InboundRouter removeRouter(InboundRouter router)
 230  
     {
 231  0
         if (routers.remove(router))
 232  
         {
 233  0
             return router;
 234  
         }
 235  
         else
 236  
         {
 237  0
             return null;
 238  
         }
 239  
     }
 240  
 
 241  
     public void addEndpoint(InboundEndpoint endpoint)
 242  
     {
 243  14
         endpoints.add(endpoint);
 244  14
     }
 245  
 
 246  
     public boolean removeEndpoint(InboundEndpoint endpoint)
 247  
     {
 248  0
         return endpoints.remove(endpoint);
 249  
     }
 250  
 
 251  
     public List getEndpoints()
 252  
     {
 253  94
         return endpoints;
 254  
     }
 255  
 
 256  
     public void setEndpoints(List endpoints)
 257  
     {
 258  6
         if (endpoints != null)
 259  
         {
 260  6
             this.endpoints.clear();
 261  
             // Ensure all endpoints are inbound endpoints
 262  
             // This will go when we start dropping support for 1.4 and start using 1.5
 263  6
             for (Iterator it = endpoints.iterator(); it.hasNext();)
 264  
             {
 265  12
                 ImmutableEndpoint endpoint = (ImmutableEndpoint) it.next();
 266  12
                 if (!(endpoint instanceof InboundEndpoint))
 267  
                 {
 268  4
                     throw new InvalidEndpointTypeException(CoreMessages.inboundRouterMustUseInboundEndpoints(this,
 269  
                         endpoint));
 270  
                 }
 271  8
             }
 272  2
             this.endpoints.addAll(endpoints);
 273  
         }
 274  
         else
 275  
         {
 276  0
             throw new IllegalArgumentException("List of endpoints = null");
 277  
         }
 278  2
     }
 279  
 
 280  
     /**
 281  
      * @param name the Endpoint identifier
 282  
      * @return the Endpoint or null if the endpointUri is not registered
 283  
      * @see org.mule.api.routing.InboundRouterCollection
 284  
      */
 285  
     public InboundEndpoint getEndpoint(String name)
 286  
     {
 287  
         InboundEndpoint endpointDescriptor;
 288  0
         for (Iterator iterator = endpoints.iterator(); iterator.hasNext();)
 289  
         {
 290  0
             endpointDescriptor = (InboundEndpoint) iterator.next();
 291  0
             if (endpointDescriptor.getName().equals(name))
 292  
             {
 293  0
                 return endpointDescriptor;
 294  
             }
 295  
         }
 296  0
         return null;
 297  
     }
 298  
 }