KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > rmi > RmiMessageReceiver


1 /*
2  * $Id: RmiMessageReceiver.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.rmi;
12
13 import org.apache.commons.collections.MapUtils;
14 import org.mule.impl.MuleMessage;
15 import org.mule.providers.ConnectException;
16 import org.mule.providers.PollingMessageReceiver;
17 import org.mule.umo.UMOComponent;
18 import org.mule.umo.endpoint.UMOEndpoint;
19 import org.mule.umo.lifecycle.InitialisationException;
20 import org.mule.umo.provider.UMOConnector;
21 import org.mule.util.ClassUtils;
22 import org.mule.config.MuleProperties;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.rmi.RMISecurityManager JavaDoc;
26 import java.rmi.Remote JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  * Will repeatedly call a method on a Remote object. If the method takes parameters A
31  * List of objects can be specified on the endpoint called
32  * <code>methodArgumentsList</code>, If this property is ommitted it is assumed
33  * that the method takes no parameters
34  */

35
36 public class RmiMessageReceiver extends PollingMessageReceiver
37 {
38     protected RmiConnector connector;
39
40     protected Remote JavaDoc remoteObject;
41
42     protected Method JavaDoc invokeMethod;
43
44     protected Object JavaDoc[] methodArguments = null;
45
46     public RmiMessageReceiver(UMOConnector connector,
47                               UMOComponent component,
48                               UMOEndpoint endpoint,
49                               Long JavaDoc frequency) throws InitialisationException
50     {
51         super(connector, component, endpoint, frequency);
52
53         this.connector = (RmiConnector)connector;
54     }
55
56     public void doConnect() throws Exception JavaDoc
57     {
58         System.setProperty("java.security.policy", connector.getSecurityPolicy());
59
60         // Set security manager
61
if (System.getSecurityManager() == null)
62         {
63             System.setSecurityManager(new RMISecurityManager JavaDoc());
64         }
65
66         String JavaDoc methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(),
67             MuleProperties.MULE_METHOD_PROPERTY, null);
68
69         if (null == methodName)
70         {
71             methodName = (String JavaDoc)endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
72
73             if (null == methodName)
74             {
75                 throw new ConnectException(new org.mule.config.i18n.Message("rmi",
76                     RmiConnector.MSG_PARAM_SERVICE_METHOD_NOT_SET), this);
77             }
78         }
79
80         remoteObject = connector.getRemoteObject(getEndpoint());
81
82         List JavaDoc args = (List JavaDoc)endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST);
83
84         Class JavaDoc[] argTypes = new Class JavaDoc[]{};
85
86         if (args == null)
87         {
88             logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST
89                         + " not set on endpoint, assuming method call has no arguments");
90             methodArguments = ClassUtils.NO_ARGS;
91         }
92         else
93         {
94             argTypes = ClassUtils.getClassTypes(methodArguments);
95             methodArguments = new Object JavaDoc[args.size()];
96             methodArguments = args.toArray(methodArguments);
97         }
98         invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes);
99     }
100
101     public void doDisconnect()
102     {
103         invokeMethod = null;
104         remoteObject = null;
105     }
106
107     public void poll()
108     {
109         try
110         {
111             Object JavaDoc result = invokeMethod.invoke(remoteObject, getMethodArguments());
112
113             if (null != result)
114             {
115                 final Object JavaDoc payload = connector.getMessageAdapter(result).getPayload();
116                 routeMessage(new MuleMessage(payload), endpoint.isSynchronous());
117             }
118         }
119         catch (Exception JavaDoc e)
120         {
121             handleException(e);
122         }
123     }
124
125     /**
126      * Returns the method arguments to use when invoking the method on the Remote
127      * object. This method can be overloaded to enable dynamic method arguments
128      *
129      * @return
130      */

131     protected Object JavaDoc[] getMethodArguments()
132     {
133         return methodArguments;
134     }
135 }
136
Popular Tags