Skip to content

Commit

Permalink
Sonar Fixes
Browse files Browse the repository at this point in the history
https://sonar.spring.io/component_issues?id=org.springframework.integration%3Aspring-integration%3Amaster#resolved=false|types=BUG

In `IntegrationFlowRegistration` double check locking is ok for `inputChannel`
because we're assigning an existing object, but `MessagingTemplate` constructs
a new object for which double check locking doesn't work.

In any case, for both these items, the chance of concurrent access is extremely low
and is idempotent anyway, so remove double check locking.

Several inner classes can be static.

Other minor fixes.
  • Loading branch information
garyrussell authored and artembilan committed Nov 18, 2016
1 parent 1bba73f commit f070de0
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public synchronized void start() {
if (!this.running) {
if (!this.lazyConnect && this.connectionFactory != null) {
try {
Connection connection = this.connectionFactory.createConnection();
Connection connection = this.connectionFactory.createConnection(); // NOSONAR (close)
if (connection != null) {
connection.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public synchronized void run() {
}


private final class MessageChannelWrapper {
private static final class MessageChannelWrapper {

private final MessageChannel channel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected Message<?> doReceive(long timeout) {
long nanos = TimeUnit.MILLISECONDS.toNanos(timeout);
long deadline = System.nanoTime() + nanos;
while (this.queue.size() == 0 && nanos > 0) {
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS);
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS); // NOSONAR - ok to ignore result
nanos = deadline - System.nanoTime();
}
return this.queue.poll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected MessageHandler createHandler(Object bean, Method method, List<Annotati
return serviceActivator;
}

private final class ReplyProducingMessageHandlerWrapper extends AbstractReplyProducingMessageHandler
private static final class ReplyProducingMessageHandlerWrapper extends AbstractReplyProducingMessageHandler
implements Lifecycle {

private final MessageHandler target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class StandardIntegrationFlow implements IntegrationFlow, SmartLifecycle

private final List<SmartLifecycle> lifecycles = new LinkedList<SmartLifecycle>();

private final boolean registerComponents = true;
private final boolean registerComponents = true; // NOSONAR

private boolean running;

StandardIntegrationFlow(Set<Object> integrationComponents) {
this.integrationComponents = new LinkedList<Object>(integrationComponents);
}

//TODO Figure out some custom DestinationResolver when we don't register singletons
//TODO Figure out some custom DestinationResolver when we don't register singletons - remove NOSONAR above when done
/*public void setRegisterComponents(boolean registerComponents) {
this.registerComponents = registerComponents;
}*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class IntegrationFlowRegistration {
}

void setBeanFactory(ConfigurableListableBeanFactory beanFactory) {
this.beanFactory = beanFactory;
this.beanFactory = beanFactory; // NOSONAR (synchronization)
}

void setIntegrationFlowContext(IntegrationFlowContext integrationFlowContext) {
Expand All @@ -78,28 +78,24 @@ public IntegrationFlow getIntegrationFlow() {

public MessageChannel getInputChannel() {
if (this.inputChannel == null) {
synchronized (this) {
if (this.inputChannel == null) {
if (this.integrationFlow instanceof StandardIntegrationFlow) {
StandardIntegrationFlow integrationFlow = (StandardIntegrationFlow) this.integrationFlow;
Object next = integrationFlow.getIntegrationComponents().iterator().next();
if (next instanceof MessageChannel) {
this.inputChannel = (MessageChannel) next;
}
else {
throw new IllegalStateException("The 'IntegrationFlow' [" + integrationFlow + "] " +
"doesn't start with 'MessageChannel' for direct message sending.");
}
}
else {
throw new IllegalStateException("Only 'StandardIntegrationFlow' instances " +
"(e.g. extracted from 'IntegrationFlow' Lambdas) can be used " +
"for direct 'send' operation. " +
"But [" + this.integrationFlow + "] ins't one of them.\n" +
"Consider 'BeanFactory.getBean()' usage for sending messages " +
"to the required 'MessageChannel'.");
}
if (this.integrationFlow instanceof StandardIntegrationFlow) {
StandardIntegrationFlow integrationFlow = (StandardIntegrationFlow) this.integrationFlow;
Object next = integrationFlow.getIntegrationComponents().iterator().next();
if (next instanceof MessageChannel) {
this.inputChannel = (MessageChannel) next;
}
else {
throw new IllegalStateException("The 'IntegrationFlow' [" + integrationFlow + "] " +
"doesn't start with 'MessageChannel' for direct message sending.");
}
}
else {
throw new IllegalStateException("Only 'StandardIntegrationFlow' instances " +
"(e.g. extracted from 'IntegrationFlow' Lambdas) can be used " +
"for direct 'send' operation. " +
"But [" + this.integrationFlow + "] ins't one of them.\n" +
"Consider 'BeanFactory.getBean()' usage for sending messages " +
"to the required 'MessageChannel'.");
}
}
return this.inputChannel;
Expand All @@ -115,25 +111,21 @@ public MessageChannel getInputChannel() {
*/
public MessagingTemplate getMessagingTemplate() {
if (this.messagingTemplate == null) {
synchronized (this) {
if (this.messagingTemplate == null) {
this.messagingTemplate = new MessagingTemplate(getInputChannel()) {

@Override
public Message<?> receive() {
return receiveAndConvert(Message.class);
}

@Override
public <T> T receiveAndConvert(Class<T> targetClass) {
throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " +
"isn't supported on the 'IntegrationFlow' input channel.");
}

};
this.messagingTemplate.setBeanFactory(this.beanFactory);
this.messagingTemplate = new MessagingTemplate(getInputChannel()) {

@Override
public Message<?> receive() {
return receiveAndConvert(Message.class);
}
}

@Override
public <T> T receiveAndConvert(Class<T> targetClass) {
throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " +
"isn't supported on the 'IntegrationFlow' input channel.");
}

};
this.messagingTemplate.setBeanFactory(this.beanFactory);
}
return this.messagingTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ public void addMessagesToGroup(Object groupId, Message<?>... messages) {
throw outOfCapacityException;
}
group = getMessageGroupFactory().create(groupId);
this.groupIdToMessageGroup.putIfAbsent(groupId, group);
this.groupIdToMessageGroup.put(groupId, group);
upperBound = new UpperBound(this.groupCapacity);
for (Message<?> message : messages) {
upperBound.tryAcquire(-1);
group.add(message);
}
this.groupToUpperBound.putIfAbsent(groupId, upperBound);
this.groupToUpperBound.put(groupId, upperBound);
}
else {
upperBound = this.groupToUpperBound.get(groupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Message<?> toMessage(Object object) throws Exception {
}


private class DefaultOutboundMessageMapper implements OutboundMessageMapper<Object> {
private static class DefaultOutboundMessageMapper implements OutboundMessageMapper<Object> {

DefaultOutboundMessageMapper() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,22 @@ public synchronized void setPoolSize(int poolSize) {
* if it was recently reduced and too many items were in use to allow the new size
* to be set.
*/
public int getPoolSize() {
@Override
public synchronized int getPoolSize() {
return this.poolSize.get();
}

@Override
public int getIdleCount() {
return this.available.size();
}

@Override
public int getActiveCount() {
return this.inUse.size();
}

@Override
public int getAllocatedCount() {
return this.allocated.size();
}
Expand All @@ -153,6 +157,7 @@ public void setWaitTimeout(long waitTimeout) {
* Obtains an item from the pool; waits up to waitTime milliseconds (default infinity).
* @throws MessagingException if no items become available in time.
*/
@Override
public T getItem() {
boolean permitted = false;
try {
Expand Down Expand Up @@ -205,6 +210,7 @@ else if (this.callback.isStale(item)) {
/**
* Returns an item to the pool.
*/
@Override
public synchronized void releaseItem(T item) {
Assert.notNull(item, "Item cannot be null");
Assert.isTrue(this.allocated.contains(item),
Expand Down Expand Up @@ -234,6 +240,7 @@ public synchronized void releaseItem(T item) {
}
}

@Override
public synchronized void removeAllIdleItems() {
T item;
while ((item = this.available.poll()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private static final class IntegrationGraphCorsConfigurer extends WebMvcConfigur

private final String[] allowedOrigins;

private IntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) {
private IntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { // NOSONAR
this.path = path;
this.allowedOrigins = allowedOrigins;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public Message<?> getReply() throws Exception {
* before the reply, on a different thread.
*/
logger.debug("second chance");
this.secondChanceLatch.await(2, TimeUnit.SECONDS);
this.secondChanceLatch.await(2, TimeUnit.SECONDS); // NOSONAR don't care about result
waitForMessageAfterError = false;
}
else if (this.reply.getPayload() instanceof MessagingException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ public String toString() {
+ ", port=" + getPort();
}

private final class PendingIO {
private static final class PendingIO {

private final long failedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void setConnectionWaitTimeout(int connectionWaitTimeout) {
* @param poolSize the new pool size.
* @see SimplePool#setPoolSize(int)
*/
public synchronized void setPoolSize(int poolSize) {
public void setPoolSize(int poolSize) {
this.pool.setPoolSize(poolSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected List<? extends Map<String, Object>> executeUpdateQuery(final Message<?
(PreparedStatementCallback<List<Map<String, Object>>>) ps -> {
JdbcMessageHandler.this.preparedStatementSetter.setValues(ps, message);
ps.executeUpdate();
ResultSet keys = ps.getGeneratedKeys();
ResultSet keys = ps.getGeneratedKeys(); // NOSONAR closed in JdbcUtils
if (keys != null) {
try {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ private AbstractIntegrationMessageBuilder<?> buildReply(javax.jms.Message jmsRep
}

private Object sendAndReceiveWithContainer(Message<?> requestMessage) throws JMSException {
Connection connection = this.createConnection();
Connection connection = this.createConnection(); // NOSONAR - closed in ConnectionFactoryUtils.
Session session = null;
Destination replyTo = this.replyContainer.getReplyDestination();
try {
Expand Down Expand Up @@ -841,7 +841,7 @@ private Object sendAndReceiveWithContainer(Message<?> requestMessage) throws JMS
}

private javax.jms.Message sendAndReceiveWithoutContainer(Message<?> requestMessage) throws JMSException {
Connection connection = this.createConnection();
Connection connection = this.createConnection(); // NOSONAR - closed in ConnectionFactoryUtils.
Session session = null;
Destination replyTo = null;
try {
Expand Down Expand Up @@ -1329,7 +1329,7 @@ private void onMessageSync(javax.jms.Message message, String correlationId) {
}
}

private class GatewayReplyListenerContainer extends DefaultMessageListenerContainer {
private static class GatewayReplyListenerContainer extends DefaultMessageListenerContainer {

private volatile Destination replyDestination;

Expand Down Expand Up @@ -1421,7 +1421,7 @@ protected void recoverAfterListenerSetupFailure() {
}
}

private final class TimedReply {
private static final class TimedReply {

private final long timeStamp = System.currentTimeMillis();

Expand Down Expand Up @@ -1470,6 +1470,7 @@ public void run() {
new Date(now + JmsOutboundGateway.this.receiveTimeout));
}
}

}

private class IdleContainerStopper implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void run() {
/**
* Callback used for handling the event-driven idle response.
*/
private class SimpleMessageCountListener extends MessageCountAdapter {
private static class SimpleMessageCountListener extends MessageCountAdapter {

SimpleMessageCountListener() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public ErrorMessage convert(DBObject source) {
}

@WritingConverter
private class ThrowableToBytesConverter implements Converter<Throwable, byte[]> {
private static class ThrowableToBytesConverter implements Converter<Throwable, byte[]> {

private final Converter<Object, byte[]> serializingConverter = new SerializingConverter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
(RedisCallback<Object>) connection -> connection.execute(command, actualArgs));
}

private class PayloadArgumentsStrategy implements ArgumentsStrategy {
private static class PayloadArgumentsStrategy implements ArgumentsStrategy {

PayloadArgumentsStrategy() {
super();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ protected Object parseStructuredDataElements(Reader r) {
return fragments;
}

protected class Reader {
protected static class Reader {

private final String line;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public void reset() {

public void await() {
try {
this.latch.await(5000, TimeUnit.MILLISECONDS);
if (latch.getCount() != 0) {
if (!this.latch.await(5000, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("test latch.await() did not count down");
}
}
Expand Down

0 comments on commit f070de0

Please sign in to comment.