1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.rmi; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.providers.AbstractMessageDispatcher; |
15 | |
import org.mule.umo.UMOEvent; |
16 | |
import org.mule.umo.UMOMessage; |
17 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
18 | |
import org.mule.umo.transformer.TransformerException; |
19 | |
|
20 | |
import java.lang.reflect.Method; |
21 | |
import java.rmi.RMISecurityManager; |
22 | |
import java.rmi.Remote; |
23 | |
import java.util.Collections; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class RmiMessageDispatcher extends AbstractMessageDispatcher |
30 | |
{ |
31 | |
private final RmiConnector connector; |
32 | |
protected volatile Remote remoteObject; |
33 | |
protected volatile Method invokedMethod; |
34 | |
|
35 | |
public RmiMessageDispatcher(UMOImmutableEndpoint endpoint) |
36 | |
{ |
37 | 28 | super(endpoint); |
38 | 28 | this.connector = (RmiConnector)endpoint.getConnector(); |
39 | 28 | } |
40 | |
|
41 | |
protected void doConnect() throws Exception |
42 | |
{ |
43 | 28 | if (remoteObject == null) |
44 | |
{ |
45 | |
|
46 | 28 | String rmiPolicyPath = connector.getSecurityPolicy(); |
47 | 28 | System.setProperty("java.security.policy", rmiPolicyPath); |
48 | |
|
49 | |
|
50 | 28 | if (System.getSecurityManager() == null) |
51 | |
{ |
52 | 0 | System.setSecurityManager(new RMISecurityManager()); |
53 | |
} |
54 | |
|
55 | 28 | remoteObject = connector.getRemoteObject(endpoint); |
56 | |
} |
57 | 28 | } |
58 | |
|
59 | |
protected void doDisconnect() throws Exception |
60 | |
{ |
61 | 28 | remoteObject = null; |
62 | 28 | invokedMethod = null; |
63 | 28 | } |
64 | |
|
65 | |
private Object[] getArgs(UMOEvent event) throws TransformerException |
66 | |
{ |
67 | 20 | Object payload = event.getTransformedMessage(); |
68 | 20 | if (payload instanceof Object[]) |
69 | |
{ |
70 | 0 | return (Object[])payload; |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 20 | return new Object[]{payload}; |
75 | |
} |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
protected void doDispatch(UMOEvent event) throws Exception |
84 | |
{ |
85 | 0 | Object[] arguments = getArgs(event); |
86 | 0 | if (invokedMethod == null) |
87 | |
{ |
88 | 0 | invokedMethod = connector.getMethodObject(remoteObject, event); |
89 | |
} |
90 | 0 | invokedMethod.invoke(remoteObject, arguments); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public UMOMessage doSend(UMOEvent event) throws Exception |
99 | |
{ |
100 | 32 | if (invokedMethod == null) |
101 | |
{ |
102 | 28 | invokedMethod = connector.getMethodObject(remoteObject, event); |
103 | |
} |
104 | |
|
105 | 20 | Object[] arguments = getArgs(event); |
106 | 20 | Object result = invokedMethod.invoke(remoteObject, arguments); |
107 | |
|
108 | 20 | if (result == null) |
109 | |
{ |
110 | 0 | return null; |
111 | |
} |
112 | |
else |
113 | |
{ |
114 | 20 | return new MuleMessage(connector.getMessageAdapter(result).getPayload(), Collections.EMPTY_MAP); |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
protected UMOMessage doReceive(long timeout) throws Exception |
131 | |
{ |
132 | 0 | throw new UnsupportedOperationException("doReceive"); |
133 | |
} |
134 | |
|
135 | |
protected void doDispose() |
136 | |
{ |
137 | |
|
138 | 28 | } |
139 | |
} |