View Javadoc

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