More sonar issues in common 64/105164/2
authorJim Hahn <jrh3@att.com>
Mon, 6 Apr 2020 15:26:13 +0000 (11:26 -0400)
committerJim Hahn <jrh3@att.com>
Mon, 6 Apr 2020 15:41:32 +0000 (11:41 -0400)
Fixed additional sonar issues:
- infinit loop; while the issue is bogus, it was easy enough to
  modify the code to satisfy sonar
- doesn't like "volatile"; again, the issue is bogus, but easy enough
  to modify the code

Disabled a couple of sonars in NetworkUtil, as they are not actually
an issue.

Issue-ID: POLICY-2305
Change-Id: I5500183e3fe4060696994cff55bdae4ba7e138c7
Signed-off-by: Jim Hahn <jrh3@att.com>
integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/http/server/internal/JettyServletServer.java
utils/src/main/java/org/onap/policy/common/utils/network/NetworkUtil.java

index 2533554..accef64 100644 (file)
@@ -169,7 +169,9 @@ public class AuditThread extends Thread {
         IntegrityAuditEntity thisEntity;
         integrityAudit.setThreadInitialized(true); // An exception will set it to false
 
-        while (true) {
+        boolean interrupted = false;
+
+        while (!interrupted) {
             try {
                 /*
                  * It may have been awhile since we last cycled through this loop, so refresh
@@ -208,16 +210,16 @@ public class AuditThread extends Thread {
                 if (isInterruptedException(e)) {
                     String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage() + "; Stopping.";
                     logger.error(MessageCodes.EXCEPTION_ERROR, e, msg);
-                    break;
-                }
+                    interrupted = true;
 
-                String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage()
-                        + "; Will try audit again in " + integrityAuditPeriodSeconds + " seconds";
-                logger.error(MessageCodes.EXCEPTION_ERROR, e, msg);
-                // Sleep and try again later
-                AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L);
+                } else {
+                    String msg = "AuditThread.run loop - Exception thrown: " + e.getMessage()
+                                    + "; Will try audit again in " + integrityAuditPeriodSeconds + " seconds";
+                    logger.error(MessageCodes.EXCEPTION_ERROR, e, msg);
+                    // Sleep and try again later
+                    AuditorTime.getInstance().sleep(integrityAuditPeriodSeconds * 1000L);
+                }
             }
-
         }
     }
 
index f7f2fc1..6825f07 100644 (file)
@@ -115,7 +115,7 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable
     /**
      * Jetty thread.
      */
-    protected volatile Thread jettyThread;
+    protected Thread jettyThread;
 
     /**
      * Start condition.
@@ -418,12 +418,13 @@ public abstract class JettyServletServer implements HttpServletServer, Runnable
 
         this.stop();
 
-        if (this.jettyThread == null) {
-            return;
+        Thread jettyThreadCopy;
+        synchronized (this) {
+            if ((jettyThreadCopy = this.jettyThread) == null) {
+                return;
+            }
         }
 
-        Thread jettyThreadCopy = this.jettyThread;
-
         if (jettyThreadCopy.isAlive()) {
             try {
                 jettyThreadCopy.join(2000L);
index 1436262..9a3d455 100644 (file)
@@ -105,7 +105,11 @@ public class NetworkUtil {
      * @throws IOException if a socket cannot be created
      */
     public static int allocPort(InetSocketAddress hostAddr) throws IOException {
-        try (ServerSocket socket = new ServerSocket()) {
+        /*
+         * The socket is only used to find an unused address for a new server. As a
+         * result, it poses no security risk, thus the sonar issue can be ignored.
+         */
+        try (ServerSocket socket = new ServerSocket()) {    // NOSONAR
             socket.bind(hostAddr);
 
             return socket.getLocalPort();
@@ -134,7 +138,11 @@ public class NetworkUtil {
             throws InterruptedException {
         int retry = 0;
         while (retry < retries) {
-            try (Socket s = new Socket(host, port)) {
+            /*
+             * As with the server socket, this is only used to see if the port is open,
+             * thus the sonar issue can be ignored.
+             */
+            try (Socket s = new Socket(host, port)) {   // NOSONAR
                 logger.debug("{}:{} connected - retries={} interval={}", host, port, retries, interval);
                 return true;
             } catch (final IOException e) {