From 1934d408c15c76ba016dc527f941d21c30eabb8a Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 7 Oct 2015 10:29:50 -0400 Subject: [PATCH] INT-3843: Backport NPE Fix for TCP ConFactories JIRA: https://jira.spring.io/browse/INT-3843 **Cherry-pick to the 3.0.x** Conflicts: spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java Resolved. --- .../TcpNetServerConnectionFactory.java | 10 +++++++++- .../TcpNioServerConnectionFactory.java | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) 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 0504f2f693..17da270f05 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 @@ -80,7 +80,15 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto * Not fatal. */ try { - socket = serverSocket.accept(); + if (this.serverSocket == null) { + if (logger.isDebugEnabled()) { + logger.debug(this + " stopped before accept"); + } + throw new IOException(this + " stopped before accept"); + } + else { + socket = this.serverSocket.accept(); + } } catch (SocketTimeoutException ste) { if (logger.isDebugEnabled()) { 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 782df53890..6ddf419c20 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 @@ -92,11 +92,17 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto Math.abs(this.getBacklog())); } final Selector selector = Selector.open(); - this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); - this.setListening(true); - this.selector = selector; - doSelect(this.serverChannel, selector); - + if (this.serverChannel == null) { + if (logger.isDebugEnabled()) { + logger.debug(this + " stopped before registering the server channel"); + } + } + else { + this.serverChannel.register(selector, SelectionKey.OP_ACCEPT); + setListening(true); + this.selector = selector; + doSelect(this.serverChannel, selector); + } } catch (IOException e) { this.close();