Coverage Report - org.mule.model.resolvers.ExplicitMethodEntryPointResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
ExplicitMethodEntryPointResolver
0%
0/35
0%
0/14
0
 
 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.config.i18n.CoreMessages;
 12  
 import org.mule.util.ClassUtils;
 13  
 import org.mule.util.StringMessageUtils;
 14  
 
 15  
 import java.lang.reflect.Method;
 16  
 import java.util.Iterator;
 17  
 import java.util.LinkedHashSet;
 18  
 import java.util.Set;
 19  
 
 20  
 /**
 21  
  * An Entry-point resolver that allows the user to set one or more acceptable method names to look for.
 22  
  * For each method reflection will be used to see if the method accepts the current payload types 
 23  
  * (the results are cached to improve performance). There has to be at least one method name set 
 24  
  * on this resolver
 25  
  */
 26  0
 public class ExplicitMethodEntryPointResolver extends AbstractEntryPointResolver
 27  
 {
 28  
 
 29  0
     private Set<String> methods = new LinkedHashSet<String>(2);
 30  
 
 31  
     public void setMethods(Set<String> methods)
 32  
     {
 33  0
         this.methods.addAll(methods);
 34  0
     }
 35  
 
 36  
     public void addMethod(String name)
 37  
     {
 38  0
         this.methods.add(name);
 39  0
     }
 40  
 
 41  
     public boolean removeMethod(String name)
 42  
     {
 43  0
         return this.methods.remove(name);
 44  
     }
 45  
 
 46  
     public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
 47  
     {
 48  0
         if (methods == null || methods.size() == 0)
 49  
         {
 50  0
             throw new IllegalStateException(CoreMessages.objectIsNull("methods").toString());
 51  
         }
 52  
 
 53  0
         Object[] payload = getPayloadFromMessage(context);
 54  0
         Class<?>[] classTypes = ClassUtils.getClassTypes(payload);
 55  0
         Method method = null;
 56  0
         for (Iterator<String> iterator = methods.iterator(); iterator.hasNext();)
 57  
         {
 58  0
             String methodName = iterator.next();
 59  0
             method = getMethodByName(component, methodName, context);
 60  
 
 61  0
             if (method == null)
 62  
             {
 63  0
                 method = ClassUtils.getMethod(component.getClass(), methodName, classTypes, true);
 64  
             }
 65  0
             if (method != null)
 66  
             {
 67  0
                 addMethodByName(component, method, context);
 68  
                 
 69  
                 // check if the current payload can be handled by this method
 70  0
                 Class<?>[] parameterTypes = method.getParameterTypes();
 71  0
                 if (ClassUtils.compare(parameterTypes, classTypes, false, true))
 72  
                 {
 73  
                     // we found a matching method, let's invoke it
 74  0
                     break;
 75  
                 }
 76  
                 else
 77  
                 {
 78  
                     // zero out the reference to the method, it doesn't match
 79  0
                     method = null;
 80  
                 }
 81  
             }
 82  0
         }
 83  
 
 84  0
         if (method == null)
 85  
         {
 86  0
             InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
 87  0
             result.setErrorNoMatchingMethods(component, classTypes);
 88  0
             return result;
 89  
         }
 90  0
         return invokeMethod(component, method, payload);
 91  
     }
 92  
 
 93  
     @Override
 94  
     public String toString()
 95  
     {
 96  0
         final StringBuffer sb = new StringBuffer();
 97  0
         sb.append("ExplicitMethodEntryPointResolver");
 98  0
         sb.append("{methods=").append(StringMessageUtils.toString(methods));
 99  0
         sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
 100  0
         sb.append('}');
 101  0
         return sb.toString();
 102  
     }
 103  
 
 104  
 }