From e4c293eb36dd955685db390d114043569241a855 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Thu, 8 Nov 2018 14:32:49 +0900 Subject: Fix sonar issue in dcaegen2/services/mapper Fix use try-with-resources sonar issue and remove printStackTrace Issue-ID: DCAEGEN2-938 Change-Id: Ibbb6bb8bb9b099f33617368b3a4cbcf98032c983 Signed-off-by: Parshad Patel --- .../service/VESAdapterInitializer.java | 186 +++++++++++---------- 1 file changed, 94 insertions(+), 92 deletions(-) (limited to 'UniversalVesAdapter/src/main') diff --git a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java index c195117..2cb4b8f 100644 --- a/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java +++ b/UniversalVesAdapter/src/main/java/org/onap/universalvesadapter/service/VESAdapterInitializer.java @@ -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 getMappingFiles() { return mappingFiles; } -- cgit 1.2.3-korg