View Javadoc

1   /*
2    * $Id: FirstSuccessful.java 22597 2011-08-05 20:40:24Z 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                  MuleEvent toProcess = cloneEventForRoutinng(event, mp);
64                  returnEvent = mp.process(toProcess);
65  
66                  if (returnEvent == null)
67                  {
68                      failed = false;
69                  }
70                  else
71                  {
72                      MuleMessage msg = returnEvent.getMessage();
73                      failed = msg == null || failureExpressionFilter.accept(msg);
74                  }
75              }
76              catch (Exception ex)
77              {
78                  failed = true;
79              }
80              if (!failed)
81              {
82                  break;
83              }
84          }
85  
86          if (failed)
87          {
88              throw new CouldNotRouteOutboundMessageException(event, this);
89          }
90  
91          return returnEvent;
92      }
93  
94      protected MuleEvent cloneEventForRoutinng(MuleEvent event, MessageProcessor mp)
95      {
96          return createEventToRoute(event, cloneMessage(event.getMessage()), mp);
97      }
98  
99      @Override
100     public boolean isMatch(MuleMessage message) throws MuleException
101     {
102         return true;
103     }
104 
105     /**
106      * Specifies an expression that when evaluated as determines if the processing of
107      * one a route was a failure or not.
108      *
109      * @param failureExpression
110      * @see ExpressionFilter
111      */
112     public void setFailureExpression(String failureExpression)
113     {
114         this.failureExpression = failureExpression;
115     }
116 }