Skip to content

Commit 62f576b

Browse files
committed
Fix new Sonar smells for TcpNioServerConnFactory
1 parent 9428e34 commit 62f576b

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractConnectionFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
* Base class for all connection factories.
5858
*
5959
* @author Gary Russell
60+
* @author Artem Bilan
61+
*
6062
* @since 2.0
6163
*
6264
*/
@@ -479,6 +481,7 @@ public void setSslHandshakeTimeout(int sslHandshakeTimeout) {
479481
* @see #setSslHandshakeTimeout(int)
480482
* @since 4.3.6
481483
*/
484+
@Nullable
482485
protected Integer getSslHandshakeTimeout() {
483486
return this.sslHandshakeTimeout;
484487
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioServerConnectionFactory.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535
import org.springframework.util.Assert;
3636

3737
/**
38-
/**
38+
/**
3939
* Implements a server connection factory that produces {@link TcpNioConnection}s using
4040
* a {@link ServerSocketChannel}. Must have a {@link TcpListener} registered.
4141
*
4242
* @author Gary Russell
4343
* @author Artem Bilan
44+
*
4445
* @since 2.0
4546
*
4647
*/
@@ -52,7 +53,7 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto
5253

5354
private volatile boolean usingDirectBuffers;
5455

55-
private final Map<SocketChannel, TcpNioConnection> channelMap = new HashMap<SocketChannel, TcpNioConnection>();
56+
private final Map<SocketChannel, TcpNioConnection> channelMap = new HashMap<>();
5657

5758
private volatile Selector selector;
5859

@@ -218,7 +219,7 @@ private void doSelect(ServerSocketChannel server, final Selector selectorToSelec
218219
protected void doAccept(final Selector selectorForNewSocket, ServerSocketChannel server, long now) {
219220
logger.debug("New accept");
220221
try {
221-
SocketChannel channel = null;
222+
SocketChannel channel;
222223
do {
223224
channel = server.accept();
224225
if (channel != null) {
@@ -230,44 +231,53 @@ protected void doAccept(final Selector selectorForNewSocket, ServerSocketChannel
230231
}
231232
channel.close();
232233
}
233-
else {
234-
try {
235-
channel.configureBlocking(false);
236-
Socket socket = channel.socket();
237-
setSocketAttributes(socket);
238-
TcpNioConnection connection = createTcpNioConnection(channel);
239-
if (connection == null) {
240-
return;
241-
}
242-
connection.setTaskExecutor(getTaskExecutor());
243-
connection.setLastRead(now);
244-
if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) {
245-
((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout());
246-
}
247-
this.channelMap.put(channel, connection);
248-
channel.register(selectorForNewSocket, SelectionKey.OP_READ, connection);
249-
connection.publishConnectionOpenEvent();
250-
}
251-
catch (IOException e) {
252-
logger.error("Exception accepting new connection from "
253-
+ channel.socket().getInetAddress().getHostAddress()
254-
+ ":" + channel.socket().getPort(), e);
255-
channel.close();
256-
}
234+
else if (createConnectionForAcceptedChannel(selectorForNewSocket, now, channel) == null) {
235+
return;
257236
}
258237
}
259-
} while (this.multiAccept && channel != null);
238+
}
239+
while (this.multiAccept && channel != null);
260240
}
261241
catch (IOException e) {
262242
throw new UncheckedIOException(e);
263243
}
264244
}
265245

246+
@Nullable
247+
private TcpNioConnection createConnectionForAcceptedChannel(Selector selectorForNewSocket, long now,
248+
SocketChannel channel) throws IOException {
249+
250+
TcpNioConnection connection = null;
251+
try {
252+
channel.configureBlocking(false);
253+
Socket socket = channel.socket();
254+
setSocketAttributes(socket);
255+
connection = createTcpNioConnection(channel);
256+
if (connection != null) {
257+
connection.setTaskExecutor(getTaskExecutor());
258+
connection.setLastRead(now);
259+
if (getSslHandshakeTimeout() != null && connection instanceof TcpNioSSLConnection) {
260+
((TcpNioSSLConnection) connection).setHandshakeTimeout(getSslHandshakeTimeout());
261+
}
262+
this.channelMap.put(channel, connection);
263+
channel.register(selectorForNewSocket, SelectionKey.OP_READ, connection);
264+
connection.publishConnectionOpenEvent();
265+
}
266+
}
267+
catch (IOException e) {
268+
logger.error("Exception accepting new connection from "
269+
+ channel.socket().getInetAddress().getHostAddress()
270+
+ ":" + channel.socket().getPort(), e);
271+
channel.close();
272+
}
273+
return connection;
274+
}
275+
266276
@Nullable
267277
private TcpNioConnection createTcpNioConnection(SocketChannel socketChannel) {
268278
try {
269279
TcpNioConnection connection = this.tcpNioConnectionSupport.createNewConnection(socketChannel, true,
270-
isLookupHost(), getApplicationEventPublisher(), getComponentName());
280+
isLookupHost(), getApplicationEventPublisher(), getComponentName());
271281
connection.setUsingDirectBuffers(this.usingDirectBuffers);
272282
TcpConnectionSupport wrappedConnection = wrapConnection(connection);
273283
initializeConnection(wrappedConnection, socketChannel.socket());

0 commit comments

Comments
 (0)