Fix use try-with-resources issues 85/57785/2
authorParshad Patel <pars.patel@samsung.com>
Fri, 27 Jul 2018 02:17:36 +0000 (11:17 +0900)
committerParshad Patel <pars.patel@samsung.com>
Fri, 3 Aug 2018 07:07:43 +0000 (16:07 +0900)
Fix in epsdk-analytics

Issue-ID: PORTAL-342
Change-Id: Ia13844c65fa23fbdea01e80091c1f8573cf7ad1f
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/controller/ActionHandler.java
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/model/runtime/ChartD3Helper.java
ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/util/jar/ExtractJar.java

index d87a5b1..e976f14 100644 (file)
@@ -1995,11 +1995,8 @@ public class ActionHandler extends org.onap.portalsdk.analytics.RaptorObject {
         String insertQry = Globals.getDownloadAllInsert();
         
         
-        Connection connection = null;
-        PreparedStatement pst = null;
-        try {
-        connection = DbUtils.getConnection();
-        pst = connection.prepareStatement(insertQry);
+        try(Connection connection = DbUtils.getConnection();
+            PreparedStatement pst = connection.prepareStatement(insertQry)){ 
         if(nvl(emailId).length()>0){
                pst.setInt(1, Integer.parseInt(userId));
                pst.setInt(2, Integer.parseInt(rr.getReportID()));
@@ -2019,8 +2016,6 @@ public class ActionHandler extends org.onap.portalsdk.analytics.RaptorObject {
                connection.commit();
                        }
         }
-        pst.close();
-        connection.close();
         logger.debug(EELFLoggerDelegate.debugLogger, ("Data inserted"));
         } catch (SQLException ex) { 
                throw new RaptorException(ex);
@@ -2028,16 +2023,7 @@ public class ActionHandler extends org.onap.portalsdk.analytics.RaptorObject {
                throw new RaptorException(ex);
            } catch (Exception ex) {
                throw new RaptorException (ex);
-           } finally {
-               try {
-                       if(connection!=null)
-                               connection.close();
-                       if(pst!=null)
-                               pst.close();
-               } catch (SQLException ex) {
-                       throw new RaptorException(ex);
-               }
-        }
+           }
         //DbUtils.commitTransaction(connection);
         //DbUtils.clearConnection(connection);
         
index ec83ffb..60c45b4 100644 (file)
@@ -3179,10 +3179,8 @@ public class ChartD3Helper {
                                
                        }
                }
-               try {
-                       BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
+               try(BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"))) {
                        out.write(wholeScript.toString());
-                       out.close();
                } catch (IOException e) { 
                        e.printStackTrace();
                        System.out.println("Exception ");
index 1235557..b5390c9 100644 (file)
@@ -73,17 +73,18 @@ public class ExtractJar {
         * @throws Exception
         */
        public static void readJar(File jarFile) throws Exception {
-               JarInputStream in = new JarInputStream(new FileInputStream(jarFile));
-               JarEntry je;
-               while ((je = in.getNextJarEntry()) != null) {
-                       if (je.isDirectory() == false) {
-                               if (je.getName().startsWith("org/onap/portalsdk/analytics/config/")) {
-                                       System.out.println(je.getName() + " " + je.getTime());
-
+               try(JarInputStream in = new JarInputStream(new FileInputStream(jarFile)))
+               {
+                       JarEntry je;
+                       while ((je = in.getNextJarEntry()) != null) {
+                               if (je.isDirectory() == false) {
+                                       if (je.getName().startsWith("org/onap/portalsdk/analytics/config/")) {
+                                               System.out.println(je.getName() + " " + je.getTime());
+       
+                                       }
                                }
                        }
                }
-               in.close();
        }
 
        /**
@@ -95,37 +96,37 @@ public class ExtractJar {
                Class clazz = ExtractJar.class;
                URL jarUrl = clazz.getProtectionDomain().getCodeSource().getLocation();
 
-               JarInputStream entryStream = new JarInputStream(jarUrl.openStream());
-               JarEntry entry;
-
-               while (true) {
-                       entry = entryStream.getNextJarEntry();
-                       if (entry == null)
-                               break;
-                       if (entry.getName().indexOf("jarutil") < 0) {
-                               System.out.println(entry.getName());
-                               File file = new File(directory, entry.getName());
-                               if (entry.isDirectory()) {
-                                       if (!file.exists())
-                                               file.mkdirs();
-                               } else {
-                                       File dir = new File(file.getParent());
-                                       if (!dir.exists())
-                                               dir.mkdirs();
-                                       if (file.exists())
-                                               file.delete();
-                                       FileOutputStream fout = new FileOutputStream(file);
-                                       copy(entryStream, fout);
-                                       fout.close();
-
-                                       if (entry.getTime() >= 0)
-                                               file.setLastModified(entry.getTime());
+               try(JarInputStream entryStream = new JarInputStream(jarUrl.openStream())){
+                       JarEntry entry;
+       
+                       while (true) {
+                               entry = entryStream.getNextJarEntry();
+                               if (entry == null)
+                                       break;
+                               if (entry.getName().indexOf("jarutil") < 0) {
+                                       System.out.println(entry.getName());
+                                       File file = new File(directory, entry.getName());
+                                       if (entry.isDirectory()) {
+                                               if (!file.exists())
+                                                       file.mkdirs();
+                                       } else {
+                                               File dir = new File(file.getParent());
+                                               if (!dir.exists())
+                                                       dir.mkdirs();
+                                               if (file.exists())
+                                                       file.delete();
+                                               try(FileOutputStream fout = new FileOutputStream(file)){
+                                                       copy(entryStream, fout);
+                                               }
+       
+                                               if (entry.getTime() >= 0)
+                                                       file.setLastModified(entry.getTime());
+                                       }
+       
                                }
-
+                               entryStream.closeEntry();
                        }
-                       entryStream.closeEntry();
                }
-               entryStream.close();
                System.out.println("/WEB-INF/classes/org/onap/portalsdk/analytics");
                System.out.println("Delete .... ");