Fix Sonar bugs 61/67761/1
authorConor Ward <conor.ward@ericsson.com>
Wed, 19 Sep 2018 15:07:46 +0000 (15:07 +0000)
committerConor Ward <conor.ward@ericsson.com>
Wed, 19 Sep 2018 15:07:46 +0000 (15:07 +0000)
Change-Id: I76b212bf18e2988cba90869fe078aa922c23b44b
Signed-off-by: Conor Ward <conor.ward@ericsson.com>
Issue-ID: DMAAP-638

datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeServlet.java
datarouter-node/src/main/java/org/onap/dmaap/datarouter/node/NodeUtils.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/InternalServlet.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/Poker.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/PublishServlet.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/StatisticsServlet.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObject.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/utils/URLUtilities.java
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/FeedReport.java
datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java

index 9ddbc25..5f85139 100644 (file)
@@ -69,18 +69,15 @@ public class NodeServlet extends HttpServlet {
         .getLogger("org.onap.dmaap.datarouter.node.NodeServlet");
 
     static {
-        try {
-            final String ws = "\\s*";
-            // assume that \\ and \" have been replaced by X
-            final String string = "\"[^\"]*\"";
-            //String string = "\"(?:[^\"\\\\]|\\\\.)*\"";
-            final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?";
-            final String value = "(?:" + string + "|" + number + "|null|true|false)";
-            final String item = string + ws + ":" + ws + value + ws;
-            final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws;
-            MetaDataPattern = Pattern.compile(object, Pattern.DOTALL);
-        } catch (Exception e) {
-        }
+        final String ws = "\\s*";
+        // assume that \\ and \" have been replaced by X
+        final String string = "\"[^\"]*\"";
+        //String string = "\"(?:[^\"\\\\]|\\\\.)*\"";
+        final String number = "[+-]?(?:\\.\\d+|(?:0|[1-9]\\d*)(?:\\.\\d*)?)(?:[eE][+-]?\\d+)?";
+        final String value = "(?:" + string + "|" + number + "|null|true|false)";
+        final String item = string + ws + ":" + ws + value + ws;
+        final String object = ws + "\\{" + ws + "(?:" + item + "(?:" + "," + ws + item + ")*)?\\}" + ws;
+        MetaDataPattern = Pattern.compile(object, Pattern.DOTALL);
     }
 
     /**
index 2c013ca..d5b15bc 100644 (file)
@@ -53,12 +53,6 @@ public class NodeUtils {
     private static EELFLogger eelfLogger = EELFManager.getInstance()
         .getLogger("org.onap.dmaap.datarouter.node.NodeUtils");
     private static Logger nodeUtilsLogger = Logger.getLogger("org.onap.dmaap.datarouter.node.NodeUtils");
-    private static SimpleDateFormat logDate;
-
-    static {
-        logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
-        logDate.setTimeZone(TimeZone.getTimeZone("GMT"));
-    }
 
     private NodeUtils() {
     }
@@ -241,6 +235,8 @@ public class NodeUtils {
      * Format a logging timestamp as yyyy-mm-ddThh:mm:ss.mmmZ
      */
     public static synchronized String logts(Date when) {
+        SimpleDateFormat logDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
+        logDate.setTimeZone(TimeZone.getTimeZone("GMT"));
         return (logDate.format(when));
     }
 
index c35978b..685d552 100644 (file)
@@ -156,6 +156,7 @@ import static org.onap.dmaap.datarouter.provisioning.utils.HttpServletUtils.send
 @SuppressWarnings("serial")
 public class InternalServlet extends ProxyServlet {
 
+    private static final Object lock = new Object();
     private static Integer logseq = 0; // another piece of info to make log spool file names unique
     //Adding EELF Logger Rally:US664892
     private static EELFLogger eelflogger = EELFManager.getInstance()
@@ -454,7 +455,7 @@ public class InternalServlet extends ProxyServlet {
             }
             String spooldir = (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.spooldir");
             String spoolname = String.format("%d-%d-", System.currentTimeMillis(), Thread.currentThread().getId());
-            synchronized (logseq) {
+            synchronized (lock) {
                 // perhaps unnecessary, but it helps make the name unique
                 spoolname += logseq.toString();
                 logseq++;
index f2c511c..563e6f7 100644 (file)
@@ -64,6 +64,8 @@ public class Poker extends TimerTask {
      */
     private static final String POKE_URL_TEMPLATE = "http://%s/internal/fetchProv";
 
+    private static final Object lock = new Object();
+
     /**
      * This is a singleton -- there is only one Poker object in the server
      */
@@ -109,7 +111,7 @@ public class Poker extends TimerTask {
      * @param t2 the second timer set the outer bound on how long to wait.  It cannot be reset.
      */
     public void setTimers(long t1, long t2) {
-        synchronized (thisPod) {
+        synchronized (lock) {
             if (timer1 == 0 || t1 > timer1) {
                 timer1 = t1;
             }
@@ -144,7 +146,7 @@ public class Poker extends TimerTask {
             if (timer1 > 0) {
                 long now = System.currentTimeMillis();
                 boolean fire = false;
-                synchronized (thisPod) {
+                synchronized (lock) {
                     if (now > timer1 || now > timer2) {
                         timer1 = timer2 = 0;
                         fire = true;
index d500bb3..0550276 100644 (file)
@@ -64,6 +64,7 @@ public class PublishServlet extends BaseServlet {
     private List<IngressRoute> irt;
     //Adding EELF Logger Rally:US664892
     private static EELFLogger eelflogger = EELFManager.getInstance().getLogger("org.onap.dmaap.datarouter.provisioning.PublishServlet");
+    private static final Object lock = new Object();
 
 
     @Override
@@ -138,7 +139,7 @@ public class PublishServlet extends BaseServlet {
         // Check to see if the IRT needs to be updated
         Poker p = Poker.getPoker();
         String s = p.getProvisioningString();
-        synchronized (provstring) {
+        synchronized (lock) {
             if (irt == null || (s.length() != provstring.length()) || !s.equals(provstring)) {
                 // Provisioning string has changed -- update the IRT
                 provstring = s;
index 3902dc6..b77062f 100755 (executable)
@@ -546,10 +546,8 @@ public class StatisticsServlet extends BaseServlet {
       intlogger.debug(filterQuery);\r
       long start = System.currentTimeMillis();\r
       DB db = new DB();\r
-      ResultSet rs = null;\r
       try (Connection conn = db.getConnection()) {\r
-        try (PreparedStatement pst = conn.prepareStatement(filterQuery)) {\r
-          rs = pst.executeQuery();\r
+        try (ResultSet rs = conn.prepareStatement(filterQuery).executeQuery()) {\r
           if (outputType.equals("csv")) {\r
             resp.setContentType("application/octet-stream");\r
             Date date = new Date();\r
index dec3cc1..1518859 100644 (file)
@@ -1446,7 +1446,7 @@ public class LOGJSONObject {
         try {
             return this.toString(0);
         } catch (Exception e) {
-            return null;
+            return "";
         }
     }
 
@@ -1495,7 +1495,7 @@ public class LOGJSONObject {
      */
     @SuppressWarnings("unchecked")
     public static String valueToString(Object value) throws JSONException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             return "null";
         }
         if (value instanceof JSONString) {
@@ -1601,7 +1601,7 @@ public class LOGJSONObject {
     @SuppressWarnings("unchecked")
     static final Writer writeValue(Writer writer, Object value,
                                    int indentFactor, int indent) throws JSONException, IOException {
-        if (value == null || value.equals(null)) {
+        if (value == null) {
             writer.write("null");
         } else if (value instanceof LOGJSONObject) {
             ((LOGJSONObject) value).write(writer, indentFactor, indent);
index 4576bd2..d0178fd 100644 (file)
@@ -115,7 +115,7 @@ public class URLUtilities {
     public static String generatePeerLogsURL() {\r
         //Fixes for Itrack ticket - DATARTR-4#Fixing if only one Prov is configured, not to give exception to fill logs.\r
         String peerPodUrl = getPeerPodName();\r
-        if (peerPodUrl.equals("") || peerPodUrl==null) {\r
+        if (peerPodUrl == null || peerPodUrl.equals("")) {\r
             return "";\r
         }\r
 \r
index 4346a0e..fe9c460 100644 (file)
@@ -72,36 +72,12 @@ public class FeedReport extends ReportBase {
            try( PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
                try (ResultSet rs = ps.executeQuery()) {\r
                    while (rs.next()) {\r
-                       if (alg1) {\r
-                           String date = rs.getString("date");\r
-                           String type = rs.getString("type");\r
-                           int feedid = rs.getInt("feedid");\r
-                           int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;\r
-                           int count = rs.getInt("count");\r
-                           sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");\r
-                       } else {\r
-                           String date = rs.getString("date");\r
-                           JSONObject datemap = jo.optJSONObject(date);\r
-                           if (datemap == null) {\r
-                               datemap = new JSONObject();\r
-                               jo.put(date, datemap);\r
-                           }\r
-                           int feed = rs.getInt("FEEDID");\r
-                           JSONObject feedmap = datemap.optJSONObject("" + feed);\r
-                           if (feedmap == null) {\r
-                               feedmap = new JSONObject();\r
-                               feedmap.put("pubcount", 0);\r
-                               datemap.put("" + feed, feedmap);\r
-                           }\r
-                           String type = rs.getString("TYPE");\r
-                           int count = rs.getInt("count");\r
-                           if (type.equals("pub")) {\r
-                               feedmap.put("pubcount", count);\r
-                           } else if (type.equals("del")) {\r
-                               String subid = "" + rs.getInt("DELIVERY_SUBID");\r
-                               feedmap.put(subid, count);\r
-                           }\r
-                       }\r
+                       String date = rs.getString("date");\r
+                       String type = rs.getString("type");\r
+                       int feedid = rs.getInt("feedid");\r
+                       int subid = type.equals("del") ? rs.getInt("delivery_subid") : 0;\r
+                       int count = rs.getInt("count");\r
+                       sb.append(date + "," + type + "," + feedid + "," + subid + "," + count + "\n");\r
                    }\r
                }\r
            }\r
@@ -110,15 +86,9 @@ public class FeedReport extends ReportBase {
             e.printStackTrace();\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
-        try {\r
-            PrintWriter os = new PrintWriter(outfile);\r
-            if (alg1) {\r
-                os.print("date,type,feedid,subid,count\n");\r
-                os.print(sb.toString());\r
-            } else {\r
-                os.println(toHTML(jo));\r
-            }\r
-            os.close();\r
+        try (PrintWriter os = new PrintWriter(outfile)) {\r
+            os.print("date,type,feedid,subid,count\n");\r
+            os.print(sb.toString());\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
         }\r
index 58bc4c4..3842574 100644 (file)
@@ -38,8 +38,8 @@ public class SampleSubscriberServlet extends HttpServlet {
 
   private static Logger logger =
       Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SampleSubscriberServlet");
-  private String outputDirectory;
-  private String basicAuth;
+  private static String outputDirectory;
+  private static String basicAuth;
 
   /**
    * Configure the SampleSubscriberServlet.