KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MulticastingRouter.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.i18n.Message;
14 import org.mule.config.i18n.Messages;
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.RoutePathNotFoundException;
21 import org.mule.umo.routing.RoutingException;
22
23 /**
24  * <code>MulticastingRouter</code> will broadcast the current message to every
25  * endpoint registed with the router.
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3798 $
29  */

30
31 public class MulticastingRouter extends FilteringOutboundRouter
32 {
33
34     public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
35         throws RoutingException
36     {
37         UMOMessage result = null;
38         if (endpoints == null || endpoints.size() == 0)
39         {
40             throw new RoutePathNotFoundException(new Message(Messages.NO_ENDPOINTS_FOR_ROUTER), message, null);
41         }
42         if (enableCorrelation != ENABLE_CORRELATION_NEVER)
43         {
44             boolean correlationSet = message.getCorrelationId() != null;
45             if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
46             {
47                 logger.debug("CorrelationId is already set, not setting Correlation group size");
48             }
49             else
50             {
51                 // the correlationId will be set by the AbstractOutboundRouter
52
message.setCorrelationGroupSize(endpoints.size());
53             }
54         }
55
56         try
57         {
58             UMOEndpoint endpoint;
59             synchronized (endpoints)
60             {
61                 for (int i = 0; i < endpoints.size(); i++)
62                 {
63                     endpoint = (UMOEndpoint)endpoints.get(i);
64                     if (synchronous)
65                     {
66                         // Were we have multiple outbound endpoints
67
if (result == null)
68                         {
69                             result = send(session, message, endpoint);
70                         }
71                         else
72                         {
73                             String JavaDoc def = (String JavaDoc)endpoint.getProperties().get("default");
74                             if (def != null)
75                             {
76                                 result = send(session, message, endpoint);
77                             }
78                             else
79                             {
80                                 send(session, message, endpoint);
81                             }
82                         }
83                     }
84                     else
85                     {
86                         dispatch(session, message, endpoint);
87                     }
88                 }
89             }
90         }
91         catch (UMOException e)
92         {
93             throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint)endpoints.get(0), e);
94         }
95         return result;
96     }
97 }
98
Popular Tags