View Javadoc

1   /*
2    * $Id: RmiMessageReceiver.java 10961 2008-02-22 19:01:02Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.DefaultMuleMessage;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.api.lifecycle.CreateException;
17  import org.mule.api.service.Service;
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>methodArgumentsList</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                                Service service,
50                                InboundEndpoint endpoint,
51                                long frequency) throws CreateException
52      {
53          super(connector, service, endpoint);
54          this.setFrequency(frequency);
55          this.connector = (RmiConnector) connector;
56      }
57  
58      protected void doDispose()
59      {
60          // template method
61      }
62  
63      protected void doConnect() throws Exception
64      {
65          System.setProperty("java.security.policy", connector.getSecurityPolicy());
66  
67          // Set security manager
68          if (System.getSecurityManager() == null)
69          {
70              System.setSecurityManager(new RMISecurityManager());
71          }
72  
73          String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(),
74                  MuleProperties.MULE_METHOD_PROPERTY, null);
75  
76          if (null == methodName)
77          {
78              methodName = (String) endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
79  
80              if (null == methodName)
81              {
82                  throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this);
83              }
84          }
85  
86          remoteObject = connector.getRemoteObject(getEndpoint());
87  
88          List args = (List) endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST);
89  
90          Class[] argTypes = new Class[]{};
91  
92          if (args == null)
93          {
94              logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST
95                      + " not set on endpoint, assuming method call has no arguments");
96              methodArguments = ClassUtils.NO_ARGS;
97          }
98          else
99          {
100             argTypes = ClassUtils.getClassTypes(methodArguments);
101             methodArguments = new Object[args.size()];
102             methodArguments = args.toArray(methodArguments);
103         }
104         invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes);
105     }
106 
107     protected void doDisconnect()
108     {
109         invokeMethod = null;
110         remoteObject = null;
111     }
112 
113     public void poll()
114     {
115         try
116         {
117             Object result = invokeMethod.invoke(remoteObject, getMethodArguments());
118 
119             if (null != result)
120             {
121                 final Object payload = connector.getMessageAdapter(result).getPayload();
122                 routeMessage(new DefaultMuleMessage(payload), endpoint.isSynchronous());
123             }
124         }
125         catch (Exception e)
126         {
127             handleException(e);
128         }
129     }
130 
131     /**
132      * Returns the method arguments to use when invoking the method on the Remote
133      * object. This method can be overloaded to enable dynamic method arguments
134      *
135      * @return
136      */
137     protected Object[] getMethodArguments()
138     {
139         return methodArguments;
140     }
141 }