View Javadoc

1   /*
2    * $Id$
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  /**
12   * FirstSuccessful is a message processor that has a list of target MPs.  Each reveiced event is routed to each
13   * target, in order,  until one succeeds by not throwing an exception.
14   */
15  package org.mule.routing;
16  
17  import org.mule.DefaultMuleEvent;
18  import org.mule.api.MessagingException;
19  import org.mule.api.MuleEvent;
20  import org.mule.api.MuleException;
21  import org.mule.api.MuleMessage;
22  import org.mule.api.endpoint.OutboundEndpoint;
23  import org.mule.api.processor.MessageProcessor;
24  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
25  import org.mule.routing.outbound.AbstractOutboundRouter;
26  
27  
28  /**
29   *  FirstSuccessful routes an event to the first target route that can accept it without throwing or returning an 
30   * exception.  If no such route can be found, an exception is thrown. Note that this works more reliable with
31   * synchronous targets, but no such restriction is imposed.
32   */
33  public class FirstSuccessful extends AbstractOutboundRouter implements MessageProcessor
34  {
35      /**
36       * Route the given event to one of our targets
37       */
38      @Override
39      public MuleEvent route(MuleEvent event) throws MessagingException
40      {
41          MuleEvent retval = null;
42  
43          boolean failed = true;
44          for (MessageProcessor mp : routes)
45          {
46              try
47              {
48                  MuleEvent toProcess = event;
49                  if (mp instanceof OutboundEndpoint)
50                  {
51                      toProcess = new DefaultMuleEvent(event.getMessage(), (OutboundEndpoint)mp, event.getSession());
52                  }
53                  retval = mp.process(toProcess);
54                  if (retval == null)
55                  {
56                      failed = false;
57                  }
58                  else
59                  {
60                      MuleMessage msg = retval.getMessage();
61                      failed = msg != null && msg.getExceptionPayload() != null;
62                  }
63              }
64              catch (Exception ex)
65              {
66                  failed = true;
67              }
68              if (!failed)
69                  break;
70          }
71  
72          if (failed)
73          {
74              throw new CouldNotRouteOutboundMessageException(event, this);
75          }
76  
77          return retval;
78      }
79  
80      public boolean isMatch(MuleMessage message) throws MuleException
81      {
82          return true;
83      }
84  }