View Javadoc

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