Fix new Sonar smells

This commit is contained in:
Artem Bilan
2020-03-03 10:25:09 -05:00
parent e6e9f45164
commit fc4907d329
7 changed files with 50 additions and 35 deletions

View File

@@ -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);

View File

@@ -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();
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -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();
}