KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractMessageSplitter.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 org.mule.config.MuleProperties;
14 import org.mule.umo.UMOException;
15 import org.mule.umo.UMOMessage;
16 import org.mule.umo.UMOSession;
17 import org.mule.umo.endpoint.UMOEndpoint;
18 import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
19 import org.mule.umo.routing.RoutingException;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * <code>AbstractMessageSplitter</code> is an outbound Message Splitter used to
26  * split the contents of a received message into sup parts that can be processed by
27  * other components. Each Part is fired as a separate event to each endpoint on the
28  * router. The endpoints can have filters on them to receive only certain message
29  * parts
30  *
31  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32  * @version $Revision: 3798 $
33  */

34
35 public abstract class AbstractMessageSplitter extends FilteringOutboundRouter
36 {
37     // Determines if the same endpoint will be matched multiple times until a
38
// match is not found. This should be set by overriding classes.
39
protected boolean multimatch = true;
40
41     // flag which, if true, makes the splitter honour settings such as remoteSync and
42
// synchronous on the endpoint
43
protected boolean honorSynchronicity = false;
44
45     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
46         throws RoutingException
47     {
48         String JavaDoc correlationId = (String JavaDoc)propertyExtractor.getProperty(
49             MuleProperties.MULE_CORRELATION_ID_PROPERTY, message);
50         initialise(message);
51
52         UMOEndpoint endpoint;
53         UMOMessage result = null;
54         List JavaDoc list = getEndpoints();
55         int correlationSequence = 1;
56         for (Iterator JavaDoc iterator = list.iterator(); iterator.hasNext();)
57         {
58             endpoint = (UMOEndpoint)iterator.next();
59             message = getMessagePart(message, endpoint);
60             if (message == null)
61             {
62                 // Log a warning if there are no messages for a given endpoint
63
logger.warn("Message part is null for endpoint: " + endpoint.getEndpointURI().toString());
64             }
65             // We'll keep looping to get all messages for the current endpoint
66
// before moving to the next endpoint
67
// This can be turned off by setting the multimatch flag to false
68
while (message != null)
69             {
70                 if (honorSynchronicity)
71                 {
72                     synchronous = endpoint.isSynchronous();
73                 }
74                 try
75                 {
76                     if (enableCorrelation != ENABLE_CORRELATION_NEVER)
77                     {
78                         boolean correlationSet = message.getCorrelationId() != null;
79                         if (!correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
80                         {
81                             message.setCorrelationId(correlationId);
82                         }
83
84                         // take correlation group size from the message
85
// properties, set by concrete message splitter
86
// implementations
87
final int groupSize = message.getCorrelationGroupSize();
88                         message.setCorrelationGroupSize(groupSize);
89                         message.setCorrelationSequence(correlationSequence++);
90                     }
91                     if (honorSynchronicity)
92                     {
93                         message.setBooleanProperty(MuleProperties.MULE_REMOTE_SYNC_PROPERTY,
94                             endpoint.isRemoteSync());
95                     }
96                     if (synchronous)
97                     {
98                         result = send(session, message, endpoint);
99                     }
100                     else
101                     {
102                         dispatch(session, message, endpoint);
103                     }
104                 }
105                 catch (UMOException e)
106                 {
107                     throw new CouldNotRouteOutboundMessageException(message, endpoint, e);
108                 }
109                 if (!multimatch)
110                 {
111                     break;
112                 }
113                 message = getMessagePart(message, endpoint);
114             }
115         }
116         return result;
117     }
118
119     /**
120      * Template method that can be used to split the message up before the
121      * getMessagePart method is called .
122      *
123      * @param message the message being routed
124      */

125     protected void initialise(UMOMessage message)
126     {
127         // nothing to do
128
}
129
130     public boolean isHonorSynchronicity()
131     {
132         return honorSynchronicity;
133     }
134
135     /**
136      * Sets the flag indicating whether the splitter honurs endpoint settings
137      *
138      * @param honorSynchronicity flag setting
139      */

140     public void setHonorSynchronicity(boolean honorSynchronicity)
141     {
142         this.honorSynchronicity = honorSynchronicity;
143     }
144
145     /**
146      * Retrieves a specific message part for the given endpoint. the message will
147      * then be routed via the provider. <p/> <strong>NOTE:</strong>Implementations
148      * must provide proper synchronization for shared state (payload, properties,
149      * etc.)
150      *
151      * @param message the current message being processed
152      * @param endpoint the endpoint that will be used to route the resulting message
153      * part
154      * @return the message part to dispatch
155      */

156     protected abstract UMOMessage getMessagePart(UMOMessage message, UMOEndpoint endpoint);
157 }
158
Popular Tags