Skip to content

Commit

Permalink
Fix new Sonar smells
Browse files Browse the repository at this point in the history
  • Loading branch information
artembilan committed Mar 4, 2020
1 parent 0efcf24 commit e71e250
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,19 +22,24 @@
import java.net.Socket;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;


/**
* Default implementation of {@link TcpNetConnectionSupport}.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.0
*
*/
public class DefaultTcpNetConnectionSupport extends AbstractTcpConnectionSupport implements TcpNetConnectionSupport {

@Override
public TcpNetConnection createNewConnection(Socket socket, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {

if (isPushbackCapable()) {
return new PushBackTcpNetConnection(socket, server, lookupHost, applicationEventPublisher,
connectionFactoryName, getPushbackBufferSize());
Expand All @@ -55,7 +60,9 @@ private static final class PushBackTcpNetConnection extends TcpNetConnection {
private volatile InputStream wrapped;

PushBackTcpNetConnection(Socket socket, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName, int bufferSize) {
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName,
int bufferSize) {

super(socket, server, lookupHost, applicationEventPublisher, connectionFactoryName);
this.pushbackBufferSize = bufferSize;
this.connectionId = "pushback:" + super.getConnectionId();
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,7 +38,8 @@ public class DefaultTcpNioConnectionSupport extends AbstractTcpConnectionSupport

@Override
public TcpNioConnection createNewConnection(SocketChannel socketChannel, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {

if (isPushbackCapable()) {
return new PushBackTcpNioConnection(socketChannel, server, lookupHost, applicationEventPublisher,
connectionFactoryName, getPushbackBufferSize());
Expand All @@ -60,7 +61,7 @@ private static final class PushBackTcpNioConnection extends TcpNioConnection {
private volatile InputStream wrapped;

PushBackTcpNioConnection(SocketChannel socketChannel, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, @Nullable String connectionFactoryName,
@Nullable ApplicationEventPublisher applicationEventPublisher, @Nullable String connectionFactoryName,
int bufferSize) {

super(socketChannel, server, lookupHost, applicationEventPublisher, connectionFactoryName);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,6 +27,7 @@
import javax.net.ssl.SSLParameters;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -75,7 +76,7 @@ public DefaultTcpNioSSLConnectionSupport(TcpSSLContextSupport sslContextSupport,
*/
@Override
public TcpNioConnection createNewConnection(SocketChannel socketChannel, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {

SSLEngine sslEngine = this.sslContext.createSSLEngine();
postProcessSSLEngine(sslEngine);
Expand Down Expand Up @@ -119,7 +120,8 @@ private static final class PushBackTcpNioSSLConnection extends TcpNioSSLConnecti
private volatile InputStream wrapped;

PushBackTcpNioSSLConnection(SocketChannel socketChannel, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName, SSLEngine sslEngine,
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName,
SSLEngine sslEngine,
int bufferSize) {

super(socketChannel, server, lookupHost, applicationEventPublisher, connectionFactoryName, sslEngine);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -115,7 +115,7 @@ public TcpConnectionSupport(@Nullable ApplicationEventPublisher applicationEvent
* during event publishing, may be null, in which case "unknown" will be used.
*/
public TcpConnectionSupport(Socket socket, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher,
@Nullable ApplicationEventPublisher applicationEventPublisher,
@Nullable String connectionFactoryName) {

this.socketInfo = new SocketInfo(socket);
Expand Down Expand Up @@ -359,21 +359,15 @@ protected final void sendExceptionToListener(Exception e) {
}

protected void publishConnectionOpenEvent() {
TcpConnectionEvent event = new TcpConnectionOpenEvent(this,
getConnectionFactoryName());
doPublish(event);
doPublish(new TcpConnectionOpenEvent(this, getConnectionFactoryName()));
}

protected void publishConnectionCloseEvent() {
TcpConnectionEvent event = new TcpConnectionCloseEvent(this,
getConnectionFactoryName());
doPublish(event);
doPublish(new TcpConnectionCloseEvent(this, getConnectionFactoryName()));
}

protected void publishConnectionExceptionEvent(Throwable t) {
TcpConnectionEvent event = new TcpConnectionExceptionEvent(this,
getConnectionFactoryName(), t);
doPublish(event);
doPublish(new TcpConnectionExceptionEvent(this, getConnectionFactoryName(), t));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -42,6 +42,7 @@
* A TcpConnection that uses and underlying {@link Socket}.
*
* @author Gary Russell
*
* @since 2.0
*
*/
Expand All @@ -68,7 +69,8 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling
* during event publishing, may be null, in which case "unknown" will be used.
*/
public TcpNetConnection(Socket socket, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {
@Nullable ApplicationEventPublisher applicationEventPublisher, String connectionFactoryName) {

super(socket, server, lookupHost, applicationEventPublisher, connectionFactoryName);
this.socket = socket;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -53,6 +53,8 @@
*
* @author Gary Russell
* @author John Anderson
* @author Artem Bilan
*
* @since 2.0
*
*/
Expand Down Expand Up @@ -106,7 +108,7 @@ public class TcpNioConnection extends TcpConnectionSupport {
* @param connectionFactoryName The name of the connection factory creating this connection.
*/
public TcpNioConnection(SocketChannel socketChannel, boolean server, boolean lookupHost,
ApplicationEventPublisher applicationEventPublisher,
@Nullable ApplicationEventPublisher applicationEventPublisher,
@Nullable String connectionFactoryName) {

super(socketChannel.socket(), server, lookupHost, applicationEventPublisher, connectionFactoryName);
Expand Down Expand Up @@ -272,8 +274,7 @@ public void run() {
}
catch (Exception e) {
if (logger.isTraceEnabled()) {
logger.error("Read exception " +
getConnectionId(), e);
logger.error("Read exception " + getConnectionId(), e);
}
else if (!isNoReadErrorOnClose()) {
logger.error("Read exception " +
Expand Down Expand Up @@ -386,13 +387,11 @@ private synchronized Message<?> convert() throws IOException {

private void sendToChannel(Message<?> message) {
try {
if (message != null) {
TcpListener listener = getListener();
if (listener == null) {
throw new NoListenerException("No listener");
}
listener.onMessage(message);
TcpListener listener = getListener();
if (listener == null) {
throw new NoListenerException("No listener");
}
listener.onMessage(message);
}
catch (Exception e) {
if (e instanceof NoListenerException) { // could also be thrown by an interceptor
Expand Down Expand Up @@ -425,19 +424,19 @@ private void doRead() throws IOException {
checkForAssembler();

if (logger.isTraceEnabled()) {
logger.trace("Before read:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
logger.trace("Before read: " + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
}
int len = this.socketChannel.read(this.rawBuffer);
if (len < 0) {
this.writingToPipe = false;
closeConnection(true);
}
if (logger.isTraceEnabled()) {
logger.trace("After read:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
logger.trace("After read: " + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
}
this.rawBuffer.flip();
if (logger.isTraceEnabled()) {
logger.trace("After flip:" + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
logger.trace("After flip: " + this.rawBuffer.position() + "/" + this.rawBuffer.limit());
}
if (logger.isDebugEnabled()) {
logger.debug("Read " + this.rawBuffer.limit() + " into raw buffer");
Expand Down Expand Up @@ -506,9 +505,7 @@ public void readPacket() {
closeConnection(true);
}
catch (Exception e) {
logger.error("Exception on Read " +
getConnectionId() + " " +
e.getMessage(), e);
logger.error("Exception on Read " + getConnectionId() + " " + e.getMessage(), e);
closeConnection(true);
}
}
Expand All @@ -535,8 +532,7 @@ public void setTaskExecutor(Executor taskExecutor) {
}

/**
* If true, connection will attempt to use direct buffers where
* possible.
* If true, connection will attempt to use direct buffers where possible.
* @param usingDirectBuffers the usingDirectBuffers to set.
*/
public void setUsingDirectBuffers(boolean usingDirectBuffers) {
Expand Down

0 comments on commit e71e250

Please sign in to comment.