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.routing.filters.WildcardFilter;
12  import org.mule.util.ClassUtils;
13  import org.mule.util.StringMessageUtils;
14  import org.mule.util.StringUtils;
15  
16  import java.lang.reflect.Method;
17  import java.util.Arrays;
18  import java.util.Collections;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  
23  /**
24   * <code>ReflectEntryPointResolver</code> is used to determine the entry point on a service
25   * after an event has been received for it. The entrypoint is  discovered using
26   * the event payload type(s) as the argument using reflection. An entry point will try and match for
27   * different argument types, so it's possible to have multiple entry points on a
28   * single service.
29   * <p/>
30   * For multiple parameters the payload of context.getMessage().getPayload() should be an Array of objects.
31   * If the message payload is of type {@link org.mule.transport.NullPayload} the resolver will look for a no-argument
32   * method to call that doesn't match the set of ignoredMethods on the resolver.
33   * <p/>
34   * Also a set of 'ignored' methods are available (and the use can add others) to tell the resolver to not
35   * resolve to these methods. The default ones are:
36   * <ul>
37   * <li>{@link #toString()}
38   * <li>{@link #getClass()}
39   * <li>{@link #notify}
40   * <li>{@link #notifyAll}
41   * <li>{@link #hashCode}
42   * <li>{@link #wait}
43   * <li>{@link java.lang.reflect.Proxy#getInvocationHandler}
44   * <li>'is*'
45   * <li>'get*'.
46   * <li>'set*'.
47   * </ul>
48   * <p/> Note that wildcard expressions can be used.
49   */
50  public class ReflectionEntryPointResolver extends AbstractEntryPointResolver
51  {
52      // we don't want to match these methods when looking for a service method
53      private Set<String> ignoredMethods = new HashSet<String>(Arrays.asList("equals",
54              "getInvocationHandler", "set*", "toString",
55              "getClass", "notify", "notifyAll", "wait", "hashCode", "clone", "is*", "get*"));
56  
57      protected WildcardFilter filter;
58  
59      public ReflectionEntryPointResolver()
60      {
61          updateFilter();
62      }
63  
64      private void updateFilter()
65      {
66          filter = new WildcardFilter(StringUtils.join(ignoredMethods, ','));
67      }
68  
69      /**
70       * Returns an unmodifiable Set of ignoredMethods on this resolver
71       * To add method to the resolver use {@link #addIgnoredMethod(String)}
72       *
73       * @return unmodifiable set of method names set on this resolver
74       */
75      public Set<String> getIgnoredMethods()
76      {
77          return Collections.unmodifiableSet(ignoredMethods);
78      }
79  
80      public void setIgnoredMethods(Set<String> methods)
81      {
82          this.ignoredMethods = new HashSet<String>(methods);
83          updateFilter();
84      }
85  
86      public void addIgnoredMethod(String name)
87      {
88          this.ignoredMethods.add(name);
89          updateFilter();
90      }
91  
92      public boolean removeIgnoredMethod(String name)
93      {
94          boolean result = this.ignoredMethods.remove(name);
95          updateFilter();
96          return result;
97      }
98  
99      /**
100      * Will discover the entrypoint on the service using the payload type to figure out the method to call.
101      * For multiple parameters the payload of context.getMessage().geTPayload() should be an Array of objects.
102      * If the message payload is of type {@link org.mule.transport.NullPayload} the resolver will look for a no-argument
103      * method to call that doesn't match the set of ignoredMethods on the resover.
104      *
105      * @throws Exception
106      */
107     public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
108     {
109         Object[] payload = getPayloadFromMessage(context);
110 
111         Method method;
112         InvocationResult result;
113 
114         method = this.getMethodByArguments(component, payload);
115 
116         if (method != null)
117         {
118             return invokeMethod(component, method, payload);
119         }
120 
121         Class<?>[] types = ClassUtils.getClassTypes(payload);
122 
123         // do any methods on the service accept a context?
124         List<Method> methods = ClassUtils.getSatisfiableMethods(component.getClass(), types,
125                 isAcceptVoidMethods(), false, ignoredMethods, filter);
126 
127         int numMethods = methods.size();
128         if (numMethods > 1)
129         {
130             result = new InvocationResult(this, InvocationResult.State.FAILED);
131             // too many methods match the context argument
132             result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods));
133             return result;
134 
135         }
136         else if (numMethods == 1)
137         {
138             // found exact match for method with context argument
139             method = this.addMethodByArguments(component, methods.get(0), payload);
140         }
141         else
142         {
143             methods = ClassUtils.getSatisfiableMethods(component.getClass(), 
144                 ClassUtils.getClassTypes(payload), true, true, ignoredMethods);
145 
146             numMethods = methods.size();
147 
148             if (numMethods > 1)
149             {
150                 result = new InvocationResult(this, InvocationResult.State.FAILED);
151                 // too many methods match the context argument
152                 result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods));
153                 return result;
154             }
155             else if (numMethods == 1)
156             {
157                 // found exact match for payload argument
158                 method = this.addMethodByArguments(component, methods.get(0), payload);
159             }
160             else
161             {
162                 result = new InvocationResult(this, InvocationResult.State.FAILED);
163                 // no method for payload argument either - bail out
164                 result.setErrorNoMatchingMethods(component, ClassUtils.getClassTypes(payload));
165                 return result;
166             }
167         }
168 
169         return invokeMethod(component, method, payload);
170     }
171 
172 
173     @Override
174     public String toString()
175     {
176         final StringBuffer sb = new StringBuffer();
177         sb.append("ReflectionEntryPointResolver");
178         sb.append("{ignoredMethods=").append(StringMessageUtils.toString(ignoredMethods));
179         sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
180         sb.append('}');
181         return sb.toString();
182     }
183 }