View Javadoc

1   /*
2    * $Id: DefaultOutboundRouterCollection.java 12181 2008-06-26 20:05:55Z 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.api.MessagingException;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.MuleSession;
16  import org.mule.api.routing.OutboundRouter;
17  import org.mule.api.routing.OutboundRouterCollection;
18  import org.mule.api.routing.RoutingException;
19  import org.mule.api.transaction.TransactionCallback;
20  import org.mule.management.stats.RouterStatistics;
21  import org.mule.routing.AbstractRouterCollection;
22  import org.mule.transaction.TransactionTemplate;
23  
24  import java.util.Iterator;
25  
26  /**
27   * <code>DefaultOutboundRouterCollection</code> is a container of routers. An
28   * DefaultOutboundRouterCollection must have atleast one router. By default the first matching
29   * router is used to route an event though it is possible to match on all routers
30   * meaning that the message will get sent over all matching routers.
31   */
32  
33  public class DefaultOutboundRouterCollection extends AbstractRouterCollection implements OutboundRouterCollection
34  {
35  
36      public DefaultOutboundRouterCollection()
37      {
38          super(RouterStatistics.TYPE_OUTBOUND);
39      }
40  
41      public MuleMessage route(final MuleMessage message, final MuleSession session, final boolean synchronous)
42              throws MessagingException
43      {
44          MuleMessage result;
45          boolean matchfound = false;
46  
47          for (Iterator iterator = getRouters().iterator(); iterator.hasNext();)
48          {
49              OutboundRouter umoOutboundRouter = (OutboundRouter) iterator.next();
50              if (umoOutboundRouter.isMatch(message))
51              {
52                  matchfound = true;
53                  // Manage outbound only transactions here
54                  final OutboundRouter router = umoOutboundRouter;
55  
56                  
57                  TransactionTemplate tt = new TransactionTemplate(umoOutboundRouter.getTransactionConfig(),
58                      session.getService().getExceptionListener(), muleContext);
59                  
60                  TransactionCallback cb = new TransactionCallback()
61                  {
62                      public Object doInTransaction() throws Exception
63                      {
64                          return router.route(message, session, synchronous);
65                      }
66                  };
67                  try
68                  {
69                      result = (MuleMessage) tt.execute(cb);
70                  }
71                  catch (Exception e)
72                  {
73                      throw new RoutingException(message, null, e);
74                  }
75  
76                  if (!isMatchAll())
77                  {
78                      return result;
79                  }
80              }
81          }
82  
83          if (!matchfound && getCatchAllStrategy() != null)
84          {
85              if (logger.isDebugEnabled())
86              {
87                  logger.debug("Message did not match any routers on: "
88                          + session.getService().getName()
89                          + " invoking catch all strategy");
90              }
91              return catchAll(message, session, synchronous);
92          }
93          else if (!matchfound)
94          {
95              logger.warn("Message did not match any routers on: "
96                      + session.getService().getName()
97                      + " and there is no catch all strategy configured on this router.  Disposing message " + message);
98          }
99          return message;
100     }
101 
102     protected MuleMessage catchAll(MuleMessage message, MuleSession session, boolean synchronous)
103             throws RoutingException
104     {
105         if (getStatistics().isEnabled())
106         {
107             getStatistics().incrementCaughtMessage();
108         }
109 
110         return getCatchAllStrategy().catchMessage(message, session, synchronous);
111     }
112 
113     public boolean hasEndpoints()
114     {
115         for (Iterator iterator = routers.iterator(); iterator.hasNext();)
116         {
117             OutboundRouter router = (OutboundRouter) iterator.next();
118             if (router.getEndpoints().size() > 0 || router.isDynamicEndpoints())
119             {
120                 return true;
121             }
122         }
123         return false;
124     }
125 
126 }