1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.tcp; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.model.streaming.CallbackOutputStream; |
15 | |
import org.mule.providers.AbstractConnector; |
16 | |
import org.mule.providers.tcp.i18n.TcpMessages; |
17 | |
import org.mule.providers.tcp.protocols.DefaultProtocol; |
18 | |
import org.mule.umo.MessagingException; |
19 | |
import org.mule.umo.UMOException; |
20 | |
import org.mule.umo.UMOMessage; |
21 | |
import org.mule.umo.endpoint.UMOImmutableEndpoint; |
22 | |
import org.mule.umo.lifecycle.InitialisationException; |
23 | |
import org.mule.util.ClassUtils; |
24 | |
|
25 | |
import java.io.BufferedOutputStream; |
26 | |
import java.io.DataOutputStream; |
27 | |
import java.io.IOException; |
28 | |
import java.io.OutputStream; |
29 | |
import java.net.ServerSocket; |
30 | |
import java.net.Socket; |
31 | |
import java.net.URI; |
32 | |
|
33 | |
import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class TcpConnector extends AbstractConnector |
42 | |
{ |
43 | |
|
44 | |
|
45 | |
|
46 | |
public static final String KEEP_SEND_SOCKET_OPEN_PROPERTY = "keepSendSocketOpen"; |
47 | |
public static final int DEFAULT_SOCKET_TIMEOUT = INT_VALUE_NOT_SET; |
48 | |
public static final int DEFAULT_BUFFER_SIZE = INT_VALUE_NOT_SET; |
49 | |
public static final int DEFAULT_BACKLOG = INT_VALUE_NOT_SET; |
50 | |
|
51 | 0 | private int sendTimeout = DEFAULT_SOCKET_TIMEOUT; |
52 | 0 | private int receiveTimeout = DEFAULT_SOCKET_TIMEOUT; |
53 | 0 | private int sendBufferSize = DEFAULT_BUFFER_SIZE; |
54 | 0 | private int receiveBufferSize = DEFAULT_BUFFER_SIZE; |
55 | 0 | private int receiveBacklog = DEFAULT_BACKLOG; |
56 | |
private boolean sendTcpNoDelay; |
57 | 0 | private boolean validateConnections = true; |
58 | 0 | private Boolean reuseAddress = null; |
59 | 0 | private int socketLinger = INT_VALUE_NOT_SET; |
60 | |
private String tcpProtocolClassName; |
61 | |
private TcpProtocol tcpProtocol; |
62 | 0 | private boolean keepSendSocketOpen = false; |
63 | 0 | private boolean keepAlive = false; |
64 | |
private PooledSocketFactory socketFactory; |
65 | |
private SimpleServerSocketFactory serverSocketFactory; |
66 | 0 | private GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool(); |
67 | |
|
68 | |
public TcpConnector() |
69 | 0 | { |
70 | 0 | setSocketFactory (new TcpSocketFactory()); |
71 | 0 | setServerSocketFactory(new TcpServerSocketFactory()); |
72 | 0 | setTcpProtocolClassName(DefaultProtocol.class.getName()); |
73 | 0 | } |
74 | |
|
75 | |
protected void doInitialise() throws InitialisationException |
76 | |
{ |
77 | 0 | if (tcpProtocol == null) |
78 | |
{ |
79 | |
try |
80 | |
{ |
81 | 0 | tcpProtocol = (TcpProtocol) ClassUtils.instanciateClass(tcpProtocolClassName, null); |
82 | |
} |
83 | 0 | catch (Exception e) |
84 | |
{ |
85 | 0 | throw new InitialisationException(TcpMessages.failedToInitMessageReader(), e); |
86 | 0 | } |
87 | |
} |
88 | |
|
89 | 0 | dispatcherSocketsPool.setFactory(getSocketFactory()); |
90 | 0 | dispatcherSocketsPool.setTestOnBorrow(true); |
91 | 0 | dispatcherSocketsPool.setTestOnReturn(true); |
92 | |
|
93 | 0 | dispatcherSocketsPool.setMaxActive(1); |
94 | 0 | } |
95 | |
|
96 | |
protected void doDispose() |
97 | |
{ |
98 | 0 | logger.debug("Closing TCP connector"); |
99 | |
try |
100 | |
{ |
101 | 0 | dispatcherSocketsPool.close(); |
102 | |
} |
103 | 0 | catch (Exception e) |
104 | |
{ |
105 | 0 | logger.warn("Failed to close dispatcher socket pool: " + e.getMessage()); |
106 | 0 | } |
107 | 0 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected Socket getSocket(UMOImmutableEndpoint endpoint) throws Exception |
114 | |
{ |
115 | 0 | Socket socket = (Socket) dispatcherSocketsPool.borrowObject(endpoint); |
116 | 0 | if (logger.isDebugEnabled()) |
117 | |
{ |
118 | 0 | logger.debug("borrowing socket; debt " + dispatcherSocketsPool.getNumActive()); |
119 | |
} |
120 | 0 | return socket; |
121 | |
} |
122 | |
|
123 | |
void releaseSocket(Socket socket, UMOImmutableEndpoint endpoint) throws Exception |
124 | |
{ |
125 | 0 | dispatcherSocketsPool.returnObject(endpoint, socket); |
126 | 0 | if (logger.isDebugEnabled()) |
127 | |
{ |
128 | 0 | logger.debug("returned socket; debt " + dispatcherSocketsPool.getNumActive()); |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
public OutputStream getOutputStream(final UMOImmutableEndpoint endpoint, UMOMessage message) |
133 | |
throws UMOException |
134 | |
{ |
135 | |
final Socket socket; |
136 | |
try |
137 | |
{ |
138 | 0 | socket = getSocket(endpoint); |
139 | |
} |
140 | 0 | catch (Exception e) |
141 | |
{ |
142 | 0 | throw new MessagingException(CoreMessages.failedToGetOutputStream(), message, e); |
143 | 0 | } |
144 | 0 | if (socket == null) |
145 | |
{ |
146 | |
|
147 | 0 | throw new IllegalStateException("could not get socket for endpoint: " |
148 | |
+ endpoint.getEndpointURI().getAddress()); |
149 | |
} |
150 | |
try |
151 | |
{ |
152 | 0 | return new CallbackOutputStream( |
153 | |
new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())), |
154 | |
new CallbackOutputStream.Callback() |
155 | |
{ |
156 | 0 | public void onClose() throws Exception |
157 | |
{ |
158 | 0 | releaseSocket(socket, endpoint); |
159 | 0 | } |
160 | |
}); |
161 | |
} |
162 | 0 | catch (IOException e) |
163 | |
{ |
164 | 0 | throw new MessagingException(CoreMessages.failedToGetOutputStream(), message, e); |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
protected void doConnect() throws Exception |
169 | |
{ |
170 | |
|
171 | 0 | } |
172 | |
|
173 | |
protected void doDisconnect() throws Exception |
174 | |
{ |
175 | 0 | dispatcherSocketsPool.clear(); |
176 | 0 | } |
177 | |
|
178 | |
protected void doStart() throws UMOException |
179 | |
{ |
180 | |
|
181 | 0 | } |
182 | |
|
183 | |
protected void doStop() throws UMOException |
184 | |
{ |
185 | |
|
186 | 0 | } |
187 | |
|
188 | |
public String getProtocol() |
189 | |
{ |
190 | 0 | return "tcp"; |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
public boolean isKeepSendSocketOpen() |
196 | |
{ |
197 | 0 | return keepSendSocketOpen; |
198 | |
} |
199 | |
|
200 | |
public void setKeepSendSocketOpen(boolean keepSendSocketOpen) |
201 | |
{ |
202 | 0 | this.keepSendSocketOpen = keepSendSocketOpen; |
203 | 0 | } |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void setTimeout(int timeout) |
210 | |
{ |
211 | 0 | setSendTimeout(timeout); |
212 | 0 | setReceiveTimeout(timeout); |
213 | 0 | } |
214 | |
|
215 | |
public int getSendTimeout() |
216 | |
{ |
217 | 0 | return this.sendTimeout; |
218 | |
} |
219 | |
|
220 | |
public void setSendTimeout(int timeout) |
221 | |
{ |
222 | 0 | this.sendTimeout = valueOrDefault(timeout, 0, DEFAULT_SOCKET_TIMEOUT); |
223 | 0 | } |
224 | |
|
225 | |
public int getReceiveTimeout() |
226 | |
{ |
227 | 0 | return receiveTimeout; |
228 | |
} |
229 | |
|
230 | |
public void setReceiveTimeout(int timeout) |
231 | |
{ |
232 | 0 | this.receiveTimeout = valueOrDefault(timeout, 0, DEFAULT_SOCKET_TIMEOUT); |
233 | 0 | } |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public int getBufferSize() |
239 | |
{ |
240 | 0 | return sendBufferSize; |
241 | |
} |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public void setBufferSize(int bufferSize) |
247 | |
{ |
248 | 0 | sendBufferSize = valueOrDefault(bufferSize, 1, DEFAULT_BUFFER_SIZE); |
249 | 0 | } |
250 | |
|
251 | |
public int getSendBufferSize() |
252 | |
{ |
253 | 0 | return sendBufferSize; |
254 | |
} |
255 | |
|
256 | |
public void setSendBufferSize(int bufferSize) |
257 | |
{ |
258 | 0 | sendBufferSize = valueOrDefault(bufferSize, 1, DEFAULT_BUFFER_SIZE); |
259 | 0 | } |
260 | |
|
261 | |
public int getReceiveBufferSize() |
262 | |
{ |
263 | 0 | return receiveBufferSize; |
264 | |
} |
265 | |
|
266 | |
public void setReceiveBufferSize(int bufferSize) |
267 | |
{ |
268 | 0 | receiveBufferSize = valueOrDefault(bufferSize, 1, DEFAULT_BUFFER_SIZE); |
269 | 0 | } |
270 | |
|
271 | |
public int getReceiveBacklog() |
272 | |
{ |
273 | 0 | return receiveBacklog; |
274 | |
} |
275 | |
|
276 | |
public void setReceiveBacklog(int receiveBacklog) |
277 | |
{ |
278 | 0 | this.receiveBacklog = valueOrDefault(receiveBacklog, 0, DEFAULT_BACKLOG); |
279 | 0 | } |
280 | |
|
281 | |
public int getSendSocketLinger() |
282 | |
{ |
283 | 0 | return socketLinger; |
284 | |
} |
285 | |
|
286 | |
public void setSendSocketLinger(int soLinger) |
287 | |
{ |
288 | 0 | this.socketLinger = valueOrDefault(soLinger, 0, INT_VALUE_NOT_SET); |
289 | 0 | } |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
public int getBacklog() |
297 | |
{ |
298 | 0 | return receiveBacklog; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
public void setBacklog(int backlog) |
307 | |
{ |
308 | 0 | this.receiveBacklog = backlog; |
309 | 0 | } |
310 | |
|
311 | |
public TcpProtocol getTcpProtocol() |
312 | |
{ |
313 | 0 | return tcpProtocol; |
314 | |
} |
315 | |
|
316 | |
public void setTcpProtocol(TcpProtocol tcpProtocol) |
317 | |
{ |
318 | 0 | this.tcpProtocol = tcpProtocol; |
319 | 0 | } |
320 | |
|
321 | |
public String getTcpProtocolClassName() |
322 | |
{ |
323 | 0 | return tcpProtocolClassName; |
324 | |
} |
325 | |
|
326 | |
public void setTcpProtocolClassName(String protocolClassName) |
327 | |
{ |
328 | 0 | this.tcpProtocolClassName = protocolClassName; |
329 | 0 | } |
330 | |
|
331 | |
public boolean isRemoteSyncEnabled() |
332 | |
{ |
333 | 0 | return true; |
334 | |
} |
335 | |
|
336 | |
public boolean isKeepAlive() |
337 | |
{ |
338 | 0 | return keepAlive; |
339 | |
} |
340 | |
|
341 | |
public void setKeepAlive(boolean keepAlive) |
342 | |
{ |
343 | 0 | this.keepAlive = keepAlive; |
344 | 0 | } |
345 | |
|
346 | |
public boolean isSendTcpNoDelay() |
347 | |
{ |
348 | 0 | return sendTcpNoDelay; |
349 | |
} |
350 | |
|
351 | |
public void setSendTcpNoDelay(boolean sendTcpNoDelay) |
352 | |
{ |
353 | 0 | this.sendTcpNoDelay = sendTcpNoDelay; |
354 | 0 | } |
355 | |
|
356 | |
protected void setSocketFactory(PooledSocketFactory socketFactory) |
357 | |
{ |
358 | 0 | this.socketFactory = socketFactory; |
359 | 0 | } |
360 | |
|
361 | |
protected PooledSocketFactory getSocketFactory() |
362 | |
{ |
363 | 0 | return socketFactory; |
364 | |
} |
365 | |
|
366 | |
public SimpleServerSocketFactory getServerSocketFactory() |
367 | |
{ |
368 | 0 | return serverSocketFactory; |
369 | |
} |
370 | |
|
371 | |
public void setServerSocketFactory(SimpleServerSocketFactory serverSocketFactory) |
372 | |
{ |
373 | 0 | this.serverSocketFactory = serverSocketFactory; |
374 | 0 | } |
375 | |
|
376 | |
protected ServerSocket getServerSocket(URI uri) throws IOException |
377 | |
{ |
378 | 0 | return getServerSocketFactory().createServerSocket(uri, getReceiveBacklog(), isReuseAddress()); |
379 | |
} |
380 | |
|
381 | |
private static int valueOrDefault(int value, int threshhold, int deflt) |
382 | |
{ |
383 | 0 | if (value < threshhold) |
384 | |
{ |
385 | 0 | return deflt; |
386 | |
} |
387 | |
else |
388 | |
{ |
389 | 0 | return value; |
390 | |
} |
391 | |
} |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
public boolean isValidateConnections() { |
399 | 0 | return validateConnections; |
400 | |
} |
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
public void setValidateConnections(boolean validateConnections) { |
407 | 0 | this.validateConnections = validateConnections; |
408 | 0 | } |
409 | |
|
410 | |
|
411 | |
|
412 | |
|
413 | |
public Boolean isReuseAddress() |
414 | |
{ |
415 | 0 | return reuseAddress; |
416 | |
} |
417 | |
|
418 | |
|
419 | |
|
420 | |
|
421 | |
|
422 | |
|
423 | |
public void setReuseAddress(Boolean reuseAddress) |
424 | |
{ |
425 | 0 | this.reuseAddress = reuseAddress; |
426 | 0 | } |
427 | |
|
428 | |
} |