Skip to content

Commit 6d936d9

Browse files
committed
Fix some Sonar smells
1 parent 236ded9 commit 6d936d9

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/dsl/AmqpOutboundChannelAdapterSpec.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
package org.springframework.integration.amqp.dsl;
1818

1919
import org.springframework.amqp.core.AmqpTemplate;
20-
import org.springframework.messaging.Message;
2120

2221
/**
2322
* Spec for an outbound AMQP channel adapter
2423
*
2524
* @author Gary Russell
25+
* @author Artme Bilan
26+
*
2627
* @since 5.3
2728
*
2829
*/
@@ -33,8 +34,8 @@ protected AmqpOutboundChannelAdapterSpec(AmqpTemplate amqpTemplate) {
3334
}
3435

3536
/**
36-
* If true, and the message payload is an {@link Iterable} of {@link Message}, send the
37-
* messages in a single invocation of the template (same channel) and optionally
37+
* If true, and the message payload is an {@link Iterable} of {@link org.springframework.messaging.Message},
38+
* send the messages in a single invocation of the template (same channel) and optionally
3839
* wait for the confirms or die.
3940
* @param multiSend true to send multiple messages.
4041
* @return the spec.

spring-integration-core/src/main/java/org/springframework/integration/endpoint/AbstractPollingEndpoint.java

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,6 +67,11 @@
6767
*/
6868
public abstract class AbstractPollingEndpoint extends AbstractEndpoint implements BeanClassLoaderAware {
6969

70+
/**
71+
* A default polling period for {@link PeriodicTrigger}.
72+
*/
73+
public static final long DEFAULT_POLLING_PERIOD = 10;
74+
7075
private final Object initializationMonitor = new Object();
7176

7277
private Executor taskExecutor = new SyncTaskExecutor();
@@ -75,7 +80,7 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
7580

7681
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
7782

78-
private Trigger trigger = new PeriodicTrigger(10);
83+
private Trigger trigger = new PeriodicTrigger(DEFAULT_POLLING_PERIOD);
7984

8085
private long maxMessagesPerPoll = -1;
8186

@@ -117,7 +122,7 @@ protected boolean isSyncExecutor() {
117122
}
118123

119124
public void setTrigger(Trigger trigger) {
120-
this.trigger = (trigger != null ? trigger : new PeriodicTrigger(10));
125+
this.trigger = (trigger != null ? trigger : new PeriodicTrigger(DEFAULT_POLLING_PERIOD));
121126
}
122127

123128
public void setAdviceChain(List<Advice> adviceChain) {
@@ -193,19 +198,16 @@ protected void onInit() {
193198
return;
194199
}
195200
Assert.notNull(this.trigger, "Trigger is required");
196-
if (this.taskExecutor != null) {
197-
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
198-
if (this.errorHandler == null) {
199-
this.errorHandler = ChannelUtils.getErrorHandler(getBeanFactory());
200-
this.errorHandlerIsDefault = true;
201-
}
202-
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
201+
if (this.taskExecutor != null && !(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
202+
if (this.errorHandler == null) {
203+
this.errorHandler = ChannelUtils.getErrorHandler(getBeanFactory());
204+
this.errorHandlerIsDefault = true;
203205
}
206+
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
204207
}
205-
if (this.transactionSynchronizationFactory == null && this.adviceChain != null) {
206-
if (this.adviceChain.stream().anyMatch(TransactionInterceptor.class::isInstance)) {
207-
this.transactionSynchronizationFactory = new PassThroughTransactionSynchronizationFactory();
208-
}
208+
if (this.transactionSynchronizationFactory == null && this.adviceChain != null &&
209+
this.adviceChain.stream().anyMatch(TransactionInterceptor.class::isInstance)) {
210+
this.transactionSynchronizationFactory = new PassThroughTransactionSynchronizationFactory();
209211
}
210212
this.initialized = true;
211213
}
@@ -328,7 +330,7 @@ private Message<?> pollForMessage() {
328330
return this.pollingTask.call();
329331
}
330332
catch (Exception e) {
331-
if (e instanceof MessagingException) {
333+
if (e instanceof MessagingException) { // NOSONAR
332334
throw (MessagingException) e;
333335
}
334336
else {
@@ -395,13 +397,11 @@ private void messageReceived(IntegrationResourceHolder holder, Message<?> messag
395397
try {
396398
handleMessage(message);
397399
}
398-
catch (Exception e) {
399-
if (e instanceof MessagingException) {
400-
throw new MessagingExceptionWrapper(message, (MessagingException) e);
401-
}
402-
else {
403-
throw new MessagingException(message, e);
404-
}
400+
catch (MessagingException ex) {
401+
throw new MessagingExceptionWrapper(message, ex);
402+
}
403+
catch (Exception ex) {
404+
throw new MessagingException(message, ex);
405405
}
406406
}
407407
}

spring-integration-core/src/main/java/org/springframework/integration/history/MessageHistory.java

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.ListIterator;
2626
import java.util.Properties;
27+
import java.util.stream.Collectors;
2728

2829
import org.apache.commons.logging.Log;
2930
import org.apache.commons.logging.LogFactory;
@@ -35,22 +36,23 @@
3536
import org.springframework.integration.support.MutableMessage;
3637
import org.springframework.integration.support.MutableMessageBuilderFactory;
3738
import org.springframework.integration.support.context.NamedComponent;
39+
import org.springframework.lang.Nullable;
3840
import org.springframework.messaging.Message;
3941
import org.springframework.messaging.support.ErrorMessage;
4042
import org.springframework.messaging.support.GenericMessage;
4143
import org.springframework.util.Assert;
42-
import org.springframework.util.StringUtils;
4344

4445
/**
4546
* @author Mark Fisher
4647
* @author Artem Bilan
4748
* @author Gary Russell
49+
*
4850
* @since 2.0
4951
*/
5052
@SuppressWarnings("serial")
5153
public final class MessageHistory implements List<Properties>, Serializable {
5254

53-
private static final Log logger = LogFactory.getLog(MessageHistory.class);
55+
private static final Log LOGGER = LogFactory.getLog(MessageHistory.class);
5456

5557
private static final UnsupportedOperationException UNSUPPORTED_OPERATION_EXCEPTION_IMMUTABLE =
5658
new UnsupportedOperationException("MessageHistory is immutable.");
@@ -68,9 +70,11 @@ public final class MessageHistory implements List<Properties>, Serializable {
6870

6971
private final List<Properties> components;
7072

71-
72-
public static MessageHistory read(Message<?> message) {
73-
return message != null ? message.getHeaders().get(HEADER_NAME, MessageHistory.class) : null;
73+
@Nullable
74+
public static MessageHistory read(@Nullable Message<?> message) {
75+
return message != null
76+
? message.getHeaders().get(HEADER_NAME, MessageHistory.class)
77+
: null;
7478
}
7579

7680
public static <T> Message<T> write(Message<T> message, NamedComponent component) {
@@ -87,8 +91,10 @@ public static <T> Message<T> write(Message<T> messageArg, NamedComponent compone
8791
Properties metadata = extractMetadata(component);
8892
if (!metadata.isEmpty()) {
8993
MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
90-
List<Properties> components = (previousHistory != null) ?
91-
new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
94+
List<Properties> components =
95+
previousHistory != null
96+
? new ArrayList<>(previousHistory)
97+
: new ArrayList<>();
9298
components.add(metadata);
9399
MessageHistory history = new MessageHistory(components);
94100

@@ -111,13 +117,13 @@ else if (message instanceof AdviceMessage) {
111117
else {
112118
if (!(message instanceof GenericMessage) &&
113119
(messageBuilderFactory instanceof DefaultMessageBuilderFactory ||
114-
messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
115-
if (logger.isWarnEnabled()) {
116-
logger.warn("MessageHistory rebuilds the message and produces the result of the [" +
117-
messageBuilderFactory + "], not an instance of the provided type [" +
118-
message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " +
119-
"to retain custom messages during MessageHistory tracking.");
120-
}
120+
messageBuilderFactory instanceof MutableMessageBuilderFactory)
121+
&& LOGGER.isWarnEnabled()) {
122+
123+
LOGGER.warn("MessageHistory rebuilds the message and produces the result of the [" +
124+
messageBuilderFactory + "], not an instance of the provided type [" +
125+
message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " +
126+
"to retain custom messages during MessageHistory tracking.");
121127
}
122128
message = messageBuilderFactory.fromMessage(message)
123129
.setHeader(HEADER_NAME, history)
@@ -201,14 +207,10 @@ public int lastIndexOf(Object o) {
201207

202208
@Override
203209
public String toString() {
204-
List<String> names = new ArrayList<String>();
205-
for (Properties p : this.components) {
206-
String name = p.getProperty(NAME_PROPERTY);
207-
if (name != null) {
208-
names.add(name);
209-
}
210-
}
211-
return StringUtils.collectionToCommaDelimitedString(names);
210+
return this.components
211+
.stream()
212+
.map((props) -> props.getProperty(NAME_PROPERTY))
213+
.collect(Collectors.joining(","));
212214
}
213215

214216

0 commit comments

Comments
 (0)