1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.rmi; |
8 | |
|
9 | |
import org.mule.api.MuleEvent; |
10 | |
import org.mule.api.MuleException; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.construct.FlowConstruct; |
13 | |
import org.mule.api.endpoint.EndpointURI; |
14 | |
import org.mule.api.endpoint.InboundEndpoint; |
15 | |
import org.mule.api.lifecycle.CreateException; |
16 | |
import org.mule.api.transport.Connector; |
17 | |
import org.mule.transport.AbstractMessageReceiver; |
18 | |
import org.mule.transport.ConnectException; |
19 | |
import org.mule.transport.rmi.i18n.RmiMessages; |
20 | |
import org.mule.util.ClassUtils; |
21 | |
|
22 | |
import java.lang.reflect.Method; |
23 | |
import java.net.InetAddress; |
24 | |
|
25 | |
import javax.naming.Context; |
26 | |
|
27 | |
|
28 | |
|
29 | |
public class RmiCallbackMessageReceiver extends AbstractMessageReceiver |
30 | |
{ |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public static final String PROPERTY_SERVICE_CLASS_NAME = "serviceClassName"; |
36 | |
|
37 | |
protected RmiConnector connector; |
38 | |
|
39 | 0 | protected RmiAble remoteObject = null; |
40 | |
|
41 | 0 | private Context jndi = null; |
42 | |
|
43 | 0 | private String bindName = null; |
44 | |
|
45 | |
private int port; |
46 | |
|
47 | |
|
48 | |
public RmiCallbackMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint) |
49 | |
throws CreateException |
50 | |
{ |
51 | 0 | super(connector, flowConstruct, endpoint); |
52 | 0 | this.connector = (RmiConnector) connector; |
53 | |
|
54 | 0 | logger.debug("Initializing with endpoint " + endpoint); |
55 | |
|
56 | 0 | String rmiPolicyPath = this.connector.getSecurityPolicy(); |
57 | |
|
58 | 0 | System.setProperty("java.security.policy", rmiPolicyPath); |
59 | |
|
60 | 0 | EndpointURI endpointUri = endpoint.getEndpointURI(); |
61 | |
|
62 | 0 | port = endpointUri.getPort(); |
63 | |
|
64 | 0 | if (port < 1) |
65 | |
{ |
66 | 0 | port = RmiConnector.DEFAULT_RMI_muleRegistry_PORT; |
67 | |
} |
68 | |
|
69 | |
|
70 | 0 | logger.debug("Initialized successfully"); |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
protected void doDispose() |
75 | |
{ |
76 | |
|
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
@Override |
87 | |
protected void doConnect() throws ConnectException |
88 | |
{ |
89 | |
try |
90 | |
{ |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | if (null == remoteObject) |
98 | |
{ |
99 | |
try |
100 | |
{ |
101 | 0 | InetAddress inetAddress = InetAddress.getByName(endpoint.getEndpointURI().getHost()); |
102 | |
|
103 | 0 | bindName = endpoint.getEndpointURI().getPath(); |
104 | |
|
105 | 0 | remoteObject = getRmiObject(); |
106 | |
|
107 | 0 | Method theMethod = remoteObject.getClass().getMethod("setReceiver", |
108 | |
new Class[]{RmiCallbackMessageReceiver.class}); |
109 | 0 | theMethod.invoke(remoteObject, new Object[]{this}); |
110 | |
|
111 | 0 | jndi = connector.getJndiContext(inetAddress.getHostAddress() + ":" + port); |
112 | |
|
113 | 0 | jndi.rebind(bindName, remoteObject); |
114 | |
} |
115 | 0 | catch (Exception e) |
116 | |
{ |
117 | 0 | throw new ConnectException(e, this); |
118 | 0 | } |
119 | |
|
120 | |
} |
121 | |
} |
122 | 0 | catch (Exception e) |
123 | |
{ |
124 | 0 | throw new ConnectException(e, this); |
125 | 0 | } |
126 | 0 | } |
127 | |
|
128 | |
|
129 | |
@Override |
130 | |
protected void doDisconnect() |
131 | |
{ |
132 | 0 | logger.debug("Disconnecting..."); |
133 | |
|
134 | |
try |
135 | |
{ |
136 | 0 | jndi.unbind(bindName); |
137 | |
} |
138 | 0 | catch (Exception e) |
139 | |
{ |
140 | 0 | logger.error(e); |
141 | 0 | } |
142 | |
|
143 | 0 | logger.debug("Disconnected successfully."); |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
protected void doStart() throws MuleException |
148 | |
{ |
149 | |
|
150 | 0 | } |
151 | |
|
152 | |
@Override |
153 | |
protected void doStop() throws MuleException |
154 | |
{ |
155 | |
|
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private RmiAble getRmiObject() throws ConnectException |
165 | |
{ |
166 | 0 | String className = (String) endpoint.getProperty(PROPERTY_SERVICE_CLASS_NAME); |
167 | |
|
168 | 0 | if (null == className) |
169 | |
{ |
170 | 0 | throw new ConnectException(RmiMessages.messageReceiverNeedsRmiAble(), this); |
171 | |
} |
172 | |
|
173 | |
RmiAble remote; |
174 | |
|
175 | |
try |
176 | |
{ |
177 | 0 | remote = (RmiAble) ClassUtils.instanciateClass(className, new Object[]{}, this.getClass()); |
178 | |
} |
179 | 0 | catch (Exception e) |
180 | |
{ |
181 | 0 | throw new ConnectException(RmiMessages.serviceClassInvocationFailed(), e, this); |
182 | 0 | } |
183 | |
|
184 | 0 | return (remote); |
185 | |
} |
186 | |
|
187 | |
public MuleEvent routeMessage(Object payload) throws MuleException |
188 | |
{ |
189 | 0 | MuleMessage messageToRoute = createMuleMessage(payload, endpoint.getEncoding()); |
190 | 0 | return routeMessage(messageToRoute); |
191 | |
} |
192 | |
} |