Skip to content

Commit fc18476

Browse files
committed
More MQTT polishing according Sonar report
1 parent 2b9ba71 commit fc18476

File tree

7 files changed

+46
-81
lines changed

7 files changed

+46
-81
lines changed

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/config/xml/MqttParserUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -33,18 +33,18 @@
3333
* respective {@link BeanDefinition}s.
3434
*
3535
* @author Gary Russell
36+
* @author Artem Bilan
37+
*
3638
* @since 4.0
3739
*
3840
*/
39-
public final class MqttParserUtils {
41+
final class MqttParserUtils {
4042

41-
/** Prevent instantiation. */
4243
private MqttParserUtils() {
43-
throw new AssertionError();
44-
}
4544

46-
public static void parseCommon(Element element, BeanDefinitionBuilder builder, ParserContext parserContext) {
45+
}
4746

47+
static void parseCommon(Element element, BeanDefinitionBuilder builder, ParserContext parserContext) {
4848
ValueHolder holder;
4949
int n = 0;
5050
String url = element.getAttribute("url");
@@ -54,7 +54,7 @@ public static void parseCommon(Element element, BeanDefinitionBuilder builder, P
5454
holder.setType("java.lang.String");
5555
}
5656
builder.addConstructorArgValue(element.getAttribute("client-id"));
57-
holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n++);
57+
holder = builder.getRawBeanDefinition().getConstructorArgumentValues().getIndexedArgumentValues().get(n);
5858
holder.setType("java.lang.String");
5959
String clientFactory = element.getAttribute("client-factory");
6060
if (StringUtils.hasText(clientFactory)) {

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/inbound/AbstractMqttMessageDrivenChannelAdapter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -35,6 +35,8 @@
3535
* Abstract class for MQTT Message-Driven Channel Adapters.
3636
*
3737
* @author Gary Russell
38+
* @author Artem Bilan
39+
*
3840
* @since 4.0
3941
*
4042
*/
@@ -50,15 +52,15 @@ public abstract class AbstractMqttMessageDrivenChannelAdapter extends MessagePro
5052

5153
private volatile MqttMessageConverter converter;
5254

53-
protected final Lock topicLock = new ReentrantLock();
55+
protected final Lock topicLock = new ReentrantLock(); // NOSONAR
5456

5557
public AbstractMqttMessageDrivenChannelAdapter(String url, String clientId, String... topic) {
5658
Assert.hasText(clientId, "'clientId' cannot be null or empty");
5759
Assert.notNull(topic, "'topics' cannot be null");
5860
Assert.noNullElements(topic, "'topics' cannot have null elements");
5961
this.url = url;
6062
this.clientId = clientId;
61-
this.topics = new LinkedHashSet<Topic>();
63+
this.topics = new LinkedHashSet<>();
6264
for (String t : topic) {
6365
this.topics.add(new Topic(t, 1));
6466
}
@@ -225,10 +227,8 @@ public void removeTopic(String... topic) {
225227
this.topicLock.lock();
226228
try {
227229
for (String t : topic) {
228-
if (this.topics.remove(new Topic(t, 0))) {
229-
if (this.logger.isDebugEnabled()) {
230-
logger.debug("Removed '" + t + "' from subscriptions.");
231-
}
230+
if (this.topics.remove(new Topic(t, 0)) && this.logger.isDebugEnabled()) {
231+
logger.debug("Removed '" + t + "' from subscriptions.");
232232
}
233233
}
234234
}

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/inbound/MqttPahoMessageDrivenChannelAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
* @author Gary Russell
4646
* @author Artem Bilan
4747
*
48-
* @since 1.0
48+
* @since 4.0
4949
*
5050
*/
5151
public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDrivenChannelAdapter
@@ -57,10 +57,10 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
5757

5858
private final MqttPahoClientFactory clientFactory;
5959

60-
private int recoveryInterval = DEFAULT_RECOVERY_INTERVAL;
61-
6260
private volatile long completionTimeout = DEFAULT_COMPLETION_TIMEOUT;
6361

62+
private volatile int recoveryInterval = DEFAULT_RECOVERY_INTERVAL;
63+
6464
private volatile IMqttClient client;
6565

6666
private volatile ScheduledFuture<?> reconnectFuture;

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/outbound/AbstractMqttMessageHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -37,6 +37,7 @@
3737
*
3838
* @author Gary Russell
3939
* @author Artem Bilan
40+
*
4041
* @since 4.0
4142
*
4243
*/

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/outbound/MqttPahoMessageHandler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,22 @@ private synchronized IMqttAsyncClient checkConnection() throws MqttException {
165165
this.client = null;
166166
}
167167
if (this.client == null) {
168-
IMqttAsyncClient client = null;
169168
try {
170169
MqttConnectOptions connectionOptions = this.clientFactory.getConnectionOptions();
171170
Assert.state(this.getUrl() != null || connectionOptions.getServerURIs() != null,
172171
"If no 'url' provided, connectionOptions.getServerURIs() must not be null");
173-
client = this.clientFactory.getAsyncClientInstance(this.getUrl(), this.getClientId());
172+
this.client = this.clientFactory.getAsyncClientInstance(this.getUrl(), this.getClientId());
174173
incrementClientInstance();
175-
client.setCallback(this);
176-
client.connect(connectionOptions).waitForCompletion(this.completionTimeout);
177-
this.client = client;
174+
this.client.setCallback(this);
175+
this.client.connect(connectionOptions).waitForCompletion(this.completionTimeout);
178176
if (logger.isDebugEnabled()) {
179177
logger.debug("Client connected");
180178
}
181179
}
182180
catch (MqttException e) {
183-
if (client != null) {
184-
client.close();
181+
if (this.client != null) {
182+
this.client.close();
183+
this.client = null;
185184
}
186185
throw new MessagingException("Failed to connect", e);
187186
}

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/support/DefaultPahoMessageConverter.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.mqtt.support;
1818

19+
import java.nio.charset.Charset;
20+
1921
import org.eclipse.paho.client.mqttv3.MqttMessage;
2022

2123
import org.springframework.beans.factory.BeanFactory;
@@ -38,12 +40,13 @@
3840
*
3941
* @author Gary Russell
4042
* @author Artem Bilan
43+
*
4144
* @since 4.0
4245
*
4346
*/
4447
public class DefaultPahoMessageConverter implements MqttMessageConverter, BeanFactoryAware {
4548

46-
private final String charset;
49+
private final Charset charset;
4750

4851
private final int defaultQos;
4952

@@ -68,7 +71,7 @@ public class DefaultPahoMessageConverter implements MqttMessageConverter, BeanFa
6871
* Construct a converter with default options (qos=0, retain=false, charset=UTF-8).
6972
*/
7073
public DefaultPahoMessageConverter() {
71-
this (0, false);
74+
this(0, false);
7275
}
7376

7477
/**
@@ -86,7 +89,7 @@ public DefaultPahoMessageConverter(int defaultQos, boolean defaultRetained) {
8689
/**
8790
* Construct a converter with default options (qos=0, retain=false) and
8891
* the supplied charset.
89-
* @param charset the charset used to convert outbound String paylaods to {@code byte[]} and inbound
92+
* @param charset the charset used to convert outbound String payloads to {@code byte[]} and inbound
9093
* {@code byte[]} to String (unless {@link #setPayloadAsBytes(boolean) payloadAdBytes} is true).
9194
* @since 4.1.2
9295
*/
@@ -99,7 +102,7 @@ public DefaultPahoMessageConverter(String charset) {
99102
* retain settings and the supplied charset.
100103
* @param defaultQos the default qos.
101104
* @param defaultRetained the default retained.
102-
* @param charset the charset used to convert outbound String paylaods to
105+
* @param charset the charset used to convert outbound String payloads to
103106
* {@code byte[]} and inbound {@code byte[]} to String (unless
104107
* {@link #setPayloadAsBytes(boolean) payloadAdBytes} is true).
105108
*/
@@ -121,6 +124,7 @@ public DefaultPahoMessageConverter(int defaultQos, boolean defaultRetained, Stri
121124
*/
122125
public DefaultPahoMessageConverter(int defaultQos, MessageProcessor<Integer> qosProcessor, boolean defaultRetained,
123126
MessageProcessor<Boolean> retainedProcessor) {
127+
124128
this(defaultQos, qosProcessor, defaultRetained, retainedProcessor, "UTF-8");
125129
}
126130

@@ -131,20 +135,21 @@ public DefaultPahoMessageConverter(int defaultQos, MessageProcessor<Integer> qos
131135
* @param qosProcessor a message processor to determine the qos.
132136
* @param defaultRetained the default retained.
133137
* @param retainedProcessor a message processor to determine the retained flag.
134-
* @param charset the charset used to convert outbound String paylaods to
138+
* @param charset the charset used to convert outbound String payloads to
135139
* {@code byte[]} and inbound {@code byte[]} to String (unless
136140
* {@link #setPayloadAsBytes(boolean) payloadAdBytes} is true).
137141
* @since 5.0
138142
*/
139143
public DefaultPahoMessageConverter(int defaultQos, MessageProcessor<Integer> qosProcessor, boolean defaultRetained,
140144
MessageProcessor<Boolean> retainedProcessor, String charset) {
145+
141146
Assert.notNull(qosProcessor, "'qosProcessor' cannot be null");
142147
Assert.notNull(retainedProcessor, "'retainedProcessor' cannot be null");
143148
this.defaultQos = defaultQos;
144149
this.qosProcessor = qosProcessor;
145150
this.defaultRetained = defaultRetained;
146151
this.retainedProcessor = retainedProcessor;
147-
this.charset = charset;
152+
this.charset = Charset.forName(charset);
148153
}
149154

150155
@Override
@@ -201,18 +206,19 @@ public Message<?> toMessage(Object mqttMessage, MessageHeaders headers) {
201206
return toMessage(null, (MqttMessage) mqttMessage);
202207
}
203208

204-
@SuppressWarnings("unchecked")
205209
@Override
206210
public Message<?> toMessage(String topic, MqttMessage mqttMessage) {
207211
try {
208-
AbstractIntegrationMessageBuilder<Object> messageBuilder;
212+
AbstractIntegrationMessageBuilder<?> messageBuilder;
209213
if (this.bytesMessageMapper != null) {
210-
messageBuilder = (AbstractIntegrationMessageBuilder<Object>) getMessageBuilderFactory()
211-
.fromMessage(this.bytesMessageMapper.toMessage(mqttMessage.getPayload()));
214+
messageBuilder =
215+
getMessageBuilderFactory()
216+
.fromMessage(this.bytesMessageMapper.toMessage(mqttMessage.getPayload()));
212217
}
213218
else {
214-
messageBuilder = getMessageBuilderFactory()
215-
.withPayload(mqttBytesToPayload(mqttMessage));
219+
messageBuilder =
220+
getMessageBuilderFactory()
221+
.withPayload(mqttBytesToPayload(mqttMessage));
216222
}
217223
messageBuilder
218224
.setHeader(MqttHeaders.RECEIVED_QOS, mqttMessage.getQos())
@@ -242,12 +248,10 @@ public MqttMessage fromMessage(Message<?> message, Class<?> targetClass) {
242248
/**
243249
* Subclasses can override this method to convert the byte[] to a payload.
244250
* The default implementation creates a String (default) or byte[].
245-
*
246251
* @param mqttMessage The inbound message.
247252
* @return The payload for the Spring integration message
248-
* @throws Exception Any.
249253
*/
250-
protected Object mqttBytesToPayload(MqttMessage mqttMessage) throws Exception {
254+
protected Object mqttBytesToPayload(MqttMessage mqttMessage) {
251255
if (this.payloadAsBytes) {
252256
return mqttMessage.getPayload();
253257
}
@@ -261,7 +265,6 @@ protected Object mqttBytesToPayload(MqttMessage mqttMessage) throws Exception {
261265
* The default implementation accepts a byte[] or String payload.
262266
* If a {@link BytesMessageMapper} is provided, conversion to byte[]
263267
* is delegated to it, so any payload that it can handle is supported.
264-
*
265268
* @param message The outbound Message.
266269
* @return The byte[] which will become the payload of the MQTT Message.
267270
*/
@@ -283,12 +286,7 @@ protected byte[] messageToMqttBytes(Message<?> message) {
283286
+ payload.getClass().getName() + " payloads");
284287
byte[] payloadBytes;
285288
if (payload instanceof String) {
286-
try {
287-
payloadBytes = ((String) payload).getBytes(this.charset);
288-
}
289-
catch (Exception e) {
290-
throw new MessageConversionException("failed to convert Message to object", e);
291-
}
289+
payloadBytes = ((String) payload).getBytes(this.charset);
292290
}
293291
else {
294292
payloadBytes = (byte[]) payload;

spring-integration-mqtt/src/main/java/org/springframework/integration/mqtt/support/MqttUtils.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)