Fixed Sonar issues in SubscriberReport.java 11/63811/1
authorAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 13:06:54 +0000 (18:36 +0530)
committerAmaresh Kumar <kamaresh@in.ibm.com>
Thu, 30 Aug 2018 13:07:06 +0000 (18:37 +0530)
Fixed 5 blocker issues.
https://sonar.onap.org/project/issues?fileUuids=AWHFaQitEU5FCDfKtnEL&id=org.onap.dmaap.datarouter%3Aparent&open=AWHFaQj0EU5FCDfKtnGN&resolved=false&severities=BLOCKER

Issue-ID: DMAAP-677
Change-Id: I4659b0d81c9c583f4fe06419ee4f685811277894
Signed-off-by: Amaresh Kumar <kamaresh@in.ibm.com>
datarouter-prov/src/main/java/org/onap/dmaap/datarouter/reports/SubscriberReport.java

index e00c394..51beac9 100644 (file)
@@ -98,62 +98,61 @@ public class SubscriberReport extends ReportBase {
     public void run() {\r
         Map<String, Counters> map = new HashMap<String, Counters>();\r
         long start = System.currentTimeMillis();\r
+\r
         try {\r
             DB db = new DB();\r
             @SuppressWarnings("resource")\r
             Connection conn = db.getConnection();\r
-            PreparedStatement ps = conn.prepareStatement(SELECT_SQL);\r
-            ps.setLong(1, from);\r
-            ps.setLong(2, to);\r
-            ResultSet rs = ps.executeQuery();\r
-            while (rs.next()) {\r
-                String date = rs.getString("DATE");\r
-                int sub = rs.getInt("DELIVERY_SUBID");\r
-                int res = rs.getInt("RESULT");\r
-                int count = rs.getInt("COUNT");\r
-                String key = date + "," + sub;\r
-                Counters c = map.get(key);\r
-                if (c == null) {\r
-                    c = new Counters(date, sub);\r
-                    map.put(key, c);\r
+            try(PreparedStatement ps = conn.prepareStatement(SELECT_SQL)) {\r
+                ps.setLong(1, from);\r
+                ps.setLong(2, to);\r
+                try(ResultSet rs = ps.executeQuery()) {\r
+                    while (rs.next()) {\r
+                        String date = rs.getString("DATE");\r
+                        int sub = rs.getInt("DELIVERY_SUBID");\r
+                        int res = rs.getInt("RESULT");\r
+                        int count = rs.getInt("COUNT");\r
+                        String key = date + "," + sub;\r
+                        Counters c = map.get(key);\r
+                        if (c == null) {\r
+                            c = new Counters(date, sub);\r
+                            map.put(key, c);\r
+                        }\r
+                        c.addCounts(res, count);\r
+                    }\r
                 }\r
-                c.addCounts(res, count);\r
             }\r
-            rs.close();\r
-            ps.close();\r
 \r
-            ps = conn.prepareStatement(SELECT_SQL2);\r
-            ps.setLong(1, from);\r
-            ps.setLong(2, to);\r
-            rs = ps.executeQuery();\r
-            while (rs.next()) {\r
-                String date = rs.getString("DATE");\r
-                int sub = rs.getInt("DELIVERY_SUBID");\r
-                int count = rs.getInt("COUNT");\r
-                String key = date + "," + sub;\r
-                Counters c = map.get(key);\r
-                if (c == null) {\r
-                    c = new Counters(date, sub);\r
-                    map.put(key, c);\r
-                }\r
-                c.addDlxCount(count);\r
-            }\r
-            rs.close();\r
-            ps.close();\r
+           try( PreparedStatement ps2 = conn.prepareStatement(SELECT_SQL2)) {\r
+               ps2.setLong(1, from);\r
+               ps2.setLong(2, to);\r
+               try(ResultSet rs2 = ps2.executeQuery()) {\r
+                   while (rs2.next()) {\r
+                       String date = rs2.getString("DATE");\r
+                       int sub = rs2.getInt("DELIVERY_SUBID");\r
+                       int count = rs2.getInt("COUNT");\r
+                       String key = date + "," + sub;\r
+                       Counters c = map.get(key);\r
+                       if (c == null) {\r
+                           c = new Counters(date, sub);\r
+                           map.put(key, c);\r
+                       }\r
+                       c.addDlxCount(count);\r
+                   }\r
+                  }\r
+           }\r
 \r
             db.release(conn);\r
         } catch (SQLException e) {\r
             e.printStackTrace();\r
         }\r
         logger.debug("Query time: " + (System.currentTimeMillis() - start) + " ms");\r
-        try {\r
-            PrintWriter os = new PrintWriter(outfile);\r
+        try (PrintWriter os = new PrintWriter(outfile)){\r
             os.println("date,subid,count100,count200,count300,count400,count500,countminus1,countdlx");\r
             for (String key : new TreeSet<String>(map.keySet())) {\r
                 Counters c = map.get(key);\r
                 os.println(c.toString());\r
             }\r
-            os.close();\r
         } catch (FileNotFoundException e) {\r
             System.err.println("File cannot be written: " + outfile);\r
         }\r