KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > outbound > FilteringListMessageSplitter


1 /*
2  * $Id: FilteringListMessageSplitter.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
14
15 import org.mule.impl.MuleMessage;
16 import org.mule.umo.UMOMessage;
17 import org.mule.umo.endpoint.UMOEndpoint;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * <code>FilteringListMessageSplitter</code> Accepts a List as a message payload
26  * then routes list elements as messages over an endpoint where the endpoint's filter
27  * accepts the payload.
28  *
29  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
30  * @version $Revision: 3798 $
31  */

32 public class FilteringListMessageSplitter extends AbstractMessageSplitter
33 {
34     private static ThreadLocal JavaDoc payloads = new ThreadLocal JavaDoc();
35     private static ThreadLocal JavaDoc properties = new ThreadLocal JavaDoc();
36
37     /**
38      * Template method can be used to split the message up before the getMessagePart
39      * method is called .
40      *
41      * @param message the message being routed
42      */

43     protected void initialise(UMOMessage message)
44     {
45         if (message.getPayload() instanceof List)
46         {
47             // get a synchronised cloned list
48
CopyOnWriteArrayList payload = new CopyOnWriteArrayList((List)message.getPayload());
49             payloads.set(payload);
50             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
final int groupSize = payload.size();
56                 message.setCorrelationGroupSize(groupSize);
57                 if (logger.isDebugEnabled())
58                 {
59                     logger.debug("java.util.List payload detected, setting correlation group size to "
60                                  + groupSize);
61                 }
62             }
63         }
64         else
65         {
66             throw new IllegalArgumentException JavaDoc("The payload for this router must be of type java.util.List");
67         }
68         // Cache the properties here because for some message types getting the
69
// properties can be expensive
70
Map JavaDoc props = new HashMap JavaDoc();
71         for (Iterator JavaDoc iterator = message.getPropertyNames().iterator(); iterator.hasNext();)
72         {
73             String JavaDoc propertyKey = (String JavaDoc)iterator.next();
74             props.put(propertyKey, message.getProperty(propertyKey));
75         }
76         properties.set(props);
77     }
78
79     /**
80      * @inheritDocs
81      */

82     protected UMOMessage getMessagePart(UMOMessage message, UMOEndpoint endpoint)
83     {
84         CopyOnWriteArrayList payload = (CopyOnWriteArrayList)payloads.get();
85         for (int i = 0; i < payload.size(); i++)
86         {
87             Object JavaDoc object = payload.get(i);
88             UMOMessage result = new MuleMessage(object, (Map JavaDoc)properties.get());
89             // If there is no filter assume that the endpoint can accept the
90
// message. Endpoints will be processed in order to only the last
91
// (if any) of the the endpoints may not have a filter
92
if (endpoint.getFilter() == null || endpoint.getFilter().accept(result))
93             {
94                 if (logger.isDebugEnabled())
95                 {
96                     logger.debug("Endpoint filter matched. Routing message over: "
97                                  + endpoint.getEndpointURI().toString());
98                 }
99                 payload.remove(i);
100                 return result;
101             }
102         }
103         return null;
104     }
105 }
106
Popular Tags