View Javadoc

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