View Javadoc

1   /*
2    * $Id: FilteringListMessageSplitter.java 10961 2008-02-22 19:01:02Z 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.outbound;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.OutboundEndpoint;
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  public class FilteringListMessageSplitter extends AbstractMessageSplitter
29  {
30      private final ThreadLocal payloadContext = new ThreadLocal();
31      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(MuleMessage message)
41      {
42          if (message.getPayload() instanceof List)
43          {
44              // get a copy of the list
45              List payload = new LinkedList((List) message.getPayload());
46              payloadContext.set(payload);
47  
48              if (enableCorrelation != ENABLE_CORRELATION_NEVER)
49              {
50                  // always set correlation group size, even if correlation id
51                  // has already been set (usually you don't have group size yet
52                  // by this point.
53                  final int groupSize = payload.size();
54                  message.setCorrelationGroupSize(groupSize);
55                  if (logger.isDebugEnabled())
56                  {
57                      logger.debug("java.util.List payload detected, setting correlation group size to "
58                                      + groupSize);
59                  }
60              }
61          }
62          else
63          {
64              throw new IllegalArgumentException("The payload for this router must be of type java.util.List");
65          }
66  
67          // Cache the properties here because for some message types getting the
68          // properties can be expensive
69          Map props = new HashMap();
70          for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
71          {
72              String propertyKey = (String) iterator.next();
73              props.put(propertyKey, message.getProperty(propertyKey));
74          }
75  
76          propertiesContext.set(props);
77      }
78  
79      // @Override
80      protected void cleanup()
81      {
82          payloadContext.set(null);
83          propertiesContext.set(null);
84      }
85  
86      /**
87       * @inheritDocs
88       */
89      protected MuleMessage getMessagePart(MuleMessage message, OutboundEndpoint endpoint)
90      {
91          List payloads = (List) payloadContext.get();
92  
93          for (Iterator i = payloads.iterator(); i.hasNext();)
94          {
95              Object payload = i.next();
96              MuleMessage result = new DefaultMuleMessage(payload, (Map) propertiesContext.get());
97              // If there is no filter assume that the endpoint can accept the
98              // message. Endpoints will be processed in order to only the last
99              // (if any) of the the endpoints may not have a filter
100             if (endpoint.getFilter() == null || endpoint.getFilter().accept(result))
101             {
102                 if (logger.isDebugEnabled())
103                 {
104                     logger.debug("Endpoint filter matched. Routing message over: "
105                                     + endpoint.getEndpointURI().toString());
106                 }
107                 i.remove();
108                 return result;
109             }
110         }
111 
112         return null;
113     }
114 }