Fix new Sonar smells

This commit is contained in:
Artem Bilan
2020-03-02 14:02:58 -05:00
parent e2b63bed7c
commit 8bc221b271
4 changed files with 49 additions and 38 deletions

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.
@@ -27,8 +27,10 @@ import org.springframework.lang.Nullable;
/**
* Abstract class for client connection factories; client connection factories
* establish outgoing connections.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*
*/
@@ -90,7 +92,7 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
}
protected TcpConnectionSupport obtainConnection() throws InterruptedException {
if (!this.isSingleUse()) {
if (!isSingleUse()) {
TcpConnectionSupport connection = obtainSharedConnection();
if (connection != null) {
return connection;
@@ -103,7 +105,7 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
protected final TcpConnectionSupport obtainSharedConnection() throws InterruptedException {
this.theConnectionLock.readLock().lockInterruptibly();
try {
TcpConnectionSupport connection = this.getTheConnection();
TcpConnectionSupport connection = getTheConnection();
if (connection != null && connection.isOpen()) {
return connection;
}
@@ -115,7 +117,7 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
}
protected final TcpConnectionSupport obtainNewConnection() throws InterruptedException {
boolean singleUse = this.isSingleUse();
boolean singleUse = isSingleUse();
if (!singleUse) {
this.theConnectionLock.writeLock().lockInterruptibly();
}
@@ -123,19 +125,19 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
TcpConnectionSupport connection;
if (!singleUse) {
// Another write lock holder might have created a new one by now.
connection = this.obtainSharedConnection();
connection = obtainSharedConnection();
if (connection != null) {
return connection;
}
}
if (logger.isDebugEnabled()) {
logger.debug("Opening new socket connection to " + this.getHost() + ":" + this.getPort());
logger.debug("Opening new socket connection to " + getHost() + ":" + getPort());
}
connection = buildNewConnection();
if (!singleUse) {
this.setTheConnection(connection);
setTheConnection(connection);
}
connection.publishConnectionOpenEvent();
return connection;
@@ -155,7 +157,8 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
}
protected TcpConnectionSupport buildNewConnection() {
throw new UnsupportedOperationException("Factories that don't override this class' obtainConnection() must implement this method");
throw new UnsupportedOperationException(
"Factories that don't override this class' obtainConnection() must implement this method");
}
/**
@@ -172,18 +175,18 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
connection.enableManualListenerRegistration();
}
else {
TcpListener listener = this.getListener();
TcpListener listener = getListener();
if (listener != null) {
connection.registerListener(listener);
}
}
TcpSender sender = this.getSender();
TcpSender sender = getSender();
if (sender != null) {
connection.registerSender(sender);
}
connection.setMapper(this.getMapper());
connection.setDeserializer(this.getDeserializer());
connection.setSerializer(this.getSerializer());
connection.setMapper(getMapper());
connection.setDeserializer(getDeserializer());
connection.setSerializer(getSerializer());
}
/**
@@ -196,6 +199,7 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection
/**
* @return the theConnection
*/
@Nullable
protected TcpConnectionSupport getTheConnection() {
return this.theConnection;
}

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.
@@ -75,9 +75,9 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
protected final Object lifecycleMonitor = new Object(); // NOSONAR final
private final Map<String, TcpConnectionSupport> connections = new ConcurrentHashMap<String, TcpConnectionSupport>();
private final Map<String, TcpConnectionSupport> connections = new ConcurrentHashMap<>();
private final BlockingQueue<PendingIO> delayedReads = new LinkedBlockingQueue<AbstractConnectionFactory.PendingIO>();
private final BlockingQueue<PendingIO> delayedReads = new LinkedBlockingQueue<>();
private String host;
@@ -154,6 +154,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
}
}
@Nullable
public ApplicationEventPublisher getApplicationEventPublisher() {
return this.applicationEventPublisher;
}
@@ -371,8 +372,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
* @param senderToRegister The sender
*/
public void registerSender(TcpSender senderToRegister) {
Assert.isNull(this.sender, this.getClass().getName() +
" may only be used by one outbound adapter");
Assert.isNull(this.sender, this.getClass().getName() + " may only be used by one outbound adapter");
this.sender = senderToRegister;
}
@@ -512,7 +512,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
protected void onInit() {
super.onInit();
if (!this.mapperSet) {
this.mapper.setBeanFactory(this.getBeanFactory());
this.mapper.setBeanFactory(getBeanFactory());
}
}
@@ -617,7 +617,6 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
* Times out any expired connections then, if {@code selectionCount > 0},
* processes the selected keys.
* Removes closed connections from the connections field, and from the connections parameter.
*
* @param selectionCount Number of IO Events, if 0 we were probably woken up by a close.
* @param selector The selector.
* @param server The server socket channel.
@@ -668,7 +667,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
}
}
}
this.harvestClosedConnections();
harvestClosedConnections();
if (logger.isTraceEnabled()) {
if (this.host == null) {
logger.trace("Port " + this.port + " SelectionCount: " + selectionCount);
@@ -783,11 +782,13 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
pendingRead.key.interestOps(SelectionKey.OP_READ);
wakeSelector = true;
if (logger.isDebugEnabled()) {
logger.debug("Rescheduling delayed read for " + ((TcpNioConnection) pendingRead.key.attachment()).getConnectionId());
logger.debug("Rescheduling delayed read for " +
((TcpNioConnection) pendingRead.key.attachment()).getConnectionId());
}
}
else {
((TcpNioConnection) pendingRead.key.attachment()).sendExceptionToListener(new EOFException("Connection is closed"));
((TcpNioConnection) pendingRead.key.attachment())
.sendExceptionToListener(new EOFException("Connection is closed"));
}
}
else {
@@ -834,7 +835,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
*/
private List<String> removeClosedConnectionsAndReturnOpenConnectionIds() {
synchronized (this.connections) {
List<String> openConnectionIds = new ArrayList<String>();
List<String> openConnectionIds = new ArrayList<>();
Iterator<Entry<String, TcpConnectionSupport>> iterator = this.connections.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, TcpConnectionSupport> entry = iterator.next();
@@ -842,7 +843,8 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
if (!connection.isOpen()) {
iterator.remove();
if (logger.isDebugEnabled()) {
logger.debug(getComponentName() + ": Removed closed connection: " + connection.getConnectionId());
logger.debug(getComponentName() + ": Removed closed connection: " +
connection.getConnectionId());
}
}
else {
@@ -860,7 +862,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
* Cleans up this.connections by removing any closed connections.
*/
protected void harvestClosedConnections() {
this.removeClosedConnectionsAndReturnOpenConnectionIds();
removeClosedConnectionsAndReturnOpenConnectionIds();
}
@Override
@@ -883,7 +885,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
}
protected void checkActive() {
if (!this.isActive()) {
if (!isActive()) {
throw new UncheckedIOException(new IOException(this + " connection factory has not been started"));
}
}
@@ -903,7 +905,7 @@ public abstract class AbstractConnectionFactory extends IntegrationObjectSupport
* @return the list of connection ids.
*/
public List<String> getOpenConnectionIds() {
return Collections.unmodifiableList(this.removeClosedConnectionsAndReturnOpenConnectionIds());
return Collections.unmodifiableList(removeClosedConnectionsAndReturnOpenConnectionIds());
}
/**

View File

@@ -62,8 +62,9 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
super("", 0);
Assert.notEmpty(factories, "At least one factory is required");
this.factories = new ArrayList<>(factories);
this.cachingDelegates = factories.stream()
.anyMatch(factory -> factory instanceof CachingClientConnectionFactory);
this.cachingDelegates =
factories.stream()
.anyMatch(factory -> factory instanceof CachingClientConnectionFactory);
}
/**
@@ -105,8 +106,8 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
protected void onInit() {
super.onInit();
for (AbstractClientConnectionFactory factory : this.factories) {
Assert.state(!(this.isSingleUse() ^ factory.isSingleUse()),
"Inconsistent singleUse - delegate factories must match this one");
Assert.state(isSingleUse() == factory.isSingleUse(),
"Inconsistent singleUse - delegate factories must match this one");
factory.enableManualListenerRegistration();
}
}
@@ -144,7 +145,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
}
}
@Override
@Override // NOSONAR
protected TcpConnectionSupport obtainConnection() throws InterruptedException {
FailoverTcpConnection sharedConnection = (FailoverTcpConnection) getTheConnection();
boolean shared = !isSingleUse() && !this.cachingDelegates;
@@ -200,7 +201,7 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
public boolean isRunning() {
boolean isRunning = true;
for (AbstractClientConnectionFactory factory : this.factories) {
isRunning = !isRunning ? false : factory.isRunning();
isRunning = isRunning && factory.isRunning();
}
return isRunning;
}
@@ -429,9 +430,10 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
@Override
public boolean onMessage(Message<?> message) {
if (this.delegate.getConnectionId().equals(message.getHeaders().get(IpHeaders.CONNECTION_ID))) {
AbstractIntegrationMessageBuilder<?> messageBuilder = FailoverClientConnectionFactory.this
.getMessageBuilderFactory().fromMessage(message)
.setHeader(IpHeaders.CONNECTION_ID, this.getConnectionId());
AbstractIntegrationMessageBuilder<?> messageBuilder =
getMessageBuilderFactory()
.fromMessage(message)
.setHeader(IpHeaders.CONNECTION_ID, this.getConnectionId());
if (message.getHeaders().get(IpHeaders.ACTUAL_CONNECTION_ID) == null) {
messageBuilder.setHeader(IpHeaders.ACTUAL_CONNECTION_ID,
message.getHeaders().get(IpHeaders.CONNECTION_ID));
@@ -456,4 +458,5 @@ public class FailoverClientConnectionFactory extends AbstractClientConnectionFac
}
}
}

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.
@@ -30,6 +30,7 @@ import org.springframework.core.serializer.Deserializer;
import org.springframework.core.serializer.Serializer;
import org.springframework.expression.Expression;
import org.springframework.integration.support.MessageBuilderFactory;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.DestinationResolver;
@@ -119,6 +120,7 @@ public class ThreadAffinityClientConnectionFactory extends AbstractClientConnect
}
@Override
@Nullable
public ApplicationEventPublisher getApplicationEventPublisher() {
return this.connectionFactory.getApplicationEventPublisher();
}