From 62f576b64f8e9cd52514ce74ec59350583265125 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 29 Mar 2019 11:46:01 -0400 Subject: [PATCH] Fix new Sonar smells for TcpNioServerConnFactory --- .../connection/AbstractConnectionFactory.java | 3 + .../TcpNioServerConnectionFactory.java | 68 +++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractConnectionFactory.java index 2f8c2884c4..2f8937bb2e 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractConnectionFactory.java @@ -57,6 +57,8 @@ import org.springframework.util.Assert; * Base class for all connection factories. * * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 * */ @@ -479,6 +481,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport * @see #setSslHandshakeTimeout(int) * @since 4.3.6 */ + @Nullable protected Integer getSslHandshakeTimeout() { return this.sslHandshakeTimeout; } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java index e2d9e9410c..e9be671de1 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java @@ -35,12 +35,13 @@ import org.springframework.lang.Nullable; import org.springframework.util.Assert; /** -/** + /** * Implements a server connection factory that produces {@link TcpNioConnection}s using * a {@link ServerSocketChannel}. Must have a {@link TcpListener} registered. * * @author Gary Russell * @author Artem Bilan + * * @since 2.0 * */ @@ -52,7 +53,7 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto private volatile boolean usingDirectBuffers; - private final Map channelMap = new HashMap(); + private final Map channelMap = new HashMap<>(); private volatile Selector selector; @@ -218,7 +219,7 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto protected void doAccept(final Selector selectorForNewSocket, ServerSocketChannel server, long now) { logger.debug("New accept"); try { - SocketChannel channel = null; + SocketChannel channel; do { channel = server.accept(); if (channel != null) { @@ -230,44 +231,53 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto } channel.close(); } - else { - try { - channel.configureBlocking(false); - Socket socket = channel.socket(); - setSocketAttributes(socket); - TcpNioConnection connection = createTcpNioConnection(channel); - if (connection == null) { - return; - } - connection.setTaskExecutor(getTaskExecutor()); - connection.setLastRead(now); - if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) { - ((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout()); - } - this.channelMap.put(channel, connection); - channel.register(selectorForNewSocket, SelectionKey.OP_READ, connection); - connection.publishConnectionOpenEvent(); - } - catch (IOException e) { - logger.error("Exception accepting new connection from " - + channel.socket().getInetAddress().getHostAddress() - + ":" + channel.socket().getPort(), e); - channel.close(); - } + else if (createConnectionForAcceptedChannel(selectorForNewSocket, now, channel) == null) { + return; } } - } while (this.multiAccept && channel != null); + } + while (this.multiAccept && channel != null); } catch (IOException e) { throw new UncheckedIOException(e); } } + @Nullable + private TcpNioConnection createConnectionForAcceptedChannel(Selector selectorForNewSocket, long now, + SocketChannel channel) throws IOException { + + TcpNioConnection connection = null; + try { + channel.configureBlocking(false); + Socket socket = channel.socket(); + setSocketAttributes(socket); + connection = createTcpNioConnection(channel); + if (connection != null) { + connection.setTaskExecutor(getTaskExecutor()); + connection.setLastRead(now); + if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) { + ((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout()); + } + this.channelMap.put(channel, connection); + channel.register(selectorForNewSocket, SelectionKey.OP_READ, connection); + connection.publishConnectionOpenEvent(); + } + } + catch (IOException e) { + logger.error("Exception accepting new connection from " + + channel.socket().getInetAddress().getHostAddress() + + ":" + channel.socket().getPort(), e); + channel.close(); + } + return connection; + } + @Nullable private TcpNioConnection createTcpNioConnection(SocketChannel socketChannel) { try { TcpNioConnection connection = this.tcpNioConnectionSupport.createNewConnection(socketChannel, true, - isLookupHost(), getApplicationEventPublisher(), getComponentName()); + isLookupHost(), getApplicationEventPublisher(), getComponentName()); connection.setUsingDirectBuffers(this.usingDirectBuffers); TcpConnectionSupport wrappedConnection = wrapConnection(connection); initializeConnection(wrappedConnection, socketChannel.socket());