From 8b580939e18d541c89b32256d99dc08aecae7809 Mon Sep 17 00:00:00 2001 From: "r.bogacki" Date: Tue, 12 Feb 2019 10:19:11 +0100 Subject: Fixed Sonar issues in ZkStatelessLockService Fixed potential NullPointerException. Added restoring of the interrupted state. Removed unnecessary imports. Added missing loggers. Change-Id: I62bb8ef2b4893c60c1cea7dcd4ba29f2908b4d05 Issue-ID: MUSIC-329 Signed-off-by: Robert Bogacki --- .../zookeeper/ZkStatelessLockService.java | 38 +++++++++++++--------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java index 38c4726f..e8ed257a 100644 --- a/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java +++ b/src/main/java/org/onap/music/lockingservice/zookeeper/ZkStatelessLockService.java @@ -3,7 +3,8 @@ * =================================================================== * Copyright (c) 2017 AT&T Intellectual Property * =================================================================== - * Modifications Copyright (c) 2018 IBM. + * Modifications Copyright (c) 2018 IBM. + * Modifications Copyright (c) 2019 Samsung. * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at @@ -39,7 +40,6 @@ import org.onap.music.eelf.logging.format.ErrorSeverity; import org.onap.music.eelf.logging.format.ErrorTypes; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; -import org.onap.music.service.impl.MusicZKCore; import com.datastax.driver.core.DataType; @@ -127,6 +127,7 @@ public class ZkStatelessLockService extends ProtocolSupport { } }catch (InterruptedException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); + Thread.currentThread().interrupt(); }catch (KeeperException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); } @@ -144,6 +145,7 @@ public class ZkStatelessLockService extends ProtocolSupport { retryOperation(zop); }catch (InterruptedException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); + Thread.currentThread().interrupt(); }catch (KeeperException e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); } @@ -303,20 +305,26 @@ public class ZkStatelessLockService extends ProtocolSupport { Stat stat = null; try { stat = zookeeper.exists(id, false); - } catch (KeeperException | InterruptedException e1) { - e1.printStackTrace(); + } catch (InterruptedException e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.EXECUTIONINTERRUPTED, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); + Thread.currentThread().interrupt(); + } catch (KeeperException e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.KEEPERERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); } - Long ctime = stat.getCtime(); - MusicUtil.zkNodeMap.put(id, ctime); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)"); - try { - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime)); - MusicCore.eventualPut(pQuery); - } catch (Exception e) { - e.printStackTrace(); + + if (stat != null){ + Long ctime = stat.getCtime(); + MusicUtil.zkNodeMap.put(id, ctime); + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "INSERT INTO admin.locks(lock_id, ctime) VALUES (?,?)"); + try { + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), id)); + pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), ctime)); + MusicCore.eventualPut(pQuery); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.UNKNOWN); + } } break; } -- cgit 1.2.3-korg