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