Coverage Report - org.mule.routing.outbound.RoundRobinXmlSplitter
 
Classes in this File Line Coverage Branch Coverage Complexity
RoundRobinXmlSplitter
63%
36/57
44%
15/34
5.2
 
 1  
 /*
 2  
  * $Id: RoundRobinXmlSplitter.java 9326 2007-10-24 12:48:57Z holger $
 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.config.MuleProperties;
 14  
 import org.mule.impl.MuleMessage;
 15  
 import org.mule.umo.UMOException;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.UMOSession;
 18  
 import org.mule.umo.endpoint.UMOEndpoint;
 19  
 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
 20  
 import org.mule.umo.routing.RoutingException;
 21  
 
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 import org.dom4j.Document;
 28  
 
 29  
 /**
 30  
  * This router will split the Xml message into parts based on the xpath expression
 31  
  * and route each new event to the endpoints on the router, one after the other.
 32  
  */
 33  16
 public class RoundRobinXmlSplitter extends FilteringXmlMessageSplitter
 34  
 {
 35  
     // We have to do some additional checks if we're going to allow filters on the
 36  
     // round robin endpoints, so for performance lets turn it off by default
 37  16
     protected volatile boolean enableEndpointFiltering = false;
 38  
 
 39  
     // @Override
 40  
     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
 41  
         throws RoutingException
 42  
     {
 43  
         try
 44  
         {
 45  20
             String correlationId = (String)propertyExtractor.getProperty(
 46  
                 MuleProperties.MULE_CORRELATION_ID_PROPERTY, message);
 47  
             
 48  20
             this.initialise(message);
 49  
 
 50  
             UMOEndpoint endpoint;
 51  16
             UMOMessage result = null;
 52  
             Document part;
 53  
 
 54  16
             List parts = (List)nodesContext.get();
 55  
 
 56  16
             if (parts == null)
 57  
             {
 58  4
                 logger.error("There are no parts for current message. No events were routed: " + message);
 59  4
                 return null;
 60  
             }
 61  
 
 62  12
             int correlationSequence = 1;
 63  12
             int epCounter = 0;
 64  
 
 65  60
             for (Iterator iterator = parts.iterator(); iterator.hasNext(); epCounter++)
 66  
             {
 67  48
                 part = (Document)iterator.next();
 68  48
                 if (epCounter == endpoints.size())
 69  
                 {
 70  12
                     epCounter = 0;
 71  
                 }
 72  
                 // Create the message
 73  48
                 Map theProperties = (Map)propertiesContext.get();
 74  48
                 message = new MuleMessage(part, new HashMap(theProperties));
 75  
 
 76  48
                 if (enableEndpointFiltering)
 77  
                 {
 78  0
                     endpoint = getEndpointForMessage(message);
 79  
                 }
 80  
                 else
 81  
                 {
 82  48
                     endpoint = (UMOEndpoint)getEndpoints().get(epCounter);
 83  
                 }
 84  
 
 85  48
                 if (endpoint == null)
 86  
                 {
 87  0
                     logger.error("There was no matching endpoint for message part: " + part.asXML());
 88  
                 }
 89  
                 else
 90  
                 {
 91  
                     try
 92  
                     {
 93  48
                         if (enableCorrelation != ENABLE_CORRELATION_NEVER)
 94  
                         {
 95  48
                             boolean correlationSet = message.getCorrelationId() != null;
 96  48
                             if (!correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
 97  
                             {
 98  48
                                 message.setCorrelationId(correlationId);
 99  
                             }
 100  
 
 101  
                             // take correlation group size from the message
 102  
                             // properties, set by concrete message splitter
 103  
                             // implementations
 104  48
                             final int groupSize = message.getCorrelationGroupSize();
 105  48
                             message.setCorrelationGroupSize(groupSize);
 106  48
                             message.setCorrelationSequence(correlationSequence++);
 107  
                         }
 108  48
                         if (synchronous)
 109  
                         {
 110  24
                             result = send(session, message, endpoint);
 111  
                         }
 112  
                         else
 113  
                         {
 114  24
                             dispatch(session, message, endpoint);
 115  
                         }
 116  
                     }
 117  0
                     catch (UMOException e)
 118  
                     {
 119  0
                         throw new CouldNotRouteOutboundMessageException(message, endpoint, e);
 120  48
                     }
 121  
                 }
 122  
             }
 123  12
             return result;
 124  
         }
 125  
         finally
 126  
         {
 127  16
             this.cleanup();
 128  
         }
 129  
     }
 130  
 
 131  
     /**
 132  
      * Retrieves a specific message part for the given endpoint. the message will
 133  
      * then be routed via the provider.
 134  
      * 
 135  
      * @param message the current message being processed
 136  
      * @return the message part to dispatch
 137  
      */
 138  
     protected UMOEndpoint getEndpointForMessage(UMOMessage message)
 139  
     {
 140  0
         for (int i = 0; i < endpoints.size(); i++)
 141  
         {
 142  0
             UMOEndpoint endpoint = (UMOEndpoint)endpoints.get(i);
 143  
 
 144  
             try
 145  
             {
 146  0
                 if (endpoint.getFilter() == null || endpoint.getFilter().accept(message))
 147  
                 {
 148  0
                     if (logger.isDebugEnabled())
 149  
                     {
 150  0
                         logger.debug("Endpoint filter matched for node " + i + ". Routing message over: "
 151  
                                      + endpoint.getEndpointURI().toString());
 152  
                     }
 153  0
                     return endpoint;
 154  
                 }
 155  
                 else
 156  
                 {
 157  0
                     if (logger.isDebugEnabled())
 158  
                     {
 159  0
                         logger.debug("Endpoint filter did not match");
 160  
                     }
 161  
                 }
 162  
             }
 163  0
             catch (Exception e)
 164  
             {
 165  0
                 logger.error("Unable to create message for node at position " + i, e);
 166  0
                 return null;
 167  0
             }
 168  
         }
 169  
 
 170  0
         return null;
 171  
     }
 172  
 
 173  
     public void addEndpoint(UMOEndpoint endpoint)
 174  
     {
 175  36
         if (endpoint.getFilter() != null && !enableEndpointFiltering)
 176  
         {
 177  0
             throw new IllegalStateException(
 178  
                 "Endpoints on the RoundRobin splitter router cannot have filters associated with them");
 179  
         }
 180  36
         super.addEndpoint(endpoint);
 181  36
     }
 182  
 
 183  
     public boolean isEnableEndpointFiltering()
 184  
     {
 185  0
         return enableEndpointFiltering;
 186  
     }
 187  
 
 188  
     public void setEnableEndpointFiltering(boolean enableEndpointFiltering)
 189  
     {
 190  0
         this.enableEndpointFiltering = enableEndpointFiltering;
 191  0
     }
 192  
 }