View Javadoc

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