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