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