Skip to content

Commit ca2e70f

Browse files
garyrussellartembilan
authored andcommitted
Sonar Fixes
Critical smells for `o.s.i.jdbc`. Fix new smell in TCP.
1 parent 579a722 commit ca2e70f

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactory.java

+3-2
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.
@@ -26,6 +26,7 @@
2626
import org.springframework.integration.ip.IpHeaders;
2727
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
2828
import org.springframework.integration.util.SimplePool;
29+
import org.springframework.lang.Nullable;
2930
import org.springframework.messaging.Message;
3031
import org.springframework.messaging.MessagingException;
3132
import org.springframework.messaging.support.ErrorMessage;
@@ -392,7 +393,7 @@ private final class CachedConnection extends TcpConnectionInterceptorSupport {
392393

393394
private final AtomicBoolean released = new AtomicBoolean();
394395

395-
private CachedConnection(TcpConnectionSupport connection, TcpListener tcpListener) {
396+
private CachedConnection(TcpConnectionSupport connection, @Nullable TcpListener tcpListener) {
396397
super.setTheConnection(connection);
397398
registerListener(tcpListener);
398399
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ else if (isActive()) {
204204
* @return The Server Socket.
205205
* @throws IOException Any IOException.
206206
*/
207-
protected ServerSocket createServerSocket(int port, int backlog, InetAddress whichNic) throws IOException {
207+
protected ServerSocket createServerSocket(int port, int backlog, @Nullable InetAddress whichNic)
208+
throws IOException {
209+
208210
ServerSocketFactory serverSocketFactory = this.tcpSocketFactorySupport.getServerSocketFactory();
209211
if (whichNic == null) {
210212
return serverSocketFactory.createServerSocket(port, Math.abs(backlog));

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/lock/DefaultLockRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Dave Syer
4343
* @author Artem Bilan
4444
* @author Glenn Renfro
45+
* @author Gary Russell
4546
*
4647
* @since 4.3
4748
*/
@@ -169,8 +170,8 @@ public boolean acquire(String lock) {
169170
@Override
170171
public boolean isAcquired(String lock) {
171172
deleteExpired(lock);
172-
return this.template.queryForObject(this.countQuery, Integer.class, this.region, lock, this.id,
173-
new Date(System.currentTimeMillis() - this.ttl)) == 1;
173+
return this.template.queryForObject(this.countQuery, Integer.class, // NOSONAR query never returns null
174+
this.region, lock, this.id, new Date(System.currentTimeMillis() - this.ttl)) == 1;
174175
}
175176

176177
private void deleteExpired(String lock) {

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public MessageGroup getMessageGroup(Object groupId) {
461461
*/
462462
@ManagedAttribute
463463
public int getMessageGroupCount() {
464-
return this.jdbcTemplate.queryForObject(
464+
return this.jdbcTemplate.queryForObject(// NOSONAR query never returns null
465465
getQuery(Query.COUNT_GROUPS,
466466
() -> "SELECT COUNT(DISTINCT GROUP_KEY) from %PREFIX%CHANNEL_MESSAGE where REGION = ?"),
467467
Integer.class, this.region);
@@ -489,7 +489,7 @@ protected String getQuery(Query queryName, Supplier<String> queryProvider) {
489489
@ManagedAttribute
490490
public int messageGroupSize(Object groupId) {
491491
final String key = getKey(groupId);
492-
return this.jdbcTemplate.queryForObject(
492+
return this.jdbcTemplate.queryForObject(// NOSONAR query never returns null
493493
getQuery(Query.GROUP_SIZE,
494494
() -> this.channelMessageStoreQueryProvider.getCountAllMessagesInGroupQuery()),
495495
Integer.class, key, this.region);
@@ -575,7 +575,9 @@ protected Message<?> doPollForMessage(String groupIdKey) {
575575
if (messages.size() > 0) {
576576

577577
final Message<?> message = messages.get(0);
578-
final String messageId = message.getHeaders().getId().toString();
578+
UUID id = message.getHeaders().getId();
579+
Assert.state(id != null, "Messages must have an id header to be stored");
580+
final String messageId = id.toString();
579581

580582
if (this.usingIdCache) {
581583
this.idCacheWriteLock.lock();

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ public Message<?> removeMessage(UUID id) {
293293
@Override
294294
@ManagedAttribute
295295
public long getMessageCount() {
296-
return this.jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), Long.class, this.region);
296+
return this.jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), // NOSONAR query never returns null
297+
Long.class, this.region);
297298
}
298299

299300
@Override
@@ -355,7 +356,7 @@ public <T> Message<T> addMessage(final Message<T> message) {
355356
@Override
356357
public void addMessagesToGroup(Object groupId, Message<?>... messages) {
357358
final String groupKey = getKey(groupId);
358-
boolean groupNotExist = this.jdbcTemplate.queryForObject(this.getQuery(Query.GROUP_EXISTS),
359+
boolean groupNotExist = this.jdbcTemplate.queryForObject(this.getQuery(Query.GROUP_EXISTS), // NOSONAR query never returns null
359360
Integer.class, groupKey, this.region) < 1;
360361

361362
final Timestamp updatedDate = new Timestamp(System.currentTimeMillis());
@@ -399,21 +400,22 @@ public void addMessagesToGroup(Object groupId, Message<?>... messages) {
399400
@Override
400401
@ManagedAttribute
401402
public int getMessageGroupCount() {
402-
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_GROUPS), Integer.class, this.region);
403+
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_GROUPS), // NOSONAR query never returns null
404+
Integer.class, this.region);
403405
}
404406

405407
@Override
406408
@ManagedAttribute
407409
public int getMessageCountForAllMessageGroups() {
408-
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUPS),
410+
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUPS), // NOSONAR query never returns null
409411
Integer.class, this.region);
410412
}
411413

412414
@Override
413415
@ManagedAttribute
414416
public int messageGroupSize(Object groupId) {
415417
String key = getKey(groupId);
416-
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUP),
418+
return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUP), // NOSONAR query never returns null
417419
Integer.class, key, this.region);
418420
}
419421

@@ -670,15 +672,18 @@ private String getKey(Object input) {
670672
/**
671673
* Convenience class to be used to unpack a message from a result set row. Uses column named in the result set to
672674
* extract the required data, so that select clause ordering is unimportant.
673-
*
674-
* @author Dave Syer
675675
*/
676676
private class MessageMapper implements RowMapper<Message<?>> {
677677

678678
@Override
679679
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
680680
byte[] messageBytes = JdbcMessageStore.this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES");
681-
return (Message<?>) JdbcMessageStore.this.deserializer.convert(messageBytes);
681+
if (messageBytes == null) {
682+
return null;
683+
}
684+
else {
685+
return (Message<?>) JdbcMessageStore.this.deserializer.convert(messageBytes);
686+
}
682687
}
683688

684689
}

spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/channel/MessageRowMapper.java

+8-2
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.
@@ -47,7 +47,13 @@ public MessageRowMapper(WhiteListDeserializingConverter deserializer, LobHandler
4747

4848
@Override
4949
public Message<?> mapRow(ResultSet rs, int rowNum) throws SQLException {
50-
return (Message<?>) this.deserializer.convert(this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"));
50+
byte[] blobAsBytes = this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES");
51+
if (blobAsBytes == null) {
52+
return null;
53+
}
54+
else {
55+
return (Message<?>) this.deserializer.convert(blobAsBytes);
56+
}
5157
}
5258

5359
}

0 commit comments

Comments
 (0)