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