1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.rmi; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.endpoint.OutboundEndpoint; |
12 | |
import org.mule.api.transformer.TransformerException; |
13 | |
import org.mule.transport.AbstractMessageDispatcher; |
14 | |
import org.mule.transport.NullPayload; |
15 | |
|
16 | |
import java.lang.reflect.Method; |
17 | |
import java.rmi.RMISecurityManager; |
18 | |
import java.rmi.Remote; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class RmiMessageDispatcher extends AbstractMessageDispatcher |
25 | |
{ |
26 | |
private final RmiConnector connector; |
27 | |
protected volatile Remote remoteObject; |
28 | |
protected volatile Method invokedMethod; |
29 | |
|
30 | |
public RmiMessageDispatcher(OutboundEndpoint endpoint) |
31 | |
{ |
32 | 0 | super(endpoint); |
33 | 0 | this.connector = (RmiConnector)endpoint.getConnector(); |
34 | 0 | } |
35 | |
|
36 | |
protected void doConnect() throws Exception |
37 | |
{ |
38 | 0 | if (remoteObject == null) |
39 | |
{ |
40 | |
|
41 | 0 | String rmiPolicyPath = connector.getSecurityPolicy(); |
42 | 0 | System.setProperty("java.security.policy", rmiPolicyPath); |
43 | |
|
44 | |
|
45 | 0 | if (System.getSecurityManager() == null) |
46 | |
{ |
47 | 0 | System.setSecurityManager(new RMISecurityManager()); |
48 | |
} |
49 | |
|
50 | 0 | remoteObject = connector.getRemoteObject(endpoint); |
51 | |
} |
52 | 0 | } |
53 | |
|
54 | |
protected void doDisconnect() throws Exception |
55 | |
{ |
56 | 0 | remoteObject = null; |
57 | 0 | invokedMethod = null; |
58 | 0 | } |
59 | |
|
60 | |
private Object[] getArgs(MuleEvent event) throws TransformerException |
61 | |
{ |
62 | 0 | Object payload = event.getMessage().getPayload(); |
63 | 0 | if (payload instanceof Object[]) |
64 | |
{ |
65 | 0 | return (Object[])payload; |
66 | |
} |
67 | |
else |
68 | |
{ |
69 | 0 | return new Object[]{payload}; |
70 | |
} |
71 | |
} |
72 | |
|
73 | |
protected void doDispatch(MuleEvent event) throws Exception |
74 | |
{ |
75 | 0 | Object[] arguments = getArgs(event); |
76 | 0 | if (invokedMethod == null) |
77 | |
{ |
78 | 0 | invokedMethod = connector.getMethodObject(remoteObject, event); |
79 | |
} |
80 | 0 | invokedMethod.invoke(remoteObject, arguments); |
81 | 0 | } |
82 | |
|
83 | |
public MuleMessage doSend(MuleEvent event) throws Exception |
84 | |
{ |
85 | 0 | if (invokedMethod == null) |
86 | |
{ |
87 | 0 | invokedMethod = connector.getMethodObject(remoteObject, event); |
88 | |
} |
89 | |
|
90 | 0 | Object[] arguments = getArgs(event); |
91 | 0 | Object result = invokedMethod.invoke(remoteObject, arguments); |
92 | |
|
93 | 0 | if (result == null) |
94 | |
{ |
95 | 0 | return createMuleMessage(NullPayload.getInstance()); |
96 | |
} |
97 | |
else |
98 | |
{ |
99 | 0 | return createMuleMessage(result); |
100 | |
} |
101 | |
} |
102 | |
|
103 | |
protected void doDispose() |
104 | |
{ |
105 | |
|
106 | 0 | } |
107 | |
} |