From 88d2bebb9cc035be349ee264e68ac51025b35365 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 3 Feb 2021 10:54:30 -0500 Subject: [PATCH] Fix new Sonar smells --- .../AbstractRemoteFileOutboundGateway.java | 18 +++-- .../integration/jms/PollableJmsChannel.java | 67 +++++++++++-------- .../RetrievingJpaOutboundGatewayParser.java | 22 +++--- 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java index e39a2b0fb7..82d3c00e0d 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java @@ -975,13 +975,8 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply boolean recursion, F file) throws IOException { String fileName = getFilename(file); - String fileSeparator = this.remoteFileTemplate.getRemoteFileSeparator(); final boolean isDirectory = isDirectory(file); - boolean isDots = - ".".equals(fileName) - || "..".equals(fileName) - || fileName.endsWith(fileSeparator + ".") - || fileName.endsWith(fileSeparator + ".."); + boolean isDots = hasDots(fileName); if ((this.options.contains(Option.SUBDIRS) || !isDirectory) && (!isDots || this.options.contains(Option.ALL))) { @@ -993,10 +988,19 @@ public abstract class AbstractRemoteFileOutboundGateway extends AbstractReply } if (recursion && isDirectory && !isDots) { - lsFiles.addAll(listFilesInRemoteDir(session, directory, subDirectory + fileName + fileSeparator)); + lsFiles.addAll(listFilesInRemoteDir(session, directory, subDirectory + fileName + + this.remoteFileTemplate.getRemoteFileSeparator())); } } + private boolean hasDots(String fileName) { + String fileSeparator = this.remoteFileTemplate.getRemoteFileSeparator(); + return ".".equals(fileName) + || "..".equals(fileName) + || fileName.endsWith(fileSeparator + ".") + || fileName.endsWith(fileSeparator + ".."); + } + protected final List filterMputFiles(File[] files) { if (files == null) { return Collections.emptyList(); diff --git a/spring-integration-jms/src/main/java/org/springframework/integration/jms/PollableJmsChannel.java b/spring-integration-jms/src/main/java/org/springframework/integration/jms/PollableJmsChannel.java index a3cca1fde5..b0a7362d63 100644 --- a/spring-integration-jms/src/main/java/org/springframework/integration/jms/PollableJmsChannel.java +++ b/spring-integration-jms/src/main/java/org/springframework/integration/jms/PollableJmsChannel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 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. @@ -74,8 +74,8 @@ public class PollableJmsChannel extends AbstractJmsChannel Deque interceptorStack = null; boolean counted = false; try { - if (isLoggingEnabled() && logger.isTraceEnabled()) { - logger.trace("preReceive on channel '" + this + "'"); + if (isLoggingEnabled()) { + logger.trace(() -> "preReceive on channel '" + this + "'"); } if (interceptorList.getInterceptors().size() > 0) { interceptorStack = new ArrayDeque<>(); @@ -84,35 +84,13 @@ public class PollableJmsChannel extends AbstractJmsChannel return null; } } - Object object; - if (this.messageSelector == null) { - object = getJmsTemplate().receiveAndConvert(); - } - else { - object = getJmsTemplate().receiveSelectedAndConvert(this.messageSelector); - } - Message message = null; - if (object == null) { - if (isLoggingEnabled() && logger.isTraceEnabled()) { - logger.trace("postReceive on channel '" + this + "', message is null"); - } - } - else { + Message message = receiveAndConvertToMessage(); + if (message != null) { incrementReceiveCounter(); counted = true; - if (object instanceof Message) { - message = (Message) object; - } - else { - message = getMessageBuilderFactory() - .withPayload(object) - .build(); - } - if (isLoggingEnabled() && logger.isDebugEnabled()) { - logger.debug("postReceive on channel '" + this + "', message: " + message); - } } + if (interceptorStack != null && message != null) { message = interceptorList.postReceive(message, this); } @@ -128,6 +106,39 @@ public class PollableJmsChannel extends AbstractJmsChannel } } + @Nullable + private Message receiveAndConvertToMessage() { + Object object; + if (this.messageSelector == null) { + object = getJmsTemplate().receiveAndConvert(); + } + else { + object = getJmsTemplate().receiveSelectedAndConvert(this.messageSelector); + } + + if (object == null) { + if (isLoggingEnabled()) { + logger.trace(() -> "postReceive on channel '" + this + "', message is null"); + } + return null; + } + else { + Message message; + if (object instanceof Message) { + message = (Message) object; + } + else { + message = getMessageBuilderFactory() + .withPayload(object) + .build(); + } + if (isLoggingEnabled()) { + logger.debug(() -> "postReceive on channel '" + this + "', message: " + message); + } + return message; + } + } + private void incrementReceiveCounter() { MetricsCaptor metricsCaptor = getMetricsCaptor(); if (metricsCaptor != null) { diff --git a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/config/xml/RetrievingJpaOutboundGatewayParser.java b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/config/xml/RetrievingJpaOutboundGatewayParser.java index 484975db98..b4433ab20a 100644 --- a/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/config/xml/RetrievingJpaOutboundGatewayParser.java +++ b/spring-integration-jpa/src/main/java/org/springframework/integration/jpa/config/xml/RetrievingJpaOutboundGatewayParser.java @@ -16,6 +16,9 @@ package org.springframework.integration.jpa.config.xml; +import java.util.Arrays; +import java.util.stream.Collectors; + import org.w3c.dom.Element; import org.springframework.beans.factory.config.BeanDefinition; @@ -87,21 +90,18 @@ public class RetrievingJpaOutboundGatewayParser extends AbstractJpaOutboundGatew String[] otherAttributes = { "jpa-query", "native-query", "named-query", "first-result", "first-result-expression", "max-results", "max-results-expression", "delete-in-batch", "expect-single-result", "parameter-source-factory", "use-payload-as-parameter-source" }; - StringBuilder others = new StringBuilder(); - for (String otherAttribute : otherAttributes) { - if (gatewayElement.hasAttribute(otherAttribute) && - StringUtils.hasText(gatewayElement.getAttribute(otherAttribute))) { - if (others.length() > 0) { - others.append(", "); - } - others.append(otherAttribute); - } - } + + String others = + Arrays.stream(otherAttributes) + .filter((attr) -> gatewayElement.hasAttribute(attr) && + StringUtils.hasText(gatewayElement.getAttribute(attr))) + .collect(Collectors.joining(",")); + boolean childElementsExist = !CollectionUtils.isEmpty(DomUtils.getChildElementsByTagName(gatewayElement, "parameter")); if (others.length() > 0 || childElementsExist) { parserContext.getReaderContext().error( - (others.length() == 0 ? "" : "'" + others.toString() + "' " + (others.length() == 0 ? "" : "'" + others + "' " + (childElementsExist ? "and " : "")) + (childElementsExist ? "child elements " : "") + "not allowed with an 'id-expression' attribute.",