Coverage Report - org.mule.model.resolvers.ReflectionEntryPointResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ReflectionEntryPointResolver
0%
0/47
0%
0/10
0
 
 1  
 /*
 2  
  * $Id: ReflectionEntryPointResolver.java 19191 2010-08-25 21:05:23Z tcarlson $
 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  0
     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  0
     {
 64  0
         updateFilter();
 65  0
     }
 66  
 
 67  
     private void updateFilter()
 68  
     {
 69  0
         filter = new WildcardFilter(StringUtils.join(ignoredMethods, ','));
 70  0
     }
 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  0
         return Collections.unmodifiableSet(ignoredMethods);
 81  
     }
 82  
 
 83  
     public void setIgnoredMethods(Set<String> methods)
 84  
     {
 85  0
         this.ignoredMethods = new HashSet<String>(methods);
 86  0
         updateFilter();
 87  0
     }
 88  
 
 89  
     public void addIgnoredMethod(String name)
 90  
     {
 91  0
         this.ignoredMethods.add(name);
 92  0
         updateFilter();
 93  0
     }
 94  
 
 95  
     public boolean removeIgnoredMethod(String name)
 96  
     {
 97  0
         boolean result = this.ignoredMethods.remove(name);
 98  0
         updateFilter();
 99  0
         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  0
         Object[] payload = getPayloadFromMessage(context);
 113  
 
 114  
         Method method;
 115  
         InvocationResult result;
 116  
 
 117  0
         method = this.getMethodByArguments(component, payload);
 118  
 
 119  0
         if (method != null)
 120  
         {
 121  0
             return invokeMethod(component, method, payload);
 122  
         }
 123  
 
 124  0
         Class<?>[] types = ClassUtils.getClassTypes(payload);
 125  
 
 126  
         // do any methods on the service accept a context?
 127  0
         List<Method> methods = ClassUtils.getSatisfiableMethods(component.getClass(), types,
 128  
                 isAcceptVoidMethods(), false, ignoredMethods, filter);
 129  
 
 130  0
         int numMethods = methods.size();
 131  0
         if (numMethods > 1)
 132  
         {
 133  0
             result = new InvocationResult(this, InvocationResult.State.FAILED);
 134  
             // too many methods match the context argument
 135  0
             result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods));
 136  0
             return result;
 137  
 
 138  
         }
 139  0
         else if (numMethods == 1)
 140  
         {
 141  
             // found exact match for method with context argument
 142  0
             method = this.addMethodByArguments(component, methods.get(0), payload);
 143  
         }
 144  
         else
 145  
         {
 146  0
             methods = ClassUtils.getSatisfiableMethods(component.getClass(), 
 147  
                 ClassUtils.getClassTypes(payload), true, true, ignoredMethods);
 148  
 
 149  0
             numMethods = methods.size();
 150  
 
 151  0
             if (numMethods > 1)
 152  
             {
 153  0
                 result = new InvocationResult(this, InvocationResult.State.FAILED);
 154  
                 // too many methods match the context argument
 155  0
                 result.setErrorTooManyMatchingMethods(component, types, StringMessageUtils.toString(methods));
 156  0
                 return result;
 157  
             }
 158  0
             else if (numMethods == 1)
 159  
             {
 160  
                 // found exact match for payload argument
 161  0
                 method = this.addMethodByArguments(component, methods.get(0), payload);
 162  
             }
 163  
             else
 164  
             {
 165  0
                 result = new InvocationResult(this, InvocationResult.State.FAILED);
 166  
                 // no method for payload argument either - bail out
 167  0
                 result.setErrorNoMatchingMethods(component, ClassUtils.getClassTypes(payload));
 168  0
                 return result;
 169  
             }
 170  
         }
 171  
 
 172  0
         return invokeMethod(component, method, payload);
 173  
     }
 174  
 
 175  
 
 176  
     @Override
 177  
     public String toString()
 178  
     {
 179  0
         final StringBuffer sb = new StringBuffer();
 180  0
         sb.append("ReflectionEntryPointResolver");
 181  0
         sb.append("{ignoredMethods=").append(StringMessageUtils.toString(ignoredMethods));
 182  0
         sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
 183  0
         sb.append('}');
 184  0
         return sb.toString();
 185  
     }
 186  
 }