Coverage Report - org.mule.transport.ibean.IBeansMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
IBeansMessageDispatcher
0%
0/28
0%
0/8
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.transport.ibean;
 8  
 
 9  
 import org.mule.DefaultMuleMessage;
 10  
 import org.mule.api.MuleEvent;
 11  
 import org.mule.api.MuleException;
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.config.MuleProperties;
 14  
 import org.mule.api.endpoint.OutboundEndpoint;
 15  
 import org.mule.api.transport.DispatchException;
 16  
 import org.mule.module.ibeans.i18n.IBeansMessages;
 17  
 import org.mule.transport.AbstractMessageDispatcher;
 18  
 
 19  
 import java.lang.reflect.Method;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collections;
 22  
 import java.util.List;
 23  
 
 24  
 import org.ibeans.impl.IBeansNotationHelper;
 25  
 
 26  
 /**
 27  
  * <code>IBeansMessageDispatcher</code> TODO document
 28  
  */
 29  
 public class IBeansMessageDispatcher extends AbstractMessageDispatcher
 30  
 {
 31  
 
 32  
     private Object ibean;
 33  
     //We only cache the method name and resolve it for every request.  this allows for invoking overloaded methods
 34  
     //depending on the payload parameters of the event
 35  
     private String method;
 36  
     private String ibeanName;
 37  
 
 38  
     public IBeansMessageDispatcher(OutboundEndpoint endpoint) throws MuleException
 39  
     {
 40  0
         super(endpoint);
 41  0
         List state = (List)endpoint.getProperty(IBeansConnector.STATE_PARAMS_PROPERTY);
 42  0
         if(state==null)
 43  
         {
 44  0
             state  = Collections.emptyList();
 45  
         }
 46  0
         IBeansConnector cnn = (IBeansConnector)getConnector();
 47  0
         ibean = cnn.createIbean(endpoint.getEndpointURI(), state);
 48  0
         ibeanName = IBeansNotationHelper.getIBeanShortID(ibean.getClass().getInterfaces()[0]);
 49  
         //Note that the address has already been validated by the {@link IBeansEndpointURIBuilder}
 50  0
         String address = endpoint.getEndpointURI().getAddress();
 51  0
         method = address.substring(address.indexOf(".")+1);
 52  
 
 53  0
     }
 54  
 
 55  
     @Override
 56  
     public void doDispatch(MuleEvent event) throws Exception
 57  
     {
 58  0
         doSend(event);
 59  0
     }
 60  
 
 61  
     @Override
 62  
     public MuleMessage doSend(MuleEvent event) throws Exception
 63  
     {
 64  0
         Object payload = event.getMessage().getPayload();
 65  
         Object[] params;
 66  
         //Lets create the params array from the request
 67  0
         if(payload.getClass().isArray())
 68  
         {
 69  0
             params = (Object[])payload;
 70  
         }
 71  0
         else if (payload instanceof ArrayList)
 72  
         {
 73  0
             params = ((ArrayList) payload).toArray();
 74  
         }
 75  
         else
 76  
         {
 77  0
             params = new Object[]{payload};
 78  
         }
 79  
 
 80  
         //Create an array of types we can use to look up the override method if there is one, or validate the parameters
 81  
         //against the expected parameters for the IBean method
 82  0
         Class<?>[] types = new Class[params.length];
 83  
 
 84  0
         for (int i = 0; i < params.length; i++)
 85  
         {
 86  0
              types[i] = params[i].getClass();
 87  
         }
 88  
 
 89  
         Method callMethod;
 90  
         //The method property can be set in the Invocation scope to override the method set on the endpoint
 91  0
         String methodName = event.getMessage().getInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, method);
 92  
             try
 93  
             {
 94  0
                 callMethod = ibean.getClass().getMethod(methodName, types);
 95  
             }
 96  0
             catch (Throwable e)
 97  
             {
 98  0
                 throw new DispatchException(IBeansMessages.ibeanMethodNotFound(ibeanName, methodName, types), event, this, e);
 99  0
             }
 100  
 
 101  0
         Object result = callMethod.invoke(ibean, params);
 102  0
         return new DefaultMuleMessage(result, event.getMessage(), event.getMuleContext());
 103  
     }
 104  
 }
 105