View Javadoc
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          super(endpoint);
41          List state = (List)endpoint.getProperty(IBeansConnector.STATE_PARAMS_PROPERTY);
42          if(state==null)
43          {
44              state  = Collections.emptyList();
45          }
46          IBeansConnector cnn = (IBeansConnector)getConnector();
47          ibean = cnn.createIbean(endpoint.getEndpointURI(), state);
48          ibeanName = IBeansNotationHelper.getIBeanShortID(ibean.getClass().getInterfaces()[0]);
49          //Note that the address has already been validated by the {@link IBeansEndpointURIBuilder}
50          String address = endpoint.getEndpointURI().getAddress();
51          method = address.substring(address.indexOf(".")+1);
52  
53      }
54  
55      @Override
56      public void doDispatch(MuleEvent event) throws Exception
57      {
58          doSend(event);
59      }
60  
61      @Override
62      public MuleMessage doSend(MuleEvent event) throws Exception
63      {
64          Object payload = event.getMessage().getPayload();
65          Object[] params;
66          //Lets create the params array from the request
67          if(payload.getClass().isArray())
68          {
69              params = (Object[])payload;
70          }
71          else if (payload instanceof ArrayList)
72          {
73              params = ((ArrayList) payload).toArray();
74          }
75          else
76          {
77              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          Class<?>[] types = new Class[params.length];
83  
84          for (int i = 0; i < params.length; i++)
85          {
86               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          String methodName = event.getMessage().getInvocationProperty(MuleProperties.MULE_METHOD_PROPERTY, method);
92              try
93              {
94                  callMethod = ibean.getClass().getMethod(methodName, types);
95              }
96              catch (Throwable e)
97              {
98                  throw new DispatchException(IBeansMessages.ibeanMethodNotFound(ibeanName, methodName, types), event, this, e);
99              }
100 
101         Object result = callMethod.invoke(ibean, params);
102         return new DefaultMuleMessage(result, event.getMessage(), event.getMuleContext());
103     }
104 }
105