Skip to content

Commit e70fd0d

Browse files
committed
Fix new Sonar smells
1 parent a9cb80a commit e70fd0d

File tree

3 files changed

+65
-57
lines changed

3 files changed

+65
-57
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ private void addToCollection(Collection<MessageChannel> channels, Collection<?>
293293
return;
294294
}
295295
for (Object channelKey : channelKeys) {
296-
addChannelKeyToCollection(channels, message, channelKey);
296+
if (channelKey != null) {
297+
addChannelKeyToCollection(channels, message, channelKey);
298+
}
297299
}
298300
}
299301

@@ -319,13 +321,13 @@ else if (channelKey instanceof String[]) {
319321
else if (channelKey instanceof Collection) {
320322
addToCollection(channels, (Collection<?>) channelKey, message);
321323
}
322-
else if (channelKey != null && conversionService.canConvert(channelKey.getClass(), String.class)) {
324+
else if (conversionService.canConvert(channelKey.getClass(), String.class)) {
323325
String converted = conversionService.convert(channelKey, String.class);
324326
if (converted != null) {
325327
addChannelFromString(channels, converted, message);
326328
}
327329
}
328-
else if (channelKey != null) {
330+
else {
329331
throw new MessagingException("unsupported return type for router [" + channelKey.getClass() + "]");
330332
}
331333
}

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

+53-47
Original file line numberDiff line numberDiff line change
@@ -353,42 +353,7 @@ public Void call() {
353353
try {
354354
while (isRunning()) {
355355
try {
356-
357-
if (logger.isDebugEnabled()) {
358-
logger.debug("Acquiring the lock for " + this.context);
359-
}
360-
361-
// We always try to acquire the lock, in case it expired
362-
boolean acquired = this.lock.tryLock(LockRegistryLeaderInitiator.this.heartBeatMillis,
363-
TimeUnit.MILLISECONDS);
364-
if (!this.locked) {
365-
if (acquired) {
366-
// Success: we are now leader
367-
this.locked = true;
368-
handleGranted();
369-
}
370-
else if (isPublishFailedEvents()) {
371-
publishFailedToAcquire();
372-
}
373-
}
374-
else if (acquired) {
375-
// If we were able to acquire it but we were already locked we
376-
// should release it
377-
this.lock.unlock();
378-
if (isRunning()) {
379-
// Give it a chance to expire.
380-
Thread.sleep(LockRegistryLeaderInitiator.this.heartBeatMillis);
381-
}
382-
}
383-
else {
384-
this.locked = false;
385-
// We were not able to acquire it, therefore not leading any more
386-
handleRevoked();
387-
if (isRunning()) {
388-
// Try again quickly in case the lock holder dropped it
389-
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
390-
}
391-
}
356+
tryAcquireLock();
392357
}
393358
catch (Exception e) {
394359
if (handleLockException(e)) {
@@ -414,7 +379,44 @@ else if (acquired) {
414379
return null;
415380
}
416381

417-
private boolean handleLockException(Exception e) {
382+
private void tryAcquireLock() throws InterruptedException {
383+
if (logger.isDebugEnabled()) {
384+
logger.debug("Acquiring the lock for " + this.context);
385+
}
386+
// We always try to acquire the lock, in case it expired
387+
boolean acquired = this.lock.tryLock(LockRegistryLeaderInitiator.this.heartBeatMillis,
388+
TimeUnit.MILLISECONDS);
389+
if (!this.locked) {
390+
if (acquired) {
391+
// Success: we are now leader
392+
this.locked = true;
393+
handleGranted();
394+
}
395+
else if (isPublishFailedEvents()) {
396+
publishFailedToAcquire();
397+
}
398+
}
399+
else if (acquired) {
400+
// If we were able to acquire it but we were already locked we
401+
// should release it
402+
this.lock.unlock();
403+
if (isRunning()) {
404+
// Give it a chance to expire.
405+
Thread.sleep(LockRegistryLeaderInitiator.this.heartBeatMillis);
406+
}
407+
}
408+
else {
409+
this.locked = false;
410+
// We were not able to acquire it, therefore not leading any more
411+
handleRevoked();
412+
if (isRunning()) {
413+
// Try again quickly in case the lock holder dropped it
414+
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
415+
}
416+
}
417+
}
418+
419+
private boolean handleLockException(Exception ex) {
418420
if (this.locked) {
419421
this.locked = false;
420422
try {
@@ -429,17 +431,10 @@ private boolean handleLockException(Exception e) {
429431
handleRevoked();
430432
}
431433

432-
if (e instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
434+
if (ex instanceof InterruptedException || Thread.currentThread().isInterrupted()) {
433435
Thread.currentThread().interrupt();
434436
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-
});
437+
restartSelectorBecauseOfError(ex);
443438
}
444439
return true;
445440
}
@@ -456,12 +451,23 @@ private boolean handleLockException(Exception e) {
456451
}
457452
if (logger.isDebugEnabled()) {
458453
logger.debug("Error acquiring the lock for " + this.context +
459-
". " + (isRunning() ? "Retrying..." : ""), e);
454+
". " + (isRunning() ? "Retrying..." : ""), ex);
460455
}
461456
}
462457
return false;
463458
}
464459

460+
private void restartSelectorBecauseOfError(Exception ex) {
461+
logger.warn("Restarting LeaderSelector for " + this.context + " because of error.", ex);
462+
LockRegistryLeaderInitiator.this.future =
463+
LockRegistryLeaderInitiator.this.executorService.submit(
464+
() -> {
465+
// Give it a chance to elect some other leader.
466+
Thread.sleep(LockRegistryLeaderInitiator.this.busyWaitMillis);
467+
return call();
468+
});
469+
}
470+
465471
public boolean isLeader() {
466472
return this.locked;
467473
}

spring-integration-core/src/main/java/org/springframework/integration/transformer/SyslogToMapTransformer.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ public class SyslogToMapTransformer extends AbstractPayloadTransformer<Object, M
9696

9797
private void parseMatcherToMap(Object payload, Matcher matcher, Map<String, Object> map) {
9898
try {
99-
String facilityString = matcher.group(1);
99+
String facilityString = matcher.group(1); // NOSONAR
100100
int facility = Integer.parseInt(facilityString);
101101
int severity = facility & 0x7;
102102
facility = facility >> 3;
103103
map.put(FACILITY, facility);
104104
map.put(SEVERITY, severity);
105-
String timestamp = matcher.group(2);
105+
String timestamp = matcher.group(2); // NOSONAR
106106
parseTimestampToMap(timestamp, map);
107-
map.put(HOST, matcher.group(3));
108-
String tag = matcher.group(4);
107+
map.put(HOST, matcher.group(3)); // NOSONAR
108+
String tag = matcher.group(4); // NOSONAR
109109
if (StringUtils.hasLength(tag)) {
110110
map.put(TAG, tag);
111111
}
112-
map.put(MESSAGE, matcher.group(5));
112+
map.put(MESSAGE, matcher.group(5)); // NOSONAR
113113
}
114114
catch (Exception e) {
115115
if (logger.isDebugEnabled()) {
@@ -132,10 +132,10 @@ private void parseTimestampToMap(String timestamp, Map<String, Object> map) {
132132
* need to insert the current year - adjusted
133133
* if necessary if close to midnight on Dec 31.
134134
*/
135-
if (month == 11 && calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
135+
if (month == Calendar.DECEMBER && calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
136136
calendar.set(Calendar.YEAR, year + 1);
137137
}
138-
else if (month == 0 && calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) {
138+
else if (month == Calendar.JANUARY && calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) {
139139
calendar.set(Calendar.YEAR, year - 1);
140140
}
141141
else {

0 commit comments

Comments
 (0)