View Javadoc

1   /*
2    * $Id: DefaultEntryPointResolverSet.java 20075 2010-11-04 20:11:25Z 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.LinkedHashSet;
19  import java.util.Set;
20  import java.util.concurrent.CopyOnWriteArraySet;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * Provides the default implementation of an {@link org.mule.api.model.EntryPointResolverSet}
27   * It resolves a method to call on the given service when an event is received.
28   * This object maintains a set of Resolvers that will be used in order to resolve
29   * an entrypoint on a service object until one is found or until the set is
30   * exhausted.
31   */
32  public class DefaultEntryPointResolverSet implements EntryPointResolverSet
33  {
34      protected final Log logger = LogFactory.getLog(getClass());
35  
36      private final Set<EntryPointResolver> entryPointResolvers = new LinkedHashSet<EntryPointResolver>(4);    
37      private final Set<String> exceptions = new CopyOnWriteArraySet<String>();
38  
39      public Object invoke(Object component, MuleEventContext context) throws Exception
40      {
41          try
42          {
43              for (EntryPointResolver resolver : entryPointResolvers)
44              {
45                  InvocationResult result = resolver.invoke(component, context);
46                  if (result.getState() == InvocationResult.State.SUCCESSFUL)
47                  {
48                      return result.getResult();
49                  }
50                  else
51                  {
52                      if (result.hasError())
53                      {
54                          exceptions.add(result.getErrorMessage());
55                      }
56                  }
57              }
58              throw new EntryPointNotFoundException(CollectionUtils.toString(exceptions, true));
59          }
60          finally
61          {
62              exceptions.clear();
63          }
64  
65      }
66  
67      public Set<EntryPointResolver> getEntryPointResolvers()
68      {
69          return entryPointResolvers;
70      }
71  
72      public void setEntryPointResolvers(Set<EntryPointResolver> entryPointResolvers)
73      {
74          this.entryPointResolvers.clear();
75          this.entryPointResolvers.addAll(entryPointResolvers);
76      }
77  
78      public void addEntryPointResolver(EntryPointResolver resolver)
79      {
80          synchronized (entryPointResolvers)
81          {
82              this.entryPointResolvers.add(resolver);
83          }
84      }
85  
86      public boolean removeEntryPointResolver(EntryPointResolver resolver)
87      {
88          return this.entryPointResolvers.remove(resolver);
89      }
90  }