INT-2154 Minor Refactoring

Factor out socket creation to a protected method, to facilitate
easier subclassing, enabling something other than the default
Socket factories to be used.
This commit is contained in:
Gary Russell
2011-09-27 11:56:17 -04:00
parent 3079fe9bf0
commit 5624feacc0
2 changed files with 39 additions and 6 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.integration.ip.tcp.connection;
import java.io.IOException;
import java.net.Socket;
import javax.net.SocketFactory;
@@ -48,7 +49,7 @@ public class TcpNetClientConnectionFactory extends
return this.theConnection;
}
logger.debug("Opening new socket connection to " + this.host + ":" + this.port);
Socket socket = SocketFactory.getDefault().createSocket(this.host, this.port);
Socket socket = createSocket(this.host, this.port);
setSocketAttributes(socket);
TcpConnection connection = new TcpNetConnection(socket, false, this.isLookupHost());
connection = wrapConnection(connection);
@@ -61,6 +62,19 @@ public class TcpNetClientConnectionFactory extends
return connection;
}
/**
* Create a new {@link Socket}. This default implementation uses the default
* {@link SocketFactory}. Override to use some other mechanism
*
* @param host The host.
* @param port The port.
* @return The Socket
* @throws IOException
*/
protected Socket createSocket(String host, int port) throws IOException {
return SocketFactory.getDefault().createSocket(host, port);
}
public void close() {
}

View File

@@ -58,14 +58,12 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto
}
try {
if (this.localAddress == null) {
this.serverSocket = ServerSocketFactory.getDefault()
.createServerSocket(this.port, Math.abs(this.poolSize));
theServerSocket = createServerSocket(this.port, this.poolSize, null);
} else {
InetAddress whichNic = InetAddress.getByName(this.localAddress);
this.serverSocket = ServerSocketFactory.getDefault()
.createServerSocket(port, Math.abs(poolSize), whichNic);
theServerSocket = createServerSocket(this.port, this.poolSize, whichNic);
}
theServerSocket = this.serverSocket;
this.serverSocket = theServerSocket;
this.listening = true;
logger.info("Listening on port " + this.port);
while (true) {
@@ -90,6 +88,27 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto
}
}
/**
* Create a new {@link ServerSocket}. This default implementation uses the default
* {@link ServerSocketFactory}. Override to use some other mechanism
*
* @param port The port.
* @param backlog The server socket backlog.
* @param whichNic An InetAddress if binding to a specific network interface. Set to
* null when configured to bind to all interfaces.
* @return The Server Socket.
* @throws IOException
*/
protected ServerSocket createServerSocket(int port, int backlog, InetAddress whichNic) throws IOException {
if (whichNic == null) {
return ServerSocketFactory.getDefault().createServerSocket(port,
Math.abs(poolSize));
} else {
return ServerSocketFactory.getDefault().createServerSocket(port,
Math.abs(poolSize), whichNic);
}
}
public boolean isRunning() {
return this.active;
}