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.model.resolvers;
8   
9   import org.mule.api.MuleEventContext;
10  import org.mule.api.model.EntryPointResolver;
11  import org.mule.api.model.EntryPointResolverSet;
12  import org.mule.api.model.InvocationResult;
13  import org.mule.util.CollectionUtils;
14  
15  import java.util.HashSet;
16  import java.util.LinkedHashSet;
17  import java.util.Set;
18  
19  /**
20   * Provides the default implementation of an {@link org.mule.api.model.EntryPointResolverSet}
21   * It resolves a method to call on the given service when an event is received.
22   * This object maintains a set of Resolvers that will be used in order to resolve
23   * an entrypoint on a service object until one is found or until the set is
24   * exhausted.
25   */
26  public class DefaultEntryPointResolverSet implements EntryPointResolverSet
27  {
28      private final Set<EntryPointResolver> entryPointResolvers = new LinkedHashSet<EntryPointResolver>(4);
29  
30      public Object invoke(Object component, MuleEventContext context) throws Exception
31      {
32          Set<String> exceptions = new HashSet<String>();
33  
34          for (EntryPointResolver resolver : entryPointResolvers)
35          {
36              InvocationResult result = resolver.invoke(component, context);
37              if (result.getState() == InvocationResult.State.SUCCESSFUL)
38              {
39                  return result.getResult();
40              }
41              else
42              {
43                  if (result.hasError())
44                  {
45                      exceptions.add(result.getErrorMessage());
46                  }
47              }
48          }
49          throw new EntryPointNotFoundException(CollectionUtils.toString(exceptions, true));
50      }
51  
52      /**
53       * @return the entry point resolves configured in this resolver set. Note that access to the
54       * set is not thread safe. Client code must take proper precautions to synchronize.
55       */
56      public Set<EntryPointResolver> getEntryPointResolvers()
57      {
58          return entryPointResolvers;
59      }
60  
61      public void setEntryPointResolvers(Set<EntryPointResolver> entryPointResolvers)
62      {
63          synchronized (this.entryPointResolvers)
64          {
65              this.entryPointResolvers.clear();
66              this.entryPointResolvers.addAll(entryPointResolvers);
67          }
68      }
69  
70      public void addEntryPointResolver(EntryPointResolver resolver)
71      {
72          synchronized (entryPointResolvers)
73          {
74              this.entryPointResolvers.add(resolver);
75          }
76      }
77  
78      public boolean removeEntryPointResolver(EntryPointResolver resolver)
79      {
80          synchronized (entryPointResolvers)
81          {
82              return this.entryPointResolvers.remove(resolver);
83          }
84      }
85  }