View Javadoc

1   /*
2    * $Id: FirstSuccessful.java 20471 2010-12-06 15:45:03Z 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;
12  
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.lifecycle.InitialisationException;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.api.routing.CouldNotRouteOutboundMessageException;
20  import org.mule.routing.filters.ExpressionFilter;
21  import org.mule.routing.outbound.AbstractOutboundRouter;
22  
23  /**
24   * FirstSuccessful routes an event to the first target route that can accept it
25   * without throwing or returning an exception. If no such route can be found, an
26   * exception is thrown. Note that this works more reliable with synchronous targets,
27   * but no such restriction is imposed.
28   */
29  public class FirstSuccessful extends AbstractOutboundRouter
30  {
31  
32      protected String failureExpression;
33      protected ExpressionFilter failureExpressionFilter;
34  
35      @Override
36      public void initialise() throws InitialisationException
37      {
38          super.initialise();
39          if (failureExpression != null)
40          {
41              failureExpressionFilter = new ExpressionFilter(failureExpression);
42          }
43          else
44          {
45              failureExpressionFilter = new ExpressionFilter("exception-type:");
46          }
47          failureExpressionFilter.setMuleContext(muleContext);
48      }
49  
50      /**
51       * Route the given event to one of our targets
52       */
53      @Override
54      public MuleEvent route(MuleEvent event) throws MessagingException
55      {
56          MuleEvent returnEvent = null;
57  
58          boolean failed = true;
59          for (MessageProcessor mp : routes)
60          {
61              try
62              {
63                  MuleMessage clonedMessage = cloneMessage(event.getMessage());
64                  MuleEvent toProcess = createEventToRoute(event, clonedMessage, mp);
65                  returnEvent = mp.process(toProcess);
66  
67                  if (returnEvent == null)
68                  {
69                      failed = false;
70                  }
71                  else
72                  {
73                      MuleMessage msg = returnEvent.getMessage();
74                      failed = msg == null || failureExpressionFilter.accept(msg);
75                  }
76              }
77              catch (Exception ex)
78              {
79                  failed = true;
80              }
81              if (!failed)
82              {
83                  break;
84              }
85          }
86  
87          if (failed)
88          {
89              throw new CouldNotRouteOutboundMessageException(event, this);
90          }
91  
92          return returnEvent;
93      }
94  
95      public boolean isMatch(MuleMessage message) throws MuleException
96      {
97          return true;
98      }
99  
100     /**
101      * Specifies an expression that when evaluated as determines if the processing of
102      * one a route was a failure or not.
103      * 
104      * @param failureExpression
105      * @see ExpressionFilter
106      */
107     public void setFailureExpression(String failureExpression)
108     {
109         this.failureExpression = failureExpression;
110     }
111 }