1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
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(Connector connector, |
49 | |
FlowConstruct flowConstruct, |
50 | |
InboundEndpoint endpoint, |
51 | |
long frequency) throws CreateException |
52 | |
{ |
53 | 0 | super(connector, flowConstruct, endpoint); |
54 | 0 | this.setFrequency(frequency); |
55 | 0 | this.connector = (RmiConnector) connector; |
56 | 0 | } |
57 | |
|
58 | |
@Override |
59 | |
protected void doDispose() |
60 | |
{ |
61 | |
|
62 | 0 | } |
63 | |
|
64 | |
@SuppressWarnings("unchecked") |
65 | |
@Override |
66 | |
protected void doConnect() throws Exception |
67 | |
{ |
68 | 0 | System.setProperty("java.security.policy", connector.getSecurityPolicy()); |
69 | |
|
70 | |
|
71 | 0 | if (System.getSecurityManager() == null) |
72 | |
{ |
73 | 0 | System.setSecurityManager(new RMISecurityManager()); |
74 | |
} |
75 | |
|
76 | |
|
77 | 0 | String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(), |
78 | |
MuleProperties.MULE_METHOD_PROPERTY, null); |
79 | 0 | if (null == methodName) |
80 | |
{ |
81 | 0 | methodName = (String) endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY); |
82 | |
|
83 | 0 | if (null == methodName) |
84 | |
{ |
85 | 0 | throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this); |
86 | |
} |
87 | |
} |
88 | |
|
89 | |
|
90 | 0 | remoteObject = connector.getRemoteObject(getEndpoint()); |
91 | |
|
92 | |
|
93 | 0 | List<Object> args = (List<Object>) endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST); |
94 | 0 | Class[] argTypes = new Class[]{}; |
95 | 0 | if (args == null) |
96 | |
{ |
97 | 0 | logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST |
98 | |
+ " not set on endpoint, assuming method call has no arguments"); |
99 | 0 | methodArguments = ClassUtils.NO_ARGS; |
100 | |
} |
101 | |
else |
102 | |
{ |
103 | 0 | argTypes = connector.getArgTypes(endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES), RequestContext.getEvent()); |
104 | 0 | methodArguments = new Object[args.size()]; |
105 | 0 | methodArguments = args.toArray(methodArguments); |
106 | |
} |
107 | |
|
108 | |
|
109 | 0 | invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes); |
110 | 0 | } |
111 | |
|
112 | |
@Override |
113 | |
protected void doDisconnect() |
114 | |
{ |
115 | 0 | invokeMethod = null; |
116 | 0 | remoteObject = null; |
117 | 0 | } |
118 | |
|
119 | |
public void poll() |
120 | |
{ |
121 | |
try |
122 | |
{ |
123 | 0 | Object result = invokeMethod.invoke(remoteObject, getMethodArguments()); |
124 | |
|
125 | 0 | if (null != result) |
126 | |
{ |
127 | 0 | routeMessage(createMuleMessage(result)); |
128 | |
} |
129 | |
} |
130 | 0 | catch (Exception e) |
131 | |
{ |
132 | 0 | getConnector().getMuleContext().getExceptionListener().handleException(e); |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
protected Object[] getMethodArguments() |
141 | |
{ |
142 | 0 | return methodArguments; |
143 | |
} |
144 | |
} |