Skip to content

Commit f070de0

Browse files
garyrussellartembilan
authored andcommitted
Sonar Fixes
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.
1 parent 1bba73f commit f070de0

File tree

20 files changed

+64
-65
lines changed

20 files changed

+64
-65
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/outbound/AbstractAmqpOutboundEndpoint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ public synchronized void start() {
349349
if (!this.running) {
350350
if (!this.lazyConnect && this.connectionFactory != null) {
351351
try {
352-
Connection connection = this.connectionFactory.createConnection();
352+
Connection connection = this.connectionFactory.createConnection(); // NOSONAR (close)
353353
if (connection != null) {
354354
connection.close();
355355
}

spring-integration-core/src/main/java/org/springframework/integration/channel/DefaultHeaderChannelRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public synchronized void run() {
229229
}
230230

231231

232-
private final class MessageChannelWrapper {
232+
private static final class MessageChannelWrapper {
233233

234234
private final MessageChannel channel;
235235

spring-integration-core/src/main/java/org/springframework/integration/channel/QueueChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected Message<?> doReceive(long timeout) {
119119
long nanos = TimeUnit.MILLISECONDS.toNanos(timeout);
120120
long deadline = System.nanoTime() + nanos;
121121
while (this.queue.size() == 0 && nanos > 0) {
122-
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS);
122+
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS); // NOSONAR - ok to ignore result
123123
nanos = deadline - System.nanoTime();
124124
}
125125
return this.queue.poll();

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/ServiceActivatorAnnotationPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected MessageHandler createHandler(Object bean, Method method, List<Annotati
8484
return serviceActivator;
8585
}
8686

87-
private final class ReplyProducingMessageHandlerWrapper extends AbstractReplyProducingMessageHandler
87+
private static final class ReplyProducingMessageHandlerWrapper extends AbstractReplyProducingMessageHandler
8888
implements Lifecycle {
8989

9090
private final MessageHandler target;

spring-integration-core/src/main/java/org/springframework/integration/dsl/StandardIntegrationFlow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public class StandardIntegrationFlow implements IntegrationFlow, SmartLifecycle
3535

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

38-
private final boolean registerComponents = true;
38+
private final boolean registerComponents = true; // NOSONAR
3939

4040
private boolean running;
4141

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

46-
//TODO Figure out some custom DestinationResolver when we don't register singletons
46+
//TODO Figure out some custom DestinationResolver when we don't register singletons - remove NOSONAR above when done
4747
/*public void setRegisterComponents(boolean registerComponents) {
4848
this.registerComponents = registerComponents;
4949
}*/

spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowRegistration.java

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class IntegrationFlowRegistration {
5353
}
5454

5555
void setBeanFactory(ConfigurableListableBeanFactory beanFactory) {
56-
this.beanFactory = beanFactory;
56+
this.beanFactory = beanFactory; // NOSONAR (synchronization)
5757
}
5858

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

7979
public MessageChannel getInputChannel() {
8080
if (this.inputChannel == null) {
81-
synchronized (this) {
82-
if (this.inputChannel == null) {
83-
if (this.integrationFlow instanceof StandardIntegrationFlow) {
84-
StandardIntegrationFlow integrationFlow = (StandardIntegrationFlow) this.integrationFlow;
85-
Object next = integrationFlow.getIntegrationComponents().iterator().next();
86-
if (next instanceof MessageChannel) {
87-
this.inputChannel = (MessageChannel) next;
88-
}
89-
else {
90-
throw new IllegalStateException("The 'IntegrationFlow' [" + integrationFlow + "] " +
91-
"doesn't start with 'MessageChannel' for direct message sending.");
92-
}
93-
}
94-
else {
95-
throw new IllegalStateException("Only 'StandardIntegrationFlow' instances " +
96-
"(e.g. extracted from 'IntegrationFlow' Lambdas) can be used " +
97-
"for direct 'send' operation. " +
98-
"But [" + this.integrationFlow + "] ins't one of them.\n" +
99-
"Consider 'BeanFactory.getBean()' usage for sending messages " +
100-
"to the required 'MessageChannel'.");
101-
}
81+
if (this.integrationFlow instanceof StandardIntegrationFlow) {
82+
StandardIntegrationFlow integrationFlow = (StandardIntegrationFlow) this.integrationFlow;
83+
Object next = integrationFlow.getIntegrationComponents().iterator().next();
84+
if (next instanceof MessageChannel) {
85+
this.inputChannel = (MessageChannel) next;
10286
}
87+
else {
88+
throw new IllegalStateException("The 'IntegrationFlow' [" + integrationFlow + "] " +
89+
"doesn't start with 'MessageChannel' for direct message sending.");
90+
}
91+
}
92+
else {
93+
throw new IllegalStateException("Only 'StandardIntegrationFlow' instances " +
94+
"(e.g. extracted from 'IntegrationFlow' Lambdas) can be used " +
95+
"for direct 'send' operation. " +
96+
"But [" + this.integrationFlow + "] ins't one of them.\n" +
97+
"Consider 'BeanFactory.getBean()' usage for sending messages " +
98+
"to the required 'MessageChannel'.");
10399
}
104100
}
105101
return this.inputChannel;
@@ -115,25 +111,21 @@ public MessageChannel getInputChannel() {
115111
*/
116112
public MessagingTemplate getMessagingTemplate() {
117113
if (this.messagingTemplate == null) {
118-
synchronized (this) {
119-
if (this.messagingTemplate == null) {
120-
this.messagingTemplate = new MessagingTemplate(getInputChannel()) {
121-
122-
@Override
123-
public Message<?> receive() {
124-
return receiveAndConvert(Message.class);
125-
}
126-
127-
@Override
128-
public <T> T receiveAndConvert(Class<T> targetClass) {
129-
throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " +
130-
"isn't supported on the 'IntegrationFlow' input channel.");
131-
}
132-
133-
};
134-
this.messagingTemplate.setBeanFactory(this.beanFactory);
114+
this.messagingTemplate = new MessagingTemplate(getInputChannel()) {
115+
116+
@Override
117+
public Message<?> receive() {
118+
return receiveAndConvert(Message.class);
135119
}
136-
}
120+
121+
@Override
122+
public <T> T receiveAndConvert(Class<T> targetClass) {
123+
throw new UnsupportedOperationException("The 'receive()/receiveAndConvert()' " +
124+
"isn't supported on the 'IntegrationFlow' input channel.");
125+
}
126+
127+
};
128+
this.messagingTemplate.setBeanFactory(this.beanFactory);
137129
}
138130
return this.messagingTemplate;
139131
}

spring-integration-core/src/main/java/org/springframework/integration/store/SimpleMessageStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,13 +278,13 @@ public void addMessagesToGroup(Object groupId, Message<?>... messages) {
278278
throw outOfCapacityException;
279279
}
280280
group = getMessageGroupFactory().create(groupId);
281-
this.groupIdToMessageGroup.putIfAbsent(groupId, group);
281+
this.groupIdToMessageGroup.put(groupId, group);
282282
upperBound = new UpperBound(this.groupCapacity);
283283
for (Message<?> message : messages) {
284284
upperBound.tryAcquire(-1);
285285
group.add(message);
286286
}
287-
this.groupToUpperBound.putIfAbsent(groupId, upperBound);
287+
this.groupToUpperBound.put(groupId, upperBound);
288288
}
289289
else {
290290
upperBound = this.groupToUpperBound.get(groupId);

spring-integration-core/src/main/java/org/springframework/integration/support/converter/SimpleMessageConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Message<?> toMessage(Object object) throws Exception {
141141
}
142142

143143

144-
private class DefaultOutboundMessageMapper implements OutboundMessageMapper<Object> {
144+
private static class DefaultOutboundMessageMapper implements OutboundMessageMapper<Object> {
145145

146146
DefaultOutboundMessageMapper() {
147147
super();

spring-integration-core/src/main/java/org/springframework/integration/util/SimplePool.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,22 @@ public synchronized void setPoolSize(int poolSize) {
122122
* if it was recently reduced and too many items were in use to allow the new size
123123
* to be set.
124124
*/
125-
public int getPoolSize() {
125+
@Override
126+
public synchronized int getPoolSize() {
126127
return this.poolSize.get();
127128
}
128129

130+
@Override
129131
public int getIdleCount() {
130132
return this.available.size();
131133
}
132134

135+
@Override
133136
public int getActiveCount() {
134137
return this.inUse.size();
135138
}
136139

140+
@Override
137141
public int getAllocatedCount() {
138142
return this.allocated.size();
139143
}
@@ -153,6 +157,7 @@ public void setWaitTimeout(long waitTimeout) {
153157
* Obtains an item from the pool; waits up to waitTime milliseconds (default infinity).
154158
* @throws MessagingException if no items become available in time.
155159
*/
160+
@Override
156161
public T getItem() {
157162
boolean permitted = false;
158163
try {
@@ -205,6 +210,7 @@ else if (this.callback.isStale(item)) {
205210
/**
206211
* Returns an item to the pool.
207212
*/
213+
@Override
208214
public synchronized void releaseItem(T item) {
209215
Assert.notNull(item, "Item cannot be null");
210216
Assert.isTrue(this.allocated.contains(item),
@@ -234,6 +240,7 @@ public synchronized void releaseItem(T item) {
234240
}
235241
}
236242

243+
@Override
237244
public synchronized void removeAllIdleItems() {
238245
T item;
239246
while ((item = this.available.poll()) != null) {

spring-integration-http/src/main/java/org/springframework/integration/http/config/IntegrationGraphControllerRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static final class IntegrationGraphCorsConfigurer extends WebMvcConfigur
119119

120120
private final String[] allowedOrigins;
121121

122-
private IntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) {
122+
private IntegrationGraphCorsConfigurer(String path, String[] allowedOrigins) { // NOSONAR
123123
this.path = path;
124124
this.allowedOrigins = allowedOrigins;
125125
}

0 commit comments

Comments
 (0)