1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.rmi; |
12 | |
|
13 | |
import org.mule.config.MuleProperties; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.providers.AbstractPollingMessageReceiver; |
16 | |
import org.mule.providers.ConnectException; |
17 | |
import org.mule.providers.rmi.i18n.RmiMessages; |
18 | |
import org.mule.umo.UMOComponent; |
19 | |
import org.mule.umo.endpoint.UMOEndpoint; |
20 | |
import org.mule.umo.lifecycle.InitialisationException; |
21 | |
import org.mule.umo.provider.UMOConnector; |
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 | |
|
33 | |
|
34 | |
|
35 | |
|
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 | 0 | protected Object[] methodArguments = null; |
47 | |
|
48 | |
public RmiMessageReceiver(UMOConnector connector, |
49 | |
UMOComponent component, |
50 | |
UMOEndpoint endpoint, |
51 | |
long frequency) throws InitialisationException |
52 | |
{ |
53 | 0 | super(connector, component, endpoint); |
54 | 0 | this.setFrequency(frequency); |
55 | 0 | this.connector = (RmiConnector)connector; |
56 | 0 | } |
57 | |
|
58 | |
protected void doDispose() |
59 | |
{ |
60 | |
|
61 | 0 | } |
62 | |
|
63 | |
protected void doConnect() throws Exception |
64 | |
{ |
65 | 0 | System.setProperty("java.security.policy", connector.getSecurityPolicy()); |
66 | |
|
67 | |
|
68 | 0 | if (System.getSecurityManager() == null) |
69 | |
{ |
70 | 0 | System.setSecurityManager(new RMISecurityManager()); |
71 | |
} |
72 | |
|
73 | 0 | String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(), |
74 | |
MuleProperties.MULE_METHOD_PROPERTY, null); |
75 | |
|
76 | 0 | if (null == methodName) |
77 | |
{ |
78 | 0 | methodName = (String)endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY); |
79 | |
|
80 | 0 | if (null == methodName) |
81 | |
{ |
82 | 0 | throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this); |
83 | |
} |
84 | |
} |
85 | |
|
86 | 0 | remoteObject = connector.getRemoteObject(getEndpoint()); |
87 | |
|
88 | 0 | List args = (List)endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST); |
89 | |
|
90 | 0 | Class[] argTypes = new Class[]{}; |
91 | |
|
92 | 0 | if (args == null) |
93 | |
{ |
94 | 0 | logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST |
95 | |
+ " not set on endpoint, assuming method call has no arguments"); |
96 | 0 | methodArguments = ClassUtils.NO_ARGS; |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | 0 | argTypes = ClassUtils.getClassTypes(methodArguments); |
101 | 0 | methodArguments = new Object[args.size()]; |
102 | 0 | methodArguments = args.toArray(methodArguments); |
103 | |
} |
104 | 0 | invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes); |
105 | 0 | } |
106 | |
|
107 | |
protected void doDisconnect() |
108 | |
{ |
109 | 0 | invokeMethod = null; |
110 | 0 | remoteObject = null; |
111 | 0 | } |
112 | |
|
113 | |
public void poll() |
114 | |
{ |
115 | |
try |
116 | |
{ |
117 | 0 | Object result = invokeMethod.invoke(remoteObject, getMethodArguments()); |
118 | |
|
119 | 0 | if (null != result) |
120 | |
{ |
121 | 0 | final Object payload = connector.getMessageAdapter(result).getPayload(); |
122 | 0 | routeMessage(new MuleMessage(payload), endpoint.isSynchronous()); |
123 | |
} |
124 | |
} |
125 | 0 | catch (Exception e) |
126 | |
{ |
127 | 0 | handleException(e); |
128 | 0 | } |
129 | 0 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
protected Object[] getMethodArguments() |
138 | |
{ |
139 | 0 | return methodArguments; |
140 | |
} |
141 | |
} |