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