Fixing sonar Exception Handling 81/27081/2
authoravigaffa <avi.gaffa@amdocs.com>
Thu, 28 Dec 2017 07:34:47 +0000 (09:34 +0200)
committerVitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com>
Sun, 31 Dec 2017 06:45:56 +0000 (06:45 +0000)
Change-Id: I0afe103b921ea26de8f701e9a2cff7943ab58110
Issue-ID: SDC-810
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java
openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exceptions/HealingRuntimeException.java [new file with mode: 0644]

index 40a56ed..7868927 100644 (file)
@@ -2,14 +2,13 @@ package org.openecomp.core.tools.Commands;
 
 import org.apache.commons.collections.CollectionUtils;
 import org.openecomp.core.tools.concurrent.ItemHealingTask;
+import org.openecomp.core.tools.exceptions.HealingRuntimeException;
 import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
 import org.openecomp.sdc.healing.api.HealingManager;
 import org.openecomp.sdc.healing.factory.HealingManagerFactory;
 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
-import org.openecomp.sdc.versioning.VersioningManager;
-import org.openecomp.sdc.versioning.VersioningManagerFactory;
 import org.openecomp.sdc.versioning.dao.types.Version;
 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
 
@@ -32,114 +31,95 @@ import java.util.stream.Stream;
  */
 public class HealAll {
 
-  private static final String HEALING_USER = "healing_user";
-  private static final int defaulThreadNumber = 100;
-  private static BufferedWriter log;
-  private static List<ItemHealingTask> tasks = new ArrayList<>();
-  private static VendorSoftwareProductManager vspManager = VspManagerFactory
-      .getInstance().createInterface();
-  private static HealingManager healingManager = HealingManagerFactory.getInstance()
-      .createInterface();
-  private static VersioningManager versioningManager = VersioningManagerFactory.getInstance()
-      .createInterface();
-
-  static {
-    try {
-      log =
-          new BufferedWriter(new FileWriter("healing.log", true));
-    } catch (IOException e) {
-      if (log != null) {
-        try {
-          log.close();
-        } catch (IOException e1) {
-          throw new RuntimeException("can't initial healing log file: " + e1.getMessage());
-        }
-      }
-      throw new RuntimeException("can't initial healing log file: " + e.getMessage());
+    private static final int DEFAULT_THREAD_NUMBER = 100;
+    private static List<ItemHealingTask> tasks = new ArrayList<>();
+    private static VendorSoftwareProductManager vspManager = VspManagerFactory
+            .getInstance().createInterface();
+    private static HealingManager healingManager = HealingManagerFactory.getInstance()
+            .createInterface();
+
+    private HealAll() {
     }
-  }
 
-  public static void healAll(String threadNumber) {
+    public static void healAll(String threadNumber) {
 
-    writeToLog("----starting healing------");
-    Instant startTime = Instant.now();
+        String logFileName = "healing.log";
+        try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
 
-    int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) :
-        defaulThreadNumber;
-    ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
+            writeToLog("----starting healing------", log);
+            Instant startTime = Instant.now();
 
-    filterByEntityType(VersionInfoCassandraLoader.list(),
-        VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach
-        (HealAll::addTaskToTasks);
+            int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) :
+                    DEFAULT_THREAD_NUMBER;
+            ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
 
-    executeAllTasks(executor);
+            filterByEntityType(VersionInfoCassandraLoader.list(),
+                    VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach
+                    (HealAll::addTaskToTasks);
 
-    writeToLog("----finished healing------");
-    Instant endTime = Instant.now();
-    writeToLog("Total runtime was: " + Duration.between(startTime, endTime));
+            executeAllTasks(executor, log);
 
-    try {
-      if (log != null) {
-        log.close();
-      }
-    } catch (IOException e) {
-      writeToLog("Error:" + e.getMessage());
-    }
+            writeToLog("----finished healing------", log);
+            Instant endTime = Instant.now();
+            writeToLog("Total runtime was: " + Duration.between(startTime, endTime), log);
+        } catch (IOException e) {
+            throw new HealingRuntimeException("can't initial healing log file '" + logFileName + "'", e);
+        }
 
-    System.exit(1);
-  }
+        System.exit(1);
+    }
 
-  private static void executeAllTasks(ExecutorService executor) {
-    List<Future<String>> futureTasks;
-    try {
-      futureTasks = executor.invokeAll(tasks);
-      futureTasks.forEach(future -> {
+    private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
+        List<Future<String>> futureTasks;
         try {
-          log.write(future.get());
-          log.newLine();
-        } catch (Exception e) {
-          writeToLog(e.getMessage());
+            futureTasks = executor.invokeAll(tasks);
+            futureTasks.forEach(future -> {
+                try {
+                    log.write(future.get());
+                    log.newLine();
+                } catch (Exception e) {
+                    writeToLog(e.getMessage(), log);
+                }
+            });
+        } catch (InterruptedException e) {
+            writeToLog("migration tasks failed with message: " + e.getMessage(), log);
+            throw new HealingRuntimeException(e);
+        }
+
+        boolean isThreadOpen = true;
+        while (isThreadOpen) {
+            isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
         }
-      });
-    } catch (InterruptedException e) {
-      writeToLog("migration tasks failed with message: " + e.getMessage());
-      throw new RuntimeException(e);
     }
 
-    boolean isThreadOpen = true;
-    while (isThreadOpen) {
-      isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
+    private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
+        if (Objects.nonNull(versionInfoEntity.getCandidate())) {
+            return versionInfoEntity.getCandidate().getVersion();
+        } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
+            return versionInfoEntity.getViewableVersions().stream().max(Version::compateTo).get();
+        }
+        return versionInfoEntity.getActiveVersion();
     }
-  }
 
-  private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
-    if (Objects.nonNull(versionInfoEntity.getCandidate())) {
-      return versionInfoEntity.getCandidate().getVersion();
-    } else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
-      return versionInfoEntity.getViewableVersions().stream().max(Version::compateTo).get();
+    private static void writeToLog(String message, BufferedWriter log) {
+        try {
+            log.write(message);
+            log.newLine();
+        } catch (IOException e) {
+            throw new HealingRuntimeException("unable to write to healing all log file.", e);
+        }
     }
-    return versionInfoEntity.getActiveVersion();
-  }
-
-  private static void writeToLog(String message) {
-    try {
-      log.write(message);
-      log.newLine();
-    } catch (IOException e) {
-      throw new RuntimeException("unable to write to healing all log file.");
+
+    private static Stream<VersionInfoEntity> filterByEntityType(
+            Collection<VersionInfoEntity> versionInfoEntities, String entityType) {
+        return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity
+                .getEntityType().equals(entityType));
+    }
+
+    private static void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
+        tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion
+                (versionInfoEntity).toString(),
+                vspManager, healingManager));
     }
-  }
-
-  private static Stream<VersionInfoEntity> filterByEntityType(
-      Collection<VersionInfoEntity> versionInfoEntities, String entityType) {
-    return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity
-        .getEntityType().equals(entityType));
-  }
-
-  private static void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
-    tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion
-        (versionInfoEntity).toString(),
-        vspManager, healingManager));
-  }
 
 }
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exceptions/HealingRuntimeException.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exceptions/HealingRuntimeException.java
new file mode 100644 (file)
index 0000000..7117378
--- /dev/null
@@ -0,0 +1,11 @@
+package org.openecomp.core.tools.exceptions;
+
+public class HealingRuntimeException extends RuntimeException {
+    public HealingRuntimeException(String message, Exception exception) {
+        super(message, exception);
+    }
+
+    public HealingRuntimeException(Exception exception) {
+        super(exception);
+    }
+}