diff options
author | avigaffa <avi.gaffa@amdocs.com> | 2017-12-28 09:34:47 +0200 |
---|---|---|
committer | Vitaly Emporopulo <Vitaliy.Emporopulo@amdocs.com> | 2017-12-31 06:45:56 +0000 |
commit | 9d7f1e2524fd97844627dbbb1e11d1c1a0ae1d9c (patch) | |
tree | db667da530a4c17cf046ab0529ff25375c2c694e /openecomp-be/tools/zusammen-tools/src/main | |
parent | 0286e10d3ca141e72afb7e0065daa76556972699 (diff) |
Fixing sonar Exception Handling
Change-Id: I0afe103b921ea26de8f701e9a2cff7943ab58110
Issue-ID: SDC-810
Signed-off-by: avigaffa <avi.gaffa@amdocs.com>
Diffstat (limited to 'openecomp-be/tools/zusammen-tools/src/main')
2 files changed, 84 insertions, 93 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java index 40a56eda83..7868927c93 100644 --- a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/HealAll.java @@ -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 index 0000000000..711737896f --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/exceptions/HealingRuntimeException.java @@ -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); + } +} |