From 6284070b45e7fef72d9607781451b88809b39f87 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 1 Nov 2022 16:29:14 -0400 Subject: [PATCH] Some Sonar fixes --- ...bitStreamMessageListenerContainerSpec.java | 13 ++--- .../outbound/CassandraMessageHandler.java | 50 +++++++------------ .../integration/dsl/Pollers.java | 11 ++-- ...WebFluxIntegrationGraphCorsConfigurer.java | 8 +-- .../WebMvcIntegrationGraphCorsConfigurer.java | 8 +-- .../jdbc/lock/DefaultLockRepository.java | 45 +++++++++-------- .../mqtt/core/AbstractMqttClientManager.java | 4 +- ...pringIntegrationTestExecutionListener.java | 2 +- .../metadata/ZookeeperMetadataStore.java | 16 +++--- 9 files changed, 73 insertions(+), 84 deletions(-) diff --git a/spring-integration-amqp/src/main/java/org/springframework/integration/amqp/dsl/RabbitStreamMessageListenerContainerSpec.java b/spring-integration-amqp/src/main/java/org/springframework/integration/amqp/dsl/RabbitStreamMessageListenerContainerSpec.java index dff3de9e19..ca2b2002ab 100644 --- a/spring-integration-amqp/src/main/java/org/springframework/integration/amqp/dsl/RabbitStreamMessageListenerContainerSpec.java +++ b/spring-integration-amqp/src/main/java/org/springframework/integration/amqp/dsl/RabbitStreamMessageListenerContainerSpec.java @@ -32,6 +32,8 @@ import com.rabbitmq.stream.Environment; * Spec for {@link StreamListenerContainer}. * * @author Gary Russell + * @author Artem Bilan + * * @since 6.0 * */ @@ -46,18 +48,9 @@ public class RabbitStreamMessageListenerContainerSpec extends this.target = new StreamListenerContainer(environment, codec); } - /** - * Set the Stream queue name; - * Mutually exclusive with {@link #superStream(String, String)}. - * @return this spec. - */ - public RabbitStreamMessageListenerContainerSpec queueName(String queueName) { - return super.queueName(queueName); - } - /** * Enable Single Active Consumer on a Super Stream. - * Mutually exclusive with {@link #setQueueName(String...)}. + * Mutually exclusive with {@link #queueName(String...)}. * @param superStream the stream. * @param name the consumer name. * @return this spec. diff --git a/spring-integration-cassandra/src/main/java/org/springframework/integration/cassandra/outbound/CassandraMessageHandler.java b/spring-integration-cassandra/src/main/java/org/springframework/integration/cassandra/outbound/CassandraMessageHandler.java index 1cda760693..14325a400a 100644 --- a/spring-integration-cassandra/src/main/java/org/springframework/integration/cassandra/outbound/CassandraMessageHandler.java +++ b/spring-integration-cassandra/src/main/java/org/springframework/integration/cassandra/outbound/CassandraMessageHandler.java @@ -80,7 +80,7 @@ public class CassandraMessageHandler extends AbstractReplyProducingMessageHandle /** * Various options that can be used for Cassandra writes. */ - private WriteOptions writeOptions = WriteOptions.empty(); + private WriteOptions writeOptions; private ReactiveSessionMessageCallback sessionMessageCallback; @@ -95,18 +95,15 @@ public class CassandraMessageHandler extends AbstractReplyProducingMessageHandle Assert.notNull(cassandraOperations, "'cassandraOperations' must not be null."); Assert.notNull(queryType, "'queryType' must not be null."); - this.cassandraOperations = cassandraOperations; + this.cassandraOperations = cassandraOperations; // NOSONAR this.mode = queryType; setAsync(true); - switch (this.mode) { - - case INSERT: - this.writeOptions = InsertOptions.empty(); - break; - case UPDATE: - this.writeOptions = UpdateOptions.empty(); - break; - } + this.writeOptions = + switch (this.mode) { + case INSERT -> InsertOptions.empty(); + case UPDATE -> UpdateOptions.empty(); + case DELETE, STATEMENT -> WriteOptions.empty(); + }; } public void setIngestQuery(String ingestQuery) { @@ -183,7 +180,7 @@ public class CassandraMessageHandler extends AbstractReplyProducingMessageHandle TypeLocator typeLocator = this.evaluationContext.getTypeLocator(); if (typeLocator instanceof StandardTypeLocator) { /* - * Register the Cassandra Query DSL package so they don't need a FQCN for QueryBuilder, for example. + * Register the Cassandra Query DSL package, so they don't need a FQCN for QueryBuilder, for example. */ ((StandardTypeLocator) typeLocator).registerImport(QueryBuilder.class.getPackage().getName()); } @@ -193,28 +190,15 @@ public class CassandraMessageHandler extends AbstractReplyProducingMessageHandle protected Object handleRequestMessage(Message requestMessage) { Object payload = requestMessage.getPayload(); - Mono result = null; + Type modeToUse = payload instanceof Statement ? modeToUse = Type.STATEMENT : this.mode; - Type mode = this.mode; - - if (payload instanceof Statement) { - mode = Type.STATEMENT; - } - - switch (mode) { - case INSERT: - result = handleInsert(payload); - break; - case UPDATE: - result = handleUpdate(payload); - break; - case DELETE: - result = handleDelete(payload); - break; - case STATEMENT: - result = handleStatement(requestMessage); - break; - } + Mono result = + switch (modeToUse) { + case INSERT -> handleInsert(payload); + case UPDATE -> handleUpdate(payload); + case DELETE -> handleDelete(payload); + case STATEMENT -> handleStatement(requestMessage); + }; if (this.producesReply) { return isAsync() ? result : result.block(); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dsl/Pollers.java b/spring-integration-core/src/main/java/org/springframework/integration/dsl/Pollers.java index d9273d82ee..a1e5ffd03a 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dsl/Pollers.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dsl/Pollers.java @@ -21,6 +21,7 @@ import java.time.temporal.ChronoUnit; import java.util.TimeZone; import java.util.concurrent.TimeUnit; +import org.springframework.lang.Nullable; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.PeriodicTrigger; @@ -46,7 +47,7 @@ public final class Pollers { } public static PollerSpec fixedRate(Duration period) { - return fixedRate(period, null); + return periodicTrigger(period, true, null); } /** @@ -75,7 +76,7 @@ public final class Pollers { } public static PollerSpec fixedDelay(Duration period) { - return fixedDelay(period, null); + return periodicTrigger(period, false, null); } public static PollerSpec fixedDelay(long period) { @@ -107,10 +108,12 @@ public final class Pollers { return fixedDelay(Duration.of(period, chronoUnit), Duration.of(initialDelay, chronoUnit)); } - private static PollerSpec periodicTrigger(Duration period, boolean fixedRate, Duration initialDelay) { + private static PollerSpec periodicTrigger(Duration period, boolean fixedRate, @Nullable Duration initialDelay) { PeriodicTrigger periodicTrigger = new PeriodicTrigger(period); periodicTrigger.setFixedRate(fixedRate); - periodicTrigger.setInitialDelay(initialDelay); + if (initialDelay != null) { + periodicTrigger.setInitialDelay(initialDelay); + } return new PollerSpec(periodicTrigger); } diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebFluxIntegrationGraphCorsConfigurer.java b/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebFluxIntegrationGraphCorsConfigurer.java index 66dd006f2e..378359e210 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebFluxIntegrationGraphCorsConfigurer.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebFluxIntegrationGraphCorsConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 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. @@ -16,6 +16,8 @@ package org.springframework.integration.http.config; +import java.util.Arrays; + import org.springframework.web.reactive.config.CorsRegistry; import org.springframework.web.reactive.config.WebFluxConfigurer; @@ -34,9 +36,9 @@ final class WebFluxIntegrationGraphCorsConfigurer implements WebFluxConfigurer { private final String[] allowedOrigins; - WebFluxIntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { // NOSONAR + WebFluxIntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { this.path = path; - this.allowedOrigins = allowedOrigins; + this.allowedOrigins = Arrays.copyOf(allowedOrigins, allowedOrigins.length); } @Override diff --git a/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebMvcIntegrationGraphCorsConfigurer.java b/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebMvcIntegrationGraphCorsConfigurer.java index 3c432ebdd3..c025997486 100644 --- a/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebMvcIntegrationGraphCorsConfigurer.java +++ b/spring-integration-http/src/main/java/org/springframework/integration/http/config/WebMvcIntegrationGraphCorsConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 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. @@ -16,6 +16,8 @@ package org.springframework.integration.http.config; +import java.util.Arrays; + import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -34,9 +36,9 @@ final class WebMvcIntegrationGraphCorsConfigurer implements WebMvcConfigurer { private final String[] allowedOrigins; - WebMvcIntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { // NOSONAR + WebMvcIntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { this.path = path; - this.allowedOrigins = allowedOrigins; + this.allowedOrigins = Arrays.copyOf(allowedOrigins, allowedOrigins.length); } @Override diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java index 1fef10d9e1..e9ce4d885d 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java @@ -225,30 +225,34 @@ public class DefaultLockRepository @Override public boolean acquire(String lock) { - return this.serializableTransactionTemplate.execute( - transactionStatus -> { - if (this.template.update(this.updateQuery, this.id, LocalDateTime.now(ZoneOffset.UTC), - this.region, lock, this.id, - LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)) > 0) { - return true; - } - try { - return this.template.update(this.insertQuery, this.region, lock, this.id, - LocalDateTime.now(ZoneOffset.UTC)) > 0; - } - catch (DataIntegrityViolationException ex) { - return false; - } - }); + Boolean result = + this.serializableTransactionTemplate.execute( + transactionStatus -> { + if (this.template.update(this.updateQuery, this.id, LocalDateTime.now(ZoneOffset.UTC), + this.region, lock, this.id, + LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)) > 0) { + return true; + } + try { + return this.template.update(this.insertQuery, this.region, lock, this.id, + LocalDateTime.now(ZoneOffset.UTC)) > 0; + } + catch (DataIntegrityViolationException ex) { + return false; + } + }); + return Boolean.TRUE.equals(result); } @Override public boolean isAcquired(String lock) { - return this.readOnlyTransactionTemplate.execute( + final Boolean result = this.readOnlyTransactionTemplate.execute( transactionStatus -> - this.template.queryForObject(this.countQuery, // NOSONAR query never returns null - Integer.class, this.region, lock, this.id, - LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)) == 1); + Integer.valueOf(1).equals( + this.template.queryForObject(this.countQuery, + Integer.class, this.region, lock, this.id, + LocalDateTime.now(ZoneOffset.UTC).minus(this.ttl, ChronoUnit.MILLIS)))); + return Boolean.TRUE.equals(result); } @Override @@ -261,10 +265,11 @@ public class DefaultLockRepository @Override public boolean renew(String lock) { - return this.defaultTransactionTemplate.execute( + final Boolean result = this.defaultTransactionTemplate.execute( transactionStatus -> this.template.update(this.renewQuery, LocalDateTime.now(ZoneOffset.UTC), this.region, lock, this.id) > 0); + return Boolean.TRUE.equals(result); } } diff --git a/spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/core/AbstractMqttClientManager.java b/spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/core/AbstractMqttClientManager.java index 1a7c759387..d57ceeb182 100644 --- a/spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/core/AbstractMqttClientManager.java +++ b/spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/core/AbstractMqttClientManager.java @@ -61,7 +61,7 @@ public abstract class AbstractMqttClientManager implements ClientManager implements ClientManager { if (this.updateMap.containsKey(key)) { @@ -312,7 +312,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif String value = IntegrationUtils.bytesToString(data.getData(), ZookeeperMetadataStore.this.encoding); switch (type) { - case NODE_CREATED: + case NODE_CREATED -> { if (ZookeeperMetadataStore.this.updateMap.containsKey(eventKey) && data.getStat().getVersion() >= ZookeeperMetadataStore.this.updateMap.get(eventKey).version()) { @@ -320,8 +320,8 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif ZookeeperMetadataStore.this.updateMap.remove(eventPath); } ZookeeperMetadataStore.this.listeners.forEach((listener) -> listener.onAdd(eventKey, value)); - break; - case NODE_CHANGED: + } + case NODE_CHANGED -> { if (ZookeeperMetadataStore.this.updateMap.containsKey(eventKey) && data.getStat().getVersion() >= ZookeeperMetadataStore.this.updateMap.get(eventKey).version()) { @@ -329,11 +329,11 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif ZookeeperMetadataStore.this.updateMap.remove(eventPath); } ZookeeperMetadataStore.this.listeners.forEach((listener) -> listener.onUpdate(eventKey, value)); - break; - case NODE_DELETED: + } + case NODE_DELETED -> { ZookeeperMetadataStore.this.updateMap.remove(eventKey); ZookeeperMetadataStore.this.listeners.forEach((listener) -> listener.onRemove(eventKey, value)); - break; + } } }