Fix use try-with-resources issues in asdctool 85/66085/4
authorParshad Patel <pars.patel@samsung.com>
Wed, 12 Sep 2018 11:26:38 +0000 (20:26 +0900)
committerMichael Lando <ml636r@att.com>
Tue, 18 Sep 2018 15:47:14 +0000 (15:47 +0000)
Fix sonar issues in ArtifactValidatorExecuter.java,ExportImportTitanServlet.java

Issue-ID: SDC-1672
Change-Id: I39a6376ea61de135bfb824e121adff37b631fdf2
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
asdctool/src/main/java/org/openecomp/sdc/asdctool/impl/validator/executers/ArtifactValidatorExecuter.java
asdctool/src/main/java/org/openecomp/sdc/asdctool/servlets/ExportImportTitanServlet.java

index 089e972..34696b3 100644 (file)
@@ -44,7 +44,7 @@ public class ArtifactValidatorExecuter{
                   Map<String, List<Component>> result = new HashMap<>();
                Either<List<GraphVertex>, 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<Component, StorageOperationStatus> 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<List<Component>> collection = vertices.values();
                        for(List<Component> 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);
                        }
                }
 
index c1f9335..31b1b1f 100644 (file)
@@ -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;