Address more sonar issues in drools-pdp
Addressed the following sonar issues:
- use of "synchronized"
- use Files.delete() instead of File.delete()
Issue-ID: POLICY-2305
Change-Id: Id55628fe12d9d764616e57321382a70cb5704ba1
Signed-off-by: Jim Hahn <jrh3@att.com>
diff --git a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
index bf6b2e7..8f33f92 100644
--- a/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
+++ b/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/RepositoryAudit.java
@@ -522,19 +522,26 @@
private final class RecursivelyDeleteDirectory extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
- file.toFile().delete();
- return FileVisitResult.CONTINUE;
+ return deletePath("file", file);
}
@Override
public FileVisitResult postVisitDirectory(Path file, IOException ex) throws IOException {
if (ex == null) {
- file.toFile().delete();
- return FileVisitResult.CONTINUE;
+ return deletePath("directory", file);
} else {
throw ex;
}
}
+
+ private FileVisitResult deletePath(String type, Path file) {
+ try {
+ Files.delete(file);
+ } catch (IOException e) {
+ logger.warn("failed to delete {} {}", type, file, e);
+ }
+ return FileVisitResult.CONTINUE;
+ }
}
/* ============================================================ */
diff --git a/policy-core/src/main/java/org/onap/policy/drools/core/lock/LockImpl.java b/policy-core/src/main/java/org/onap/policy/drools/core/lock/LockImpl.java
index 9596dbe..8866eb8 100644
--- a/policy-core/src/main/java/org/onap/policy/drools/core/lock/LockImpl.java
+++ b/policy-core/src/main/java/org/onap/policy/drools/core/lock/LockImpl.java
@@ -100,13 +100,15 @@
* This method always succeeds, unless the lock is already unavailable.
*/
@Override
- public synchronized boolean free() {
- if (isUnavailable()) {
- return false;
- }
+ public boolean free() {
+ synchronized (this) {
+ if (isUnavailable()) {
+ return false;
+ }
- logger.info("releasing lock: {}", this);
- setState(LockState.UNAVAILABLE);
+ logger.info("releasing lock: {}", this);
+ setState(LockState.UNAVAILABLE);
+ }
return true;
}
diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java
index 6b0c8a4..799cca7 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/FeatureLockImpl.java
@@ -119,7 +119,7 @@
* @return {@code true} if the lock can be freed, {@code false} if the lock is
* unavailable
*/
- protected boolean freeAllowed() {
+ protected synchronized boolean freeAllowed() {
// do a quick check of the state
if (isUnavailable()) {
return false;
@@ -150,7 +150,7 @@
* @return {@code true} if the lock can be extended, {@code false} if the lock is
* unavailable
*/
- protected boolean extendAllowed(int holdSec, LockCallback callback) {
+ protected synchronized boolean extendAllowed(int holdSec, LockCallback callback) {
if (holdSec < 0) {
throw new IllegalArgumentException("holdSec is negative");
}
diff --git a/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java b/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java
index 1df139e..c2c921f 100644
--- a/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java
+++ b/policy-management/src/main/java/org/onap/policy/drools/system/internal/SimpleLockManager.java
@@ -227,7 +227,7 @@
}
@Override
- public synchronized boolean free() {
+ public boolean free() {
if (!freeAllowed()) {
return false;
}