From 48a9fdbd8da734f8e5bfb3cd38295ee315dd7fd2 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Wed, 12 Sep 2018 20:26:38 +0900 Subject: Fix use try-with-resources issues in asdctool Fix sonar issues in ArtifactValidatorExecuter.java,ExportImportTitanServlet.java Issue-ID: SDC-1672 Change-Id: I39a6376ea61de135bfb824e121adff37b631fdf2 Signed-off-by: Parshad Patel --- .../executers/ArtifactValidatorExecuter.java | 18 ++++------- .../servlets/ExportImportTitanServlet.java | 35 ++++++++-------------- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java index 089e9729d4..34696b33eb 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java @@ -44,7 +44,7 @@ public class ArtifactValidatorExecuter{ Map> result = new HashMap<>(); Either, TitanOperationStatus> resultsEither = titanDao.getByCriteria(type, hasProps); if (resultsEither.isRight()) { - System.out.println("getVerticesToValidate failed "+ resultsEither.right().value()); + log.error("getVerticesToValidate failed "+ resultsEither.right().value()); return result; } System.out.println("getVerticesToValidate: "+resultsEither.left().value().size()+" vertices to scan"); @@ -62,7 +62,7 @@ public class ArtifactValidatorExecuter{ Either toscaElement = toscaOperationFacade.getToscaElement(vertex.getUniqueId(), filter); if (toscaElement.isRight()) { - System.out.println("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value()); + log.error("getVerticesToValidate: failed to find element"+ vertex.getUniqueId()+" staus is" + toscaElement.right().value()); }else{ compList.add(toscaElement.left().value()); } @@ -76,9 +76,7 @@ public class ArtifactValidatorExecuter{ boolean result = true; long time = System.currentTimeMillis(); String fileName = ValidationConfigManager.getOutputFilePath() + this.getName() + "_"+ time + ".csv"; - Writer writer = null; - try { - writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8")); + try(Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "utf-8"))) { writer.write("name, UUID, invariantUUID, state, version\n"); Collection> collection = vertices.values(); for(List compList: collection ){ @@ -100,15 +98,10 @@ public class ArtifactValidatorExecuter{ } } catch (Exception e) { - log.info("Failed to fetch vf resources ", e); + log.error("Failed to fetch vf resources ", e); return false; } finally { titanDao.commit(); - try { - writer.flush(); - writer.close(); - } catch (Exception ex) { - /* ignore */} } return result; } @@ -124,8 +117,7 @@ public class ArtifactValidatorExecuter{ writer.write(sb.toString()); } } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("Failed to write module result to file ", e); } } diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java index c1f9335d59..31b1b1f07a 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java @@ -70,39 +70,30 @@ public class ExportImportTitanServlet { conf.setProperty("storage.machine-id-appendix", System.currentTimeMillis() % 1000); - TitanGraph openGraph = Utils.openGraph(conf); - if (openGraph == null) { - Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null); - return buildErrorResponse; + try(TitanGraph openGraph = Utils.openGraph(conf)){ + + if (openGraph == null) { + Response buildErrorResponse = Utils.buildOkResponse(500, "failed to open graph", null); + return buildErrorResponse; + } + + // Open Titan Graph + + Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null); + + return buildOkResponse; } - - // Open Titan Graph - - Response buildOkResponse = Utils.buildOkResponse(200, "ok man", null); - - return buildOkResponse; } private Properties convertFileToProperties(File titanPropertiesFile) { Properties properties = new Properties(); - FileReader fileReader = null; - try { - fileReader = new FileReader(titanPropertiesFile); + try (FileReader fileReader = new FileReader(titanPropertiesFile)){ properties.load(fileReader); - } catch (Exception e) { log.error("Failed to convert file to properties", e); return null; - } finally { - if (fileReader != null) { - try { - fileReader.close(); - } catch (IOException e) { - log.error("Failed to close file", e); - } - } } return properties; -- cgit 1.2.3-korg