View Javadoc

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