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