View Javadoc

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