From 5624feacc044ab32182978616e8bebc58c8711de Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 27 Sep 2011 11:56:17 -0400 Subject: [PATCH] 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. --- .../TcpNetClientConnectionFactory.java | 16 +++++++++- .../TcpNetServerConnectionFactory.java | 29 +++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetClientConnectionFactory.java index 5374492332..d4f714ef02 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetClientConnectionFactory.java @@ -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() { } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java index 38b3a9e971..b3ae401739 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java @@ -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; }