Coverage Report - org.mule.impl.model.resolvers.AnnotatedEntryPointResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
AnnotatedEntryPointResolver
0%
0/71
0%
0/34
0
 
 1  
 /*
 2  
  * $Id: AnnotatedEntryPointResolver.java 20140 2010-11-09 22:11:42Z 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.impl.model.resolvers;
 11  
 
 12  
 import org.mule.api.MuleEventContext;
 13  
 import org.mule.api.annotations.meta.Evaluator;
 14  
 import org.mule.api.config.MuleProperties;
 15  
 import org.mule.api.lifecycle.InitialisationException;
 16  
 import org.mule.api.model.InvocationResult;
 17  
 import org.mule.api.transformer.Transformer;
 18  
 import org.mule.api.transformer.TransformerException;
 19  
 import org.mule.api.transport.PropertyScope;
 20  
 import org.mule.config.expression.ExpressionAnnotationsHelper;
 21  
 import org.mule.config.i18n.CoreMessages;
 22  
 import org.mule.expression.transformers.ExpressionTransformer;
 23  
 import org.mule.model.resolvers.AbstractEntryPointResolver;
 24  
 import org.mule.transport.NullPayload;
 25  
 import org.mule.util.ClassUtils;
 26  
 import org.mule.util.annotation.AnnotationUtils;
 27  
 
 28  
 import java.lang.reflect.Method;
 29  
 import java.util.Map;
 30  
 
 31  
 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
 32  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 33  
 
 34  
 import net.sf.cglib.proxy.Enhancer;
 35  
 
 36  
 /**
 37  
  * A Mule {@link org.mule.api.model.EntryPointResolver} implementation that will resolve methods on a service class
 38  
  * that have Mule expression annotations such as {@link org.mule.api.annotations.param.Payload}, {@link org.mule.api.annotations.param.InboundHeaders}
 39  
  * It will transform the received message to match the annotated method arguments. For example -
 40  
  * <code>
 41  
  * public Object doSomething(
 42  
  *         &#64;XPath ("/foo/bar") String bar,
 43  
  *         &#64;Payload Document doc,
 44  
  *         &#64;InboundHeaders("name") String name)
 45  
  *  {
 46  
  *      //do stuff
 47  
  *  }
 48  
  * </code>
 49  
  *
 50  
  * The component method above will be invoked by running the XPath expression on the payload, adding a second parameter as
 51  
  * the payload itself and passing in the header 'name' as the third parameter.
 52  
  *
 53  
  * There are some rules for how components with annotations will be processed -
 54  
  * <ul>
 55  
  * <li>For components with more than one method annotated with Mule expression annotations the method name to call needs
 56  
  * to be set on the incoming message or endpoint using the {@link org.mule.api.config.MuleProperties#MULE_METHOD_PROPERTY} key.</li>
 57  
  * <li>If the component has only one method annotated with Mule expression annotations there is no need to set the method name to invoke</li>
 58  
  * <li>Every parameter on the method needs to be annotated</li>
 59  
  * </ul>
 60  
  *
 61  
  * @see org.mule.api.annotations.param.Payload
 62  
  * @see org.mule.api.annotations.param.InboundHeaders
 63  
  * @see org.mule.api.annotations.param.InboundAttachments
 64  
  * @see org.mule.api.annotations.param.OutboundHeaders
 65  
  * @see org.mule.api.annotations.param.OutboundAttachments
 66  
  * @see org.mule.api.annotations.expressions.Mule
 67  
  *
 68  
  * @since 3.0
 69  
  *
 70  
  */
 71  0
 public class AnnotatedEntryPointResolver extends AbstractEntryPointResolver
 72  
 {
 73  
 
 74  0
     private AtomicBoolean cacheBuilt = new AtomicBoolean(false);
 75  
 
 76  
 
 77  0
     @SuppressWarnings("unchecked")
 78  
     private Map<Method, Transformer> transformerCache = new ConcurrentHashMap();
 79  
 
 80  
     public InvocationResult invoke(Object component, MuleEventContext context) throws Exception
 81  
     {
 82  
         try
 83  
         {
 84  0
             initCache(component, context);
 85  
         }
 86  0
         catch (Exception e)
 87  
         {
 88  0
             InvocationResult result = new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED);
 89  0
             result.setErrorMessage(e.toString());
 90  0
             return result;
 91  0
         }
 92  
 
 93  0
         if(methodCache.size()==0)
 94  
         {
 95  0
             InvocationResult result = new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED);
 96  0
             result.setErrorMessage("Component: " + component + " doesn't have any annotated methods, skipping.");
 97  0
             return result;
 98  
         }
 99  
 
 100  
         Object[] payload;
 101  0
         Method method = null;
 102  
         //We remove the property here as a workaround to MULE-4769
 103  0
         Object tempMethod = context.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY, PropertyScope.INVOCATION);
 104  0
         String methodName = null;
 105  0
         if (tempMethod != null && tempMethod instanceof Method)
 106  
         {
 107  0
             method = (Method) tempMethod;
 108  
         }
 109  
         else
 110  
         {
 111  0
             methodName = (String)tempMethod;
 112  
         }
 113  
 
 114  
         //If a method param is set use that over anything else. This is used by the @Reply Callbacks and where annotations are set
 115  
         //on the method
 116  0
         if (methodName != null)
 117  
         {
 118  0
             method = getMethodByName(methodName, context);
 119  0
             if (method == null)
 120  
             {
 121  0
                 InvocationResult result = new InvocationResult(this, InvocationResult.State.NOT_SUPPORTED);
 122  0
                 result.setErrorMessage("Method not found: " + methodName + " on object: " + component.getClass() + ". If the component is a proxy there needs to be an interface on the proxy that defines this method");
 123  0
                 return result;
 124  
                 //TODO i18n
 125  
             }
 126  0
             payload = getPayloadForMethod(method, component, context);
 127  
         }
 128  0
         else if (method != null)
 129  
         {
 130  0
             payload = getPayloadForMethod(method, component, context);
 131  
         }
 132  0
         else if (methodCache.size() == 1)
 133  
         {
 134  0
             method = (Method) methodCache.values().iterator().next();
 135  0
             payload = getPayloadForMethod(method, component, context);
 136  
         }
 137  
         else
 138  
         {
 139  0
             InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
 140  0
             result.setErrorMessage("Component: " + component + " has more than one method annotated, which means the target method name needs to be set on the event");
 141  0
             return result;
 142  
         }
 143  0
         return invokeMethod(component, method,
 144  
                 (method.getParameterTypes().length == 0 ? ClassUtils.NO_ARGS : payload));
 145  
     }
 146  
 
 147  
     protected Object[] getPayloadForMethod(Method method, Object component, MuleEventContext context) throws TransformerException, InitialisationException
 148  
     {
 149  0
         Object[] payload = null;
 150  0
         Method m = method;
 151  
         //If we are using cglib enhanced service objects, we need to read annotations from the real component class
 152  0
         if (Enhancer.isEnhanced(component.getClass()))
 153  
         {
 154  
             try
 155  
             {
 156  0
                 m = component.getClass().getSuperclass().getMethod(method.getName(), method.getParameterTypes());
 157  
             }
 158  0
             catch (NoSuchMethodException e)
 159  
             {
 160  0
                 throw new TransformerException(CoreMessages.createStaticMessage(e.getMessage()), e);
 161  0
             }
 162  
         }
 163  
 
 164  0
         if (AnnotationUtils.getParamAnnotationsWithMeta(m, Evaluator.class).size() > 0)
 165  
         {
 166  0
             payload = getPayloadFromMessageWithAnnotations(m, context);
 167  
         }
 168  0
         return payload;
 169  
     }
 170  
 
 171  
     protected Object[] getPayloadFromMessageWithAnnotations(Method method, MuleEventContext context) throws TransformerException, InitialisationException
 172  
     {
 173  0
         ExpressionTransformer trans = (ExpressionTransformer) transformerCache.get(method);
 174  0
         if (trans == null)
 175  
         {
 176  0
             trans = ExpressionAnnotationsHelper.getTransformerForMethodWithAnnotations(method, context.getMuleContext());
 177  0
             transformerCache.put(method, trans);
 178  
         }
 179  
 
 180  0
         Object o = trans.transform(context.getMessage());
 181  0
         if (o instanceof NullPayload)
 182  
         {
 183  0
             return new Object[]{null};
 184  
         }
 185  0
         else if (o.getClass().isArray())
 186  
         {
 187  0
             return (Object[]) o;
 188  
         }
 189  
         else
 190  
         {
 191  0
             return new Object[]{o};
 192  
         }
 193  
     }
 194  
 
 195  
     @Override
 196  
     public String toString()
 197  
     {
 198  0
         final StringBuffer sb = new StringBuffer();
 199  0
         sb.append("AnnotatedEntryPointResolver");
 200  0
         sb.append(", acceptVoidMethods=").append(isAcceptVoidMethods());
 201  0
         sb.append('}');
 202  0
         return sb.toString();
 203  
     }
 204  
 
 205  
 
 206  
     protected void initCache(Object component, MuleEventContext context)
 207  
     {
 208  0
         if (!cacheBuilt.get())
 209  
         {
 210  0
             synchronized (this)
 211  
             {
 212  
                 try
 213  
                 {
 214  0
                     if (!cacheBuilt.get())
 215  
                     {
 216  0
                         for (int i = 0; i < component.getClass().getMethods().length; i++)
 217  
                         {
 218  0
                             Method method = component.getClass().getMethods()[i];
 219  0
                             if (AnnotationUtils.getParamAnnotationsWithMeta(method, Evaluator.class).size() > 0)
 220  
                             {
 221  0
                                 this.addMethodByName(method, context);
 222  
                             }
 223  
                         }
 224  
                     }
 225  
                 }
 226  
                 finally
 227  
                 {
 228  0
                     cacheBuilt.set(true);
 229  0
                 }
 230  0
             }
 231  
         }
 232  0
     }
 233  
 }