View Javadoc

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