View Javadoc

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