View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.routing;
8   
9   import java.util.Collection;
10  import java.util.Collections;
11  
12  import org.mule.api.MuleEvent;
13  import org.mule.api.processor.MessageProcessor;
14  import org.mule.api.routing.RoutePathNotFoundException;
15  import org.mule.api.routing.filter.Filter;
16  
17  /**
18   * Routes the event to a single<code>MessageProcessor</code> using a {@link Filter}
19   * to evaluate the event being processed and find the first route that can be used.
20   * <p>
21   * If a default route has been configured and no match has been found, the default
22   * route will be used. Otherwise it throws a {@link RoutePathNotFoundException}.
23   */
24  public class ChoiceRouter extends AbstractSelectiveRouter
25  {
26      @Override
27      protected Collection<MessageProcessor> selectProcessors(MuleEvent event)
28      {
29          for (MessageProcessorFilterPair mpfp : getConditionalMessageProcessors())
30          {
31              if (mpfp.getFilter().accept(event.getMessage()))
32              {
33                  return Collections.singleton(mpfp.getMessageProcessor());
34              }
35          }
36  
37          return Collections.emptySet();
38      }
39  }