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.MuleException;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.endpoint.InboundEndpoint;
13  import org.mule.api.expression.ExpressionManager;
14  import org.mule.api.service.Service;
15  import org.mule.api.transport.Connector;
16  import org.mule.transport.AbstractPollingMessageReceiver;
17  import org.mule.transport.NullPayload;
18  
19  import java.lang.reflect.Method;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
24  
25  /**
26   * <code>IBeansMessageReceiver</code> TODO document
27   */
28  public class IBeansMessageReceiver extends  AbstractPollingMessageReceiver
29  {
30      private Object ibean;
31      private Method ibeanMethod;
32      private Object[] callParams;
33      private String methodName;
34  
35      public IBeansMessageReceiver(Connector connector, Service service, InboundEndpoint endpoint)
36              throws MuleException
37      {
38          super(connector, service, endpoint);
39          setFrequency(60);
40          setTimeUnit(TimeUnit.SECONDS);
41  
42          List<?> state = (List)endpoint.getProperty(IBeansConnector.STATE_PARAMS_PROPERTY);
43          if(state==null)
44          {
45              state  = Collections.emptyList();
46          }
47  
48          ibean = ((IBeansConnector)connector).createIbean(endpoint.getEndpointURI(), state);
49          //Note that the address has already been validated by the {@link IBeansEndpointURIBuilder}
50          String address = endpoint.getEndpointURI().getAddress();
51          methodName = address.substring(address.indexOf(".")+1);
52  
53          List params = (List)endpoint.getProperty(IBeansConnector.CALL_PARAMS_PROPERTY);
54          if(params==null) params = Collections.emptyList();
55  
56          ExpressionManager em = connector.getMuleContext().getExpressionManager();        
57          MuleMessage defaultMessage = new DefaultMuleMessage(NullPayload.getInstance(), endpoint.getProperties(), connector.getMuleContext());
58  
59          callParams = new Object[params.size()];
60          int i = 0;
61          for (Object param : params)
62          {
63              if(param instanceof String && em.isExpression(param.toString()))
64              {
65                  param = em.parse(param.toString(), defaultMessage);
66              }
67              callParams[i++] = param;
68          }
69  
70      }
71      
72      public void poll() throws Exception
73      {
74          if(ibeanMethod==null)
75          {
76              ibeanMethod = getMethod();
77          }
78          ibeanMethod.invoke(ibean, callParams);
79      }
80  
81      protected Method getMethod() throws NoSuchMethodException
82      {
83          Class<?>[] paramTypes = new Class<?>[callParams.length];
84          int i = 0;
85          for (Object param : callParams)
86          {
87              paramTypes[i++] = param.getClass();
88          }
89          return ibean.getClass().getMethod(methodName, paramTypes);
90      }
91  }