Fix sonar issue in dcaegen2/services/mapper 66/72166/2
authorParshad Patel <pars.patel@samsung.com>
Thu, 8 Nov 2018 05:32:49 +0000 (14:32 +0900)
committerParshad Patel <pars.patel@samsung.com>
Thu, 8 Nov 2018 05:51:35 +0000 (14:51 +0900)
Fix use try-with-resources sonar issue and remove printStackTrace

Issue-ID: DCAEGEN2-938
Change-Id: Ibbb6bb8bb9b099f33617368b3a4cbcf98032c983
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java
snmpmapper/src/main/java/org/onap/dcaegen2/services/mapper/snmpmapper/DAO/MappingFileDAOImpl.java

index c195117..2cb4b8f 100644 (file)
@@ -141,7 +141,7 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered {
        public static void writefile(String retCBSString) {
                //TODO
        }
-       
+
        private static String executecurl(String url) {
 
                LOGGER.info("Running curl command for url:"+url);
@@ -151,29 +151,26 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered {
                String result = null;
                try {
                        p = process.start();
-                       InputStreamReader ipr = new InputStreamReader(p.getInputStream());
-                       BufferedReader reader = new BufferedReader(ipr);
-                       StringBuilder builder = new StringBuilder();
-                       String line;
+                       try(InputStreamReader ipr = new InputStreamReader(p.getInputStream());
+                               BufferedReader reader = new BufferedReader(ipr)){
+                               StringBuilder builder = new StringBuilder();
+                               String line;
 
-                       while ((line = reader.readLine()) != null) {
-                               builder.append(line);
+                               while ((line = reader.readLine()) != null) {
+                                   builder.append(line);
+                               }
+                               result = builder.toString();
+                               LOGGER.info(result);
                        }
-                       result = builder.toString();
-                       LOGGER.info(result);
-
-                       reader.close();
-                       ipr.close();
                } catch (IOException e) {
                        LOGGER.error("error", e);
-                       e.printStackTrace();
                }
                return result;
 
        }
 
        public void fetchMappingFile() {
-               
+
                try (Connection con = DriverManager.getConnection(dBurl, user, pwd);PreparedStatement pstmt = con.prepareStatement("SELECT * FROM mapping_file");ResultSet rs = pstmt.executeQuery()) {
                        LOGGER.info("Retrieving data from DB");
                        // parsing the column each time is a linear search
@@ -191,88 +188,93 @@ public class VESAdapterInitializer implements CommandLineRunner, Ordered {
                        LOGGER.info("DB Initialization Completed, Total # Mappingfiles are" + mappingFiles.size());
                } catch (Exception e) {
                        LOGGER.error("Error occured due to :" + e.getMessage());
-                       e.printStackTrace();
                }
 
        }
-       
-       
-        private void prepareDatabase() throws IOException {       
-                
-     
-         LOGGER.info("The Default Mapping file Location:"+defaultMappingFileLocation.trim());
-                
-                if(ClassLoader.getSystemResource(defaultMappingFileLocation.trim())==null){
-                        LOGGER.error("Default mapping file " + defaultMappingFileLocation.trim() + " is missing");
-                 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));
-                }
-                
-                File file = new File(ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
-             FileInputStream fileInputStream=null;
-             
-                try 
-                {
-                bytesArray = new byte[(int) file.length()];
-                fileInputStream = new FileInputStream(file);
-             fileInputStream.read(bytesArray);
-                     
-          } catch (IOException e1) {
-           LOGGER.error("Exception Occured while reading the default mapping file ,Cause: " + e1.getMessage(), e1);                    
-            //exit on missing default mapping file 
-            System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped due to missing default mapping file");return-1;}));      
-           }finally {
-                  try {
-                                       fileInputStream.close();
-                               }catch (IOException e) {
-                                       LOGGER.error("Exception while closing file inputstream" + e.getMessage(), e);
-                               }
+
+
+    private void prepareDatabase() throws IOException {
+
+
+        LOGGER.info("The Default Mapping file Location:" + defaultMappingFileLocation.trim());
+
+        if (ClassLoader.getSystemResource(defaultMappingFileLocation.trim()) == null) {
+            LOGGER.error(
+                    "Default mapping file " + defaultMappingFileLocation.trim() + " is missing");
+            System.exit(SpringApplication.exit(applicationContext, () -> {
+                LOGGER.error("Application stoped due to missing default mapping file");
+                return -1;
+            }));
+        }
+
+        File file = new File(
+                ClassLoader.getSystemResource(defaultMappingFileLocation.trim()).getFile());
+
+        try (FileInputStream fileInputStream = new FileInputStream(file)) {
+            bytesArray = new byte[(int) file.length()];
+            fileInputStream.read(bytesArray);
+
+        } catch (IOException e1) {
+            LOGGER.error("Exception Occured while reading the default mapping file ,Cause: "
+                    + e1.getMessage(), e1);
+            // exit on missing default mapping file
+            System.exit(SpringApplication.exit(applicationContext, () -> {
+                LOGGER.error("Application stoped due to missing default mapping file");
+                return -1;
+            }));
+        }
+
+        try (Connection con = DriverManager.getConnection(dBurl, user, pwd);
+                // creating table if not exist
+                PreparedStatement pstmt11 =
+                        con.prepareStatement("CREATE TABLE IF NOT EXISTS public."
+                                + MappingFileTableName + "\r\n" + "(\r\n"
+                                + "    enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n"
+                                + "    mappingfilecontents bytea,\r\n"
+                                + "    mimetype character varying COLLATE pg_catalog.\"default\",\r\n"
+                                + "    file_name character varying COLLATE pg_catalog.\"default\",\r\n"
+                                + "    CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n"
+                                + ")\r\n" + "WITH (\r\n" + "    OIDS = FALSE\r\n" + ")\r\n"
+                                + "TABLESPACE pg_default;")) {
+
+            LOGGER.info("Postgresql Connection successful...");
+            LOGGER.debug("Connection object:" + con.toString());
+
+            pstmt11.executeUpdate();
+            LOGGER.info("CREATE TABLE IF NOT EXISTS executed successfully....");
+
+            if ((bytesArray.length > 0) && (!bytesArray.toString().equals(""))) {
+                LOGGER.debug("2Connection object:" + con.toString());
+                try (PreparedStatement pstmt = con.prepareStatement("INSERT INTO "
+                        + MappingFileTableName
+                        + "(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;")) {
+                    pstmt.setString(1, defaultEnterpriseId);
+                    pstmt.setBytes(2, bytesArray);
+                    pstmt.setString(3, "text/xml");
+                    pstmt.setString(4, file.getName());
+
+                    pstmt.executeUpdate();
+                    LOGGER.info("Made sure that default mapping file is present in table");
+                }
+            } else {
+                LOGGER.error(file.getName() + " is empty");
+                // exit on empty mapping file
+                System.exit(SpringApplication.exit(applicationContext, () -> {
+                    LOGGER.error("Application stoped beacuase default mapping file is empty..");
+                    return -1;
+                }));
             }
-                
-               try (Connection con = DriverManager.getConnection(dBurl, user, pwd)) {
-                     LOGGER.info("Postgresql Connection successful...");
-                     LOGGER.debug("Connection object:"+con.toString());
-              //creating table if not exist
-             PreparedStatement pstmt11=con.prepareStatement("CREATE TABLE IF NOT EXISTS public."+MappingFileTableName+"\r\n" + 
-                     "(\r\n" + 
-                     "    enterpriseid character varying COLLATE pg_catalog.\"default\" NOT NULL,\r\n" + 
-                     "    mappingfilecontents bytea,\r\n" + 
-                     "    mimetype character varying COLLATE pg_catalog.\"default\",\r\n" + 
-                     "    file_name character varying COLLATE pg_catalog.\"default\",\r\n" + 
-                     "    CONSTRAINT mapping_file_pkey5 PRIMARY KEY (enterpriseid)\r\n" + 
-                     ")\r\n" + 
-                     "WITH (\r\n" + 
-                     "    OIDS = FALSE\r\n" + 
-                     ")\r\n" + 
-                     "TABLESPACE pg_default;");
-             pstmt11.executeUpdate();
-             LOGGER.info("CREATE TABLE IF NOT EXISTS executed successfully....");
-             
-             if((bytesArray.length>0)&&(!bytesArray.toString().equals(""))) {
-               LOGGER.debug("2Connection object:"+con.toString());
-                PreparedStatement pstmt=con.prepareStatement("INSERT INTO "+MappingFileTableName+"(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?) ON CONFLICT (enterpriseid) DO NOTHING;");
-                 pstmt.setString(1,defaultEnterpriseId);
-                 pstmt.setBytes(2,bytesArray );
-                 pstmt.setString(3,"text/xml");
-                 pstmt.setString(4, file.getName());
-       
-                 pstmt.executeUpdate();
-                 LOGGER.info("Made sure that default mapping file is present in table");
-             }
-             else {
-                LOGGER.error(file.getName()+" is empty");
-                 //exit on empty mapping file
-                 System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application stoped beacuase default mapping file is empty..");return-1;}));
-             }
-              
-              
-              
-               } catch (SQLException e) {
-                     LOGGER.error("Received exception : " + e.getMessage(), e);
-                     //exit on SqlException
-                     System.exit(SpringApplication.exit(applicationContext, () -> {LOGGER.error("Application Stoped due to ",e.getCause());return-1;}));
-               }
-          
-        }
+
+        } catch (SQLException e) {
+            LOGGER.error("Received exception : " + e.getMessage(), e);
+            // exit on SqlException
+            System.exit(SpringApplication.exit(applicationContext, () -> {
+                LOGGER.error("Application Stoped due to ", e.getCause());
+                return -1;
+            }));
+        }
+
+    }
        public static Map<String, String> getMappingFiles() {
                return mappingFiles;
        }
index 3d18d1d..6bfc269 100644 (file)
@@ -59,7 +59,7 @@ public class MappingFileDAOImpl implements MappingFileDAO {
                        LOGGER.info(entry.getKey() + ":" + entry.getValue());
                }
 
-                       if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) {
+               if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) {
                                //TODO - Add logic to talk to Consul and CBS to get the configuration. For now, we will refer to configuration coming from docker env parameters
                                
                                LOGGER.info(">>>Dynamic configuration to be used");
@@ -88,10 +88,10 @@ public class MappingFileDAOImpl implements MappingFileDAO {
                
                
                
-               try (Connection con = DriverManager.getConnection(url, user, pwd)) {
+               try (Connection con = DriverManager.getConnection(url, user, pwd);
+                       PreparedStatement pstmt = con.prepareStatement(
+                           "INSERT INTO mapping_file(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?)")) {
                        LOGGER.debug("Connection established successfully");
-                       PreparedStatement pstmt = con.prepareStatement(
-                                       "INSERT INTO mapping_file(enterpriseid, mappingfilecontents, mimetype,  File_Name) VALUES (?, ?, ?, ?)");
 
                        pstmt.setString(1, enterpriseid);
                        pstmt.setBytes(2, mappingFile.getBytes());
@@ -102,7 +102,6 @@ public class MappingFileDAOImpl implements MappingFileDAO {
 
                }catch (Exception e) {
                        LOGGER.error("Error occured due to :" + e.getMessage());
-                       e.printStackTrace();
                }
                return "Uploaded successfully";