Coverage Report - org.mule.routing.outbound.FilteringListMessageSplitter
 
Classes in this File Line Coverage Branch Coverage Complexity
FilteringListMessageSplitter
92%
33/36
75%
12/16
4
 
 1  
 /*
 2  
  * $Id: FilteringListMessageSplitter.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.impl.MuleMessage;
 14  
 import org.mule.umo.UMOMessage;
 15  
 import org.mule.umo.endpoint.UMOEndpoint;
 16  
 
 17  
 import java.util.HashMap;
 18  
 import java.util.Iterator;
 19  
 import java.util.LinkedList;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * <code>FilteringListMessageSplitter</code> accepts a List as a message payload
 25  
  * then routes list elements as messages over an endpoint where the endpoint's filter
 26  
  * accepts the payload.
 27  
  */
 28  4
 public class FilteringListMessageSplitter extends AbstractMessageSplitter
 29  
 {
 30  4
     private final ThreadLocal payloadContext = new ThreadLocal();
 31  4
     private final ThreadLocal propertiesContext = new ThreadLocal();
 32  
 
 33  
     /**
 34  
      * Template method can be used to split the message up before the getMessagePart
 35  
      * method is called .
 36  
      * 
 37  
      * @param message the message being routed
 38  
      */
 39  
     // @Override
 40  
     protected void initialise(UMOMessage message)
 41  
     {
 42  6
         super.initialise(message);
 43  
 
 44  6
         if (message.getPayload() instanceof List)
 45  
         {
 46  
             // get a copy of the list
 47  6
             List payload = new LinkedList((List) message.getPayload());
 48  6
             payloadContext.set(payload);
 49  
 
 50  6
             if (enableCorrelation != ENABLE_CORRELATION_NEVER)
 51  
             {
 52  
                 // always set correlation group size, even if correlation id
 53  
                 // has already been set (usually you don't have group size yet
 54  
                 // by this point.
 55  6
                 final int groupSize = payload.size();
 56  6
                 message.setCorrelationGroupSize(groupSize);
 57  6
                 if (logger.isDebugEnabled())
 58  
                 {
 59  0
                     logger.debug("java.util.List payload detected, setting correlation group size to "
 60  
                                     + groupSize);
 61  
                 }
 62  
             }
 63  6
         }
 64  
         else
 65  
         {
 66  0
             throw new IllegalArgumentException("The payload for this router must be of type java.util.List");
 67  
         }
 68  
 
 69  
         // Cache the properties here because for some message types getting the
 70  
         // properties can be expensive
 71  6
         Map props = new HashMap();
 72  6
         for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
 73  
         {
 74  6
             String propertyKey = (String) iterator.next();
 75  6
             props.put(propertyKey, message.getProperty(propertyKey));
 76  6
         }
 77  
 
 78  6
         propertiesContext.set(props);
 79  6
     }
 80  
 
 81  
     // @Override
 82  
     protected void cleanup()
 83  
     {
 84  6
         payloadContext.set(null);
 85  6
         propertiesContext.set(null);
 86  6
         super.cleanup();
 87  6
     }
 88  
 
 89  
     /**
 90  
      * @inheritDocs
 91  
      */
 92  
     protected UMOMessage getMessagePart(UMOMessage message, UMOEndpoint endpoint)
 93  
     {
 94  38
         List payloads = (List) payloadContext.get();
 95  
 
 96  38
         for (Iterator i = payloads.iterator(); i.hasNext();)
 97  
         {
 98  36
             Object payload = i.next();
 99  36
             UMOMessage result = new MuleMessage(payload, (Map) propertiesContext.get());
 100  
             // If there is no filter assume that the endpoint can accept the
 101  
             // message. Endpoints will be processed in order to only the last
 102  
             // (if any) of the the endpoints may not have a filter
 103  36
             if (endpoint.getFilter() == null || endpoint.getFilter().accept(result))
 104  
             {
 105  24
                 if (logger.isDebugEnabled())
 106  
                 {
 107  0
                     logger.debug("Endpoint filter matched. Routing message over: "
 108  
                                     + endpoint.getEndpointURI().toString());
 109  
                 }
 110  24
                 i.remove();
 111  24
                 return result;
 112  
             }
 113  12
         }
 114  
 
 115  14
         return null;
 116  
     }
 117  
 }