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.rmi;
8   
9   import org.mule.RequestContext;
10  import org.mule.api.config.MuleProperties;
11  import org.mule.api.construct.FlowConstruct;
12  import org.mule.api.endpoint.InboundEndpoint;
13  import org.mule.api.lifecycle.CreateException;
14  import org.mule.api.transport.Connector;
15  import org.mule.transport.AbstractPollingMessageReceiver;
16  import org.mule.transport.ConnectException;
17  import org.mule.transport.rmi.i18n.RmiMessages;
18  import org.mule.util.ClassUtils;
19  
20  import java.lang.reflect.Method;
21  import java.rmi.RMISecurityManager;
22  import java.rmi.Remote;
23  import java.util.List;
24  
25  import org.apache.commons.collections.MapUtils;
26  
27  /**
28   * Will repeatedly call a method on a Remote object. If the method takes parameters A
29   * List of objects can be specified on the endpoint called
30   * <code>methodArgumentTypes</code>, If this property is ommitted it is assumed
31   * that the method takes no parameters
32   */
33  
34  public class RmiMessageReceiver extends AbstractPollingMessageReceiver
35  {
36      protected RmiConnector connector;
37  
38      protected Remote remoteObject;
39  
40      protected Method invokeMethod;
41  
42      protected Object[] methodArguments = null;
43  
44      public RmiMessageReceiver(Connector connector,
45                                FlowConstruct flowConstruct,
46                                InboundEndpoint endpoint,
47                                long frequency) throws CreateException
48      {
49          super(connector, flowConstruct, endpoint);
50          this.setFrequency(frequency);
51          this.connector = (RmiConnector) connector;
52      }
53  
54      @Override
55      protected void doDispose()
56      {
57          // template method
58      }
59  
60      @SuppressWarnings("unchecked")
61      @Override
62      protected void doConnect() throws Exception
63      {
64          System.setProperty("java.security.policy", connector.getSecurityPolicy());
65  
66          // Set security manager
67          if (System.getSecurityManager() == null)
68          {
69              System.setSecurityManager(new RMISecurityManager());
70          }        
71  
72          // Get methodName
73          String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(),
74                  MuleProperties.MULE_METHOD_PROPERTY, null);
75          if (null == methodName)
76          {
77              methodName = (String) endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
78  
79              if (null == methodName)
80              {
81                  throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this);
82              }
83          }
84  
85          // Get remoteObject
86          remoteObject = connector.getRemoteObject(getEndpoint());
87  
88          // Set methodArguments
89          List<Object> args = (List<Object>) endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST);
90          Class[] argTypes = new Class[]{};
91          if (args == null)
92          {
93              logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST
94                      + " not set on endpoint, assuming method call has no arguments");
95              methodArguments = ClassUtils.NO_ARGS;
96          }
97          else
98          {
99              argTypes = connector.getArgTypes(endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES), RequestContext.getEvent());
100             methodArguments = new Object[args.size()];
101             methodArguments = args.toArray(methodArguments);
102         }
103         
104         // Set invokeMethod
105         invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes);
106     }
107 
108     @Override
109     protected void doDisconnect()
110     {
111         invokeMethod = null;
112         remoteObject = null;
113     }
114 
115     public void poll()
116     {
117         try
118         {
119             Object result = invokeMethod.invoke(remoteObject, getMethodArguments());
120 
121             if (null != result)
122             {
123                 routeMessage(createMuleMessage(result));
124             }
125         }
126         catch (Exception e)
127         {
128             getConnector().getMuleContext().getExceptionListener().handleException(e);
129         }
130     }
131 
132     /**
133      * Returns the method arguments to use when invoking the method on the Remote
134      * object. This method can be overloaded to enable dynamic method arguments
135      */
136     protected Object[] getMethodArguments()
137     {
138         return methodArguments;
139     }
140 }