View Javadoc

1   /*
2    * $Id: MulticastingRouter.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.config.i18n.CoreMessages;
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.RoutePathNotFoundException;
20  import org.mule.umo.routing.RoutingException;
21  
22  /**
23   * <code>MulticastingRouter</code> will broadcast the current message to every
24   * endpoint registed with the router.
25   * 
26   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
27   * @version $Revision: 7976 $
28   */
29  
30  public class MulticastingRouter extends FilteringOutboundRouter
31  {
32  
33      public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous)
34          throws RoutingException
35      {
36          UMOMessage result = null;
37          if (endpoints == null || endpoints.size() == 0)
38          {
39              throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null);
40          }
41          if (enableCorrelation != ENABLE_CORRELATION_NEVER)
42          {
43              boolean correlationSet = message.getCorrelationId() != null;
44              if (correlationSet && (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET))
45              {
46                  logger.debug("CorrelationId is already set, not setting Correlation group size");
47              }
48              else
49              {
50                  // the correlationId will be set by the AbstractOutboundRouter
51                  message.setCorrelationGroupSize(endpoints.size());
52              }
53          }
54  
55          try
56          {
57              UMOEndpoint endpoint;
58              synchronized (endpoints)
59              {
60                  for (int i = 0; i < endpoints.size(); i++)
61                  {
62                      endpoint = (UMOEndpoint) endpoints.get(i);
63                      if (synchronous)
64                      {
65                          // Were we have multiple outbound endpoints
66                          if (result == null)
67                          {
68                              result = send(session, message, endpoint);
69                          }
70                          else
71                          {
72                              String def = (String) endpoint.getProperties().get("default");
73                              if (def != null)
74                              {
75                                  result = send(session, message, endpoint);
76                              }
77                              else
78                              {
79                                  send(session, message, endpoint);
80                              }
81                          }
82                      }
83                      else
84                      {
85                          dispatch(session, message, endpoint);
86                      }
87                  }
88              }
89          }
90          catch (UMOException e)
91          {
92              throw new CouldNotRouteOutboundMessageException(message, (UMOEndpoint) endpoints.get(0), e);
93          }
94          return result;
95      }
96  }