Skip to content

Commit a9cb80a

Browse files
committed
Fix some Sonar smells
1 parent bfe28db commit a9cb80a

File tree

4 files changed

+166
-148
lines changed

4 files changed

+166
-148
lines changed

spring-integration-core/src/main/java/org/springframework/integration/handler/DelayHandler.java

+15-11
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,25 @@ else if (delayValue != null) {
385385
}
386386
}
387387
if (delayValueException != null) {
388-
if (this.ignoreExpressionFailures) {
389-
if (logger.isDebugEnabled()) {
390-
logger.debug("Failed to get delay value from 'delayExpression': " +
391-
delayValueException.getMessage() +
392-
". Will fall back to default delay: " + this.defaultDelay);
393-
}
394-
}
395-
else {
396-
throw new IllegalStateException("Error occurred during 'delay' value determination",
397-
delayValueException);
398-
}
388+
handleDelayValueException(delayValueException);
399389
}
400390
return delay;
401391
}
402392

393+
private void handleDelayValueException(Exception delayValueException) {
394+
if (this.ignoreExpressionFailures) {
395+
if (logger.isDebugEnabled()) {
396+
logger.debug("Failed to get delay value from 'delayExpression': " +
397+
delayValueException.getMessage() +
398+
". Will fall back to default delay: " + this.defaultDelay);
399+
}
400+
}
401+
else {
402+
throw new IllegalStateException("Error occurred during 'delay' value determination",
403+
delayValueException);
404+
}
405+
}
406+
403407
private void releaseMessageAfterDelay(final Message<?> message, long delay) {
404408
Message<?> delayedMessage = message;
405409

spring-integration-core/src/main/java/org/springframework/integration/router/AbstractMappingMessageRouter.java

+33-28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Properties;
2828
import java.util.Set;
2929

30+
import org.springframework.core.convert.ConversionService;
3031
import org.springframework.integration.support.management.MappingMessageRouterManagement;
3132
import org.springframework.jmx.export.annotation.ManagedAttribute;
3233
import org.springframework.jmx.export.annotation.ManagedOperation;
@@ -292,37 +293,41 @@ private void addToCollection(Collection<MessageChannel> channels, Collection<?>
292293
return;
293294
}
294295
for (Object channelKey : channelKeys) {
295-
if (channelKey instanceof MessageChannel) {
296-
channels.add((MessageChannel) channelKey);
297-
}
298-
else if (channelKey instanceof MessageChannel[]) {
299-
channels.addAll(Arrays.asList((MessageChannel[]) channelKey));
300-
}
301-
else if (channelKey instanceof String) {
302-
addChannelFromString(channels, (String) channelKey, message);
303-
}
304-
else if (channelKey instanceof Class) {
305-
addChannelFromString(channels, ((Class<?>) channelKey).getName(), message);
306-
}
307-
else if (channelKey instanceof String[]) {
308-
for (String indicatorName : (String[]) channelKey) {
309-
addChannelFromString(channels, indicatorName, message);
310-
}
311-
}
312-
else if (channelKey instanceof Collection) {
313-
addToCollection(channels, (Collection<?>) channelKey, message);
314-
}
315-
else if (channelKey != null &&
316-
getRequiredConversionService().canConvert(channelKey.getClass(), String.class)) {
317-
String converted = getConversionService().convert(channelKey, String.class);
318-
if (converted != null) {
319-
addChannelFromString(channels, converted, message);
320-
}
296+
addChannelKeyToCollection(channels, message, channelKey);
297+
}
298+
}
299+
300+
private void addChannelKeyToCollection(Collection<MessageChannel> channels, Message<?> message, Object channelKey) {
301+
ConversionService conversionService = getRequiredConversionService();
302+
if (channelKey instanceof MessageChannel) {
303+
channels.add((MessageChannel) channelKey);
304+
}
305+
else if (channelKey instanceof MessageChannel[]) {
306+
channels.addAll(Arrays.asList((MessageChannel[]) channelKey));
307+
}
308+
else if (channelKey instanceof String) {
309+
addChannelFromString(channels, (String) channelKey, message);
310+
}
311+
else if (channelKey instanceof Class) {
312+
addChannelFromString(channels, ((Class<?>) channelKey).getName(), message);
313+
}
314+
else if (channelKey instanceof String[]) {
315+
for (String indicatorName : (String[]) channelKey) {
316+
addChannelFromString(channels, indicatorName, message);
321317
}
322-
else if (channelKey != null) {
323-
throw new MessagingException("unsupported return type for router [" + channelKey.getClass() + "]");
318+
}
319+
else if (channelKey instanceof Collection) {
320+
addToCollection(channels, (Collection<?>) channelKey, message);
321+
}
322+
else if (channelKey != null && conversionService.canConvert(channelKey.getClass(), String.class)) {
323+
String converted = conversionService.convert(channelKey, String.class);
324+
if (converted != null) {
325+
addChannelFromString(channels, converted, message);
324326
}
325327
}
328+
else if (channelKey != null) {
329+
throw new MessagingException("unsupported return type for router [" + channelKey.getClass() + "]");
330+
}
326331
}
327332

328333
}

spring-integration-core/src/main/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiator.java

+50-43
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -391,50 +391,9 @@ else if (acquired) {
391391
}
392392
}
393393
catch (Exception e) {
394-
if (this.locked) {
395-
this.locked = false;
396-
try {
397-
this.lock.unlock();
398-
}
399-
catch (Exception e1) {
400-
logger.debug("Could not unlock - treat as broken " + this.context +
401-
". Revoking " + (isRunning() ? " and retrying..." : "..."), e1);
402-
403-
}
404-
// The lock was broken and we are no longer leader
405-
handleRevoked();
406-
}
407-
408-
if (e instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
409-
Thread.currentThread().interrupt();
410-
if (isRunning()) {
411-
logger.warn("Restarting LeaderSelector for " + this.context + " because of error.", e);
412-
LockRegistryLeaderInitiator.this.future =
413-
LockRegistryLeaderInitiator.this.executorService.submit(
414-
() -> {
415-
// Give it a chance to elect some other leader.
416-
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
417-
return call();
418-
});
419-
}
394+
if (handleLockException(e)) {
420395
return null;
421396
}
422-
else {
423-
if (isRunning()) {
424-
// Give it a chance to elect some other leader.
425-
try {
426-
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
427-
}
428-
catch (InterruptedException e1) {
429-
// Ignore interruption and let it to be caught on the next cycle.
430-
Thread.currentThread().interrupt();
431-
}
432-
}
433-
if (logger.isDebugEnabled()) {
434-
logger.debug("Error acquiring the lock for " + this.context +
435-
". " + (isRunning() ? "Retrying..." : ""), e);
436-
}
437-
}
438397
}
439398
}
440399
}
@@ -455,6 +414,54 @@ else if (acquired) {
455414
return null;
456415
}
457416

417+
private boolean handleLockException(Exception e) {
418+
if (this.locked) {
419+
this.locked = false;
420+
try {
421+
this.lock.unlock();
422+
}
423+
catch (Exception e1) {
424+
logger.debug("Could not unlock - treat as broken " + this.context +
425+
". Revoking " + (isRunning() ? " and retrying..." : "..."), e1);
426+
427+
}
428+
// The lock was broken and we are no longer leader
429+
handleRevoked();
430+
}
431+
432+
if (e instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
433+
Thread.currentThread().interrupt();
434+
if (isRunning()) {
435+
logger.warn("Restarting LeaderSelector for " + this.context + " because of error.", e);
436+
LockRegistryLeaderInitiator.this.future =
437+
LockRegistryLeaderInitiator.this.executorService.submit(
438+
() -> {
439+
// Give it a chance to elect some other leader.
440+
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
441+
return call();
442+
});
443+
}
444+
return true;
445+
}
446+
else {
447+
if (isRunning()) {
448+
// Give it a chance to elect some other leader.
449+
try {
450+
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
451+
}
452+
catch (InterruptedException e1) {
453+
// Ignore interruption and let it to be caught on the next cycle.
454+
Thread.currentThread().interrupt();
455+
}
456+
}
457+
if (logger.isDebugEnabled()) {
458+
logger.debug("Error acquiring the lock for " + this.context +
459+
". " + (isRunning() ? "Retrying..." : ""), e);
460+
}
461+
}
462+
return false;
463+
}
464+
458465
public boolean isLeader() {
459466
return this.locked;
460467
}

0 commit comments

Comments
 (0)