View Javadoc

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