View Javadoc

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