From fc4907d329d04a257ec21be788d13415104eedec Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 3 Mar 2020 10:25:09 -0500 Subject: [PATCH] Fix new Sonar smells --- .../AbstractServerConnectionFactory.java | 23 +++++++------ .../FailoverClientConnectionFactory.java | 7 ++-- .../TcpNetClientConnectionFactory.java | 32 ++++++++++--------- .../connection/TcpNetConnectionSupport.java | 8 +++-- .../TcpNioClientConnectionFactory.java | 4 +-- .../connection/TcpNioConnectionSupport.java | 7 ++-- .../TcpNioServerConnectionFactory.java | 4 +-- 7 files changed, 50 insertions(+), 35 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractServerConnectionFactory.java index 708dfa8d97..44d7ca8979 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractServerConnectionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2001-2019 the original author or authors. + * Copyright 2001-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,8 @@ import org.springframework.util.Assert; * for each new connection. * * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 */ public abstract class AbstractServerConnectionFactory extends AbstractConnectionFactory @@ -43,12 +45,12 @@ public abstract class AbstractServerConnectionFactory extends AbstractConnection private static final int DEFAULT_BACKLOG = 5; + private int backlog = DEFAULT_BACKLOG; + + private String localAddress; + private volatile boolean listening; - private volatile String localAddress; - - private volatile int backlog = DEFAULT_BACKLOG; - private volatile boolean shuttingDown; @@ -161,7 +163,7 @@ public abstract class AbstractServerConnectionFactory extends AbstractConnection /** * Used on multi-homed systems to enforce the server to listen - * on a specfic network address instead of all network adapters. + * on a specific network address instead of all network adapters. * @param localAddress the ip address of the required adapter. */ public void setLocalAddress(String localAddress) { @@ -200,8 +202,9 @@ public abstract class AbstractServerConnectionFactory extends AbstractConnection } protected void publishServerExceptionEvent(Exception e) { - if (getApplicationEventPublisher() != null) { - getApplicationEventPublisher().publishEvent(new TcpConnectionServerExceptionEvent(this, e)); + ApplicationEventPublisher applicationEventPublisher = getApplicationEventPublisher(); + if (applicationEventPublisher != null) { + applicationEventPublisher.publishEvent(new TcpConnectionServerExceptionEvent(this, e)); } } @@ -209,10 +212,10 @@ public abstract class AbstractServerConnectionFactory extends AbstractConnection final ApplicationEventPublisher eventPublisher = getApplicationEventPublisher(); if (eventPublisher != null) { final TcpConnectionServerListeningEvent event = new TcpConnectionServerListeningEvent(this, port); - TaskScheduler taskScheduler = this.getTaskScheduler(); + TaskScheduler taskScheduler = getTaskScheduler(); if (taskScheduler != null) { try { - taskScheduler.schedule((Runnable) () -> eventPublisher.publishEvent(event), new Date()); + taskScheduler.schedule(() -> eventPublisher.publishEvent(event), new Date()); } catch (@SuppressWarnings("unused") TaskRejectedException e) { eventPublisher.publishEvent(event); diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java index 389dc16337..9e699e6946 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java @@ -28,6 +28,7 @@ import org.springframework.core.serializer.Deserializer; import org.springframework.core.serializer.Serializer; import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.support.AbstractIntegrationMessageBuilder; +import org.springframework.lang.Nullable; import org.springframework.messaging.Message; import org.springframework.util.Assert; @@ -147,7 +148,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac } } - @Override // NOSONAR + @Override protected TcpConnectionSupport obtainConnection() throws InterruptedException { FailoverTcpConnection sharedConnection = (FailoverTcpConnection) getTheConnection(); boolean shared = !isSingleUse() && !this.cachingDelegates; @@ -170,7 +171,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac return failoverTcpConnection; } - private void closeRefreshedIfNecessary(FailoverTcpConnection sharedConnection, boolean refreshShared, + private void closeRefreshedIfNecessary(@Nullable FailoverTcpConnection sharedConnection, boolean refreshShared, FailoverTcpConnection failoverTcpConnection) { this.creationTime = System.currentTimeMillis(); @@ -178,8 +179,10 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac * We may have simply wrapped the same connection in a new wrapper; don't close. */ if (refreshShared && this.closeOnRefresh + && sharedConnection != null && !sharedConnection.delegate.equals(failoverTcpConnection.delegate) && sharedConnection.isOpen()) { + sharedConnection.close(); } } 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 eaf8976d3a..125b1461a2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,10 @@ import org.springframework.util.Assert; /** * A client connection factory that creates {@link TcpNetConnection}s. + * * @author Gary Russell + * @author Artem Bilan + * * @since 2.0 * */ @@ -48,9 +51,10 @@ public class TcpNetClientConnectionFactory extends @Override protected TcpConnectionSupport buildNewConnection() { try { - Socket socket = createSocket(this.getHost(), this.getPort()); + Socket socket = createSocket(getHost(), getPort()); setSocketAttributes(socket); - TcpConnectionSupport connection = this.tcpNetConnectionSupport.createNewConnection(socket, false, isLookupHost(), + TcpConnectionSupport connection = + this.tcpNetConnectionSupport.createNewConnection(socket, false, isLookupHost(), getApplicationEventPublisher(), getComponentName()); connection = wrapConnection(connection); initializeConnection(connection, socket); @@ -73,16 +77,24 @@ public class TcpNetClientConnectionFactory extends this.tcpNetConnectionSupport = connectionSupport; } + public void setTcpSocketFactorySupport(TcpSocketFactorySupport tcpSocketFactorySupport) { + Assert.notNull(tcpSocketFactorySupport, "TcpSocketFactorySupport may not be null"); + this.tcpSocketFactorySupport = tcpSocketFactorySupport; + } + + protected TcpSocketFactorySupport getTcpSocketFactorySupport() { + return this.tcpSocketFactorySupport; + } + @Override public void start() { - this.setActive(true); + setActive(true); super.start(); } /** * Create a new {@link Socket}. This default implementation uses the default * {@link javax.net.SocketFactory}. Override to use some other mechanism - * * @param host The host. * @param port The port. * @return The Socket @@ -94,14 +106,4 @@ public class TcpNetClientConnectionFactory extends return socket; } - protected TcpSocketFactorySupport getTcpSocketFactorySupport() { - return this.tcpSocketFactorySupport; - } - - public void setTcpSocketFactorySupport( - TcpSocketFactorySupport tcpSocketFactorySupport) { - Assert.notNull(tcpSocketFactorySupport, "TcpSocketFactorySupport may not be null"); - this.tcpSocketFactorySupport = tcpSocketFactorySupport; - } - } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnectionSupport.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnectionSupport.java index 66d6b6b756..e8fe59fd44 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnectionSupport.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnectionSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,15 @@ package org.springframework.integration.ip.tcp.connection; import java.net.Socket; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.lang.Nullable; /** * Used by NET connection factories to instantiate a {@link TcpNetConnection} object. + * * @author Gary Russell + * @author Artem Bilan + * * @since 5.0 * */ @@ -44,7 +48,7 @@ public interface TcpNetConnectionSupport { */ TcpNetConnection createNewConnection(Socket socket, boolean server, boolean lookupHost, - ApplicationEventPublisher applicationEventPublisher, + @Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName); } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java index 03d7e42365..fbcc3c20d3 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -201,7 +201,7 @@ public class TcpNioClientConnectionFactory extends int soTimeout = getSoTimeout(); int selectionCount = 0; try { - long timeout = soTimeout < 0 ? 0 : soTimeout; + long timeout = Math.max(soTimeout, 0); if (getDelayedReads().size() > 0 && (timeout == 0 || getReadDelay() < timeout)) { timeout = getReadDelay(); } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionSupport.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionSupport.java index 1e75cd61ea..51cb432a6b 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionSupport.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,12 +19,15 @@ package org.springframework.integration.ip.tcp.connection; import java.nio.channels.SocketChannel; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.lang.Nullable; /** * Used by NIO connection factories to instantiate a {@link TcpNioConnection} object. * Implementations for SSL and non-SSL {@link TcpNioConnection}s are provided. + * * @author Gary Russell + * * @since 2.2 * */ @@ -45,7 +48,7 @@ public interface TcpNioConnectionSupport { */ TcpNioConnection createNewConnection(SocketChannel socketChannel, boolean server, boolean lookupHost, - ApplicationEventPublisher applicationEventPublisher, + @Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName); } 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 f85caf7794..2bfce00b6e 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -187,7 +187,7 @@ public class TcpNioServerConnectionFactory extends AbstractServerConnectionFacto int soTimeout = getSoTimeout(); int selectionCount = 0; try { - long timeout = soTimeout < 0 ? 0 : soTimeout; + long timeout = Math.max(soTimeout, 0); if (getDelayedReads().size() > 0 && (timeout == 0 || getReadDelay() < timeout)) { timeout = getReadDelay(); }