From ca2e70f236246f1c6d97964b3aa2ac25ecd3facf Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Fri, 7 Dec 2018 16:46:49 -0500 Subject: [PATCH] Sonar Fixes Critical smells for `o.s.i.jdbc`. Fix new smell in TCP. --- .../CachingClientConnectionFactory.java | 5 +++-- .../TcpNetServerConnectionFactory.java | 4 +++- .../jdbc/lock/DefaultLockRepository.java | 5 +++-- .../jdbc/store/JdbcChannelMessageStore.java | 8 ++++--- .../jdbc/store/JdbcMessageStore.java | 21 ++++++++++++------- .../jdbc/store/channel/MessageRowMapper.java | 10 +++++++-- 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactory.java index df02e36b29..a9532c4ae8 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/CachingClientConnectionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -26,6 +26,7 @@ import org.springframework.core.serializer.Serializer; import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.support.AbstractIntegrationMessageBuilder; import org.springframework.integration.util.SimplePool; +import org.springframework.lang.Nullable; import org.springframework.messaging.Message; import org.springframework.messaging.MessagingException; import org.springframework.messaging.support.ErrorMessage; @@ -392,7 +393,7 @@ public class CachingClientConnectionFactory extends AbstractClientConnectionFact private final AtomicBoolean released = new AtomicBoolean(); - private CachedConnection(TcpConnectionSupport connection, TcpListener tcpListener) { + private CachedConnection(TcpConnectionSupport connection, @Nullable TcpListener tcpListener) { super.setTheConnection(connection); registerListener(tcpListener); } diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java index 72d3e53723..1245bc6318 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java @@ -204,7 +204,9 @@ public class TcpNetServerConnectionFactory extends AbstractServerConnectionFacto * @return The Server Socket. * @throws IOException Any IOException. */ - protected ServerSocket createServerSocket(int port, int backlog, InetAddress whichNic) throws IOException { + protected ServerSocket createServerSocket(int port, int backlog, @Nullable InetAddress whichNic) + throws IOException { + ServerSocketFactory serverSocketFactory = this.tcpSocketFactorySupport.getServerSocketFactory(); if (whichNic == null) { return serverSocketFactory.createServerSocket(port, Math.abs(backlog)); 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 4fe1fcd859..a541b7ed5b 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 @@ -42,6 +42,7 @@ import org.springframework.util.Assert; * @author Dave Syer * @author Artem Bilan * @author Glenn Renfro + * @author Gary Russell * * @since 4.3 */ @@ -169,8 +170,8 @@ public class DefaultLockRepository implements LockRepository, InitializingBean { @Override public boolean isAcquired(String lock) { deleteExpired(lock); - return this.template.queryForObject(this.countQuery, Integer.class, this.region, lock, this.id, - new Date(System.currentTimeMillis() - this.ttl)) == 1; + return this.template.queryForObject(this.countQuery, Integer.class, // NOSONAR query never returns null + this.region, lock, this.id, new Date(System.currentTimeMillis() - this.ttl)) == 1; } private void deleteExpired(String lock) { diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java index a169ac3ace..c06412dc83 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcChannelMessageStore.java @@ -461,7 +461,7 @@ public class JdbcChannelMessageStore implements PriorityCapableChannelMessageSto */ @ManagedAttribute public int getMessageGroupCount() { - return this.jdbcTemplate.queryForObject( + return this.jdbcTemplate.queryForObject(// NOSONAR query never returns null getQuery(Query.COUNT_GROUPS, () -> "SELECT COUNT(DISTINCT GROUP_KEY) from %PREFIX%CHANNEL_MESSAGE where REGION = ?"), Integer.class, this.region); @@ -489,7 +489,7 @@ public class JdbcChannelMessageStore implements PriorityCapableChannelMessageSto @ManagedAttribute public int messageGroupSize(Object groupId) { final String key = getKey(groupId); - return this.jdbcTemplate.queryForObject( + return this.jdbcTemplate.queryForObject(// NOSONAR query never returns null getQuery(Query.GROUP_SIZE, () -> this.channelMessageStoreQueryProvider.getCountAllMessagesInGroupQuery()), Integer.class, key, this.region); @@ -575,7 +575,9 @@ public class JdbcChannelMessageStore implements PriorityCapableChannelMessageSto if (messages.size() > 0) { final Message message = messages.get(0); - final String messageId = message.getHeaders().getId().toString(); + UUID id = message.getHeaders().getId(); + Assert.state(id != null, "Messages must have an id header to be stored"); + final String messageId = id.toString(); if (this.usingIdCache) { this.idCacheWriteLock.lock(); diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java index 298abfd45d..b64e581387 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/JdbcMessageStore.java @@ -293,7 +293,8 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa @Override @ManagedAttribute public long getMessageCount() { - return this.jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), Long.class, this.region); + return this.jdbcTemplate.queryForObject(getQuery(Query.GET_MESSAGE_COUNT), // NOSONAR query never returns null + Long.class, this.region); } @Override @@ -355,7 +356,7 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa @Override public void addMessagesToGroup(Object groupId, Message... messages) { final String groupKey = getKey(groupId); - boolean groupNotExist = this.jdbcTemplate.queryForObject(this.getQuery(Query.GROUP_EXISTS), + boolean groupNotExist = this.jdbcTemplate.queryForObject(this.getQuery(Query.GROUP_EXISTS), // NOSONAR query never returns null Integer.class, groupKey, this.region) < 1; final Timestamp updatedDate = new Timestamp(System.currentTimeMillis()); @@ -399,13 +400,14 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa @Override @ManagedAttribute public int getMessageGroupCount() { - return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_GROUPS), Integer.class, this.region); + return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_GROUPS), // NOSONAR query never returns null + Integer.class, this.region); } @Override @ManagedAttribute public int getMessageCountForAllMessageGroups() { - return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUPS), + return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUPS), // NOSONAR query never returns null Integer.class, this.region); } @@ -413,7 +415,7 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa @ManagedAttribute public int messageGroupSize(Object groupId) { String key = getKey(groupId); - return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUP), + return this.jdbcTemplate.queryForObject(getQuery(Query.COUNT_ALL_MESSAGES_IN_GROUP), // NOSONAR query never returns null Integer.class, key, this.region); } @@ -670,15 +672,18 @@ public class JdbcMessageStore extends AbstractMessageGroupStore implements Messa /** * Convenience class to be used to unpack a message from a result set row. Uses column named in the result set to * extract the required data, so that select clause ordering is unimportant. - * - * @author Dave Syer */ private class MessageMapper implements RowMapper> { @Override public Message mapRow(ResultSet rs, int rowNum) throws SQLException { byte[] messageBytes = JdbcMessageStore.this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"); - return (Message) JdbcMessageStore.this.deserializer.convert(messageBytes); + if (messageBytes == null) { + return null; + } + else { + return (Message) JdbcMessageStore.this.deserializer.convert(messageBytes); + } } } diff --git a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/channel/MessageRowMapper.java b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/channel/MessageRowMapper.java index b98bbf8246..9eac951857 100644 --- a/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/channel/MessageRowMapper.java +++ b/spring-integration-jdbc/src/main/java/org/springframework/integration/jdbc/store/channel/MessageRowMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -47,7 +47,13 @@ public class MessageRowMapper implements RowMapper> { @Override public Message mapRow(ResultSet rs, int rowNum) throws SQLException { - return (Message) this.deserializer.convert(this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES")); + byte[] blobAsBytes = this.lobHandler.getBlobAsBytes(rs, "MESSAGE_BYTES"); + if (blobAsBytes == null) { + return null; + } + else { + return (Message) this.deserializer.convert(blobAsBytes); + } } }