1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.tcp; |
8 | |
|
9 | |
import org.mule.util.StringUtils; |
10 | |
|
11 | |
import java.io.IOException; |
12 | |
import java.net.InetAddress; |
13 | |
import java.net.InetSocketAddress; |
14 | |
import java.net.ServerSocket; |
15 | |
import java.net.URI; |
16 | |
|
17 | 0 | public class TcpServerSocketFactory implements SimpleServerSocketFactory |
18 | |
{ |
19 | |
|
20 | |
public ServerSocket createServerSocket(URI uri, int backlog, Boolean reuse) throws IOException |
21 | |
{ |
22 | 0 | String host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost"); |
23 | 0 | InetAddress inetAddress = InetAddress.getByName(host); |
24 | |
|
25 | 0 | if (inetAddress.equals(InetAddress.getLocalHost()) |
26 | |
|| inetAddress.isLoopbackAddress() |
27 | |
|| host.trim().equals("localhost")) |
28 | |
{ |
29 | 0 | return createServerSocket(uri.getPort(), backlog, reuse); |
30 | |
} |
31 | |
else |
32 | |
{ |
33 | 0 | return createServerSocket(inetAddress, uri.getPort(), backlog, reuse); |
34 | |
} |
35 | |
} |
36 | |
|
37 | |
public ServerSocket createServerSocket(InetAddress address, int port, int backlog, Boolean reuse) throws IOException |
38 | |
{ |
39 | 0 | return configure(new ServerSocket(), reuse, new InetSocketAddress(address, port), backlog); |
40 | |
} |
41 | |
|
42 | |
public ServerSocket createServerSocket(int port, int backlog, Boolean reuse) throws IOException |
43 | |
{ |
44 | 0 | return configure(new ServerSocket(), reuse, new InetSocketAddress(port), backlog); |
45 | |
} |
46 | |
|
47 | |
protected ServerSocket configure(ServerSocket socket, Boolean reuse, InetSocketAddress address, int backlog) |
48 | |
throws IOException |
49 | |
{ |
50 | 0 | if (null != reuse && reuse.booleanValue() != socket.getReuseAddress()) |
51 | |
{ |
52 | 0 | socket.setReuseAddress(reuse.booleanValue()); |
53 | |
} |
54 | |
|
55 | 0 | socket.bind(address, backlog); |
56 | 0 | return socket; |
57 | |
} |
58 | |
|
59 | |
} |