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