View Javadoc

1   /*
2    * $Id: ExplicitMethodEntryPointResolver.java 20477 2010-12-06 23:38:52Z mike.schilling $
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.InvocationResult;
14  import org.mule.config.i18n.CoreMessages;
15  import org.mule.util.ClassUtils;
16  import org.mule.util.StringMessageUtils;
17  
18  import java.lang.reflect.Method;
19  import java.util.Iterator;
20  import java.util.LinkedHashSet;
21  import java.util.Set;
22  
23  /**
24   * An Entry-point resolver that allows the user to set one or more acceptable method names to look for.
25   * For each method reflection will be used to see if the method accepts the current payload types 
26   * (the results are cached to improve performance). There has to be at least one method name set 
27   * on this resolver
28   */
29  public class ExplicitMethodEntryPointResolver extends AbstractEntryPointResolver
30  {
31  
32      private Set<String> methods = new LinkedHashSet<String>(2);
33  
34      public void setMethods(Set<String> methods)
35      {
36          this.methods.addAll(methods);
37      }
38  
39      public void addMethod(String name)
40      {
41          this.methods.add(name);
42      }
43  
44      public boolean removeMethod(String name)
45      {
46          return this.methods.remove(name);
47      }
48  
49      public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
50      {
51          if (methods == null || methods.size() == 0)
52          {
53              throw new IllegalStateException(CoreMessages.objectIsNull("methods").toString());
54          }
55  
56          Object[] payload = getPayloadFromMessage(context);
57          Class<?>[] classTypes = ClassUtils.getClassTypes(payload);
58          Method method = null;
59          for (Iterator<String> iterator = methods.iterator(); iterator.hasNext();)
60          {
61              String methodName = iterator.next();
62              method = getMethodByName(component, methodName, context);
63  
64              if (method == null)
65              {
66                  method = ClassUtils.getMethod(component.getClass(), methodName, classTypes);
67              }
68              if (method != null)
69              {
70                  addMethodByName(component, method, context);
71                  
72                  // check if the current payload can be handled by this method
73                  Class<?>[] parameterTypes = method.getParameterTypes();
74                  if (ClassUtils.compare(parameterTypes, classTypes, false))
75                  {
76                      // we found a matching method, let's invoke it
77                      break;
78                  }
79                  else
80                  {
81                      // zero out the reference to the method, it doesn't match
82                      method = null;
83                  }
84              }
85          }
86  
87          if (method == null)
88          {
89              InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
90              result.setErrorNoMatchingMethods(component, classTypes);
91              return result;
92          }
93          return invokeMethod(component, method, payload);
94      }
95  
96      @Override
97      public String toString()
98      {
99          final StringBuffer sb = new StringBuffer();
100         sb.append("ExplicitMethodEntryPointResolver");
101         sb.append("{methods=").append(StringMessageUtils.toString(methods));
102         sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
103         sb.append('}');
104         return sb.toString();
105     }
106 
107 }