refactor(run): Reducing cognitive complexity for Detect Run.
*/
package com.synopsys.integration.detect.lifecycle.run;
+import java.util.Optional;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -44,12 +46,12 @@ public class DetectRun {
}
public void run(BootSingletons bootSingletons) {
- OperationSystem operationSystem = null;
+ Optional<OperationSystem> operationSystem = Optional.empty();
try {
SingletonFactory singletonFactory = new SingletonFactory(bootSingletons);
EventSingletons eventSingletons = singletonFactory.createEventSingletons();
UtilitySingletons utilitySingletons = singletonFactory.createUtilitySingletons(eventSingletons, exitCodeManager);
- operationSystem = utilitySingletons.getOperationSystem();
+ operationSystem = Optional.of(utilitySingletons.getOperationSystem());
ProductRunData productRunData = bootSingletons.getProductRunData(); //TODO: Remove run data from boot singletons
OperationFactory operationFactory = createOperationFactory(bootSingletons, utilitySingletons, eventSingletons);
@@ -61,19 +63,16 @@ public class DetectRun {
NameVersion nameVersion = stepRunner.determineProjectInformation(universalToolsResult);
operationFactory.publishProjectNameVersionChosen(nameVersion);
BdioResult bdio = stepRunner.generateBdio(universalToolsResult, nameVersion);
- StepHelper stepHelper = new StepHelper(operationSystem, utilitySingletons.getOperationWrapper(), productRunData.getDetectToolFilter());
+ StepHelper stepHelper = new StepHelper(utilitySingletons.getOperationSystem(), utilitySingletons.getOperationWrapper(), productRunData.getDetectToolFilter());
if (productRunData.shouldUseBlackDuckProduct()) {
BlackDuckRunData blackDuckRunData = productRunData.getBlackDuckRunData();
- if (blackDuckRunData.isRapid()) {
- if (blackDuckRunData.isOnline()) {
+ if (blackDuckRunData.isRapid() && blackDuckRunData.isOnline()) {
RapidModeStepRunner rapidModeSteps = new RapidModeStepRunner(operationFactory);
rapidModeSteps.runOnline(blackDuckRunData, nameVersion, bdio);
- } else {
+ } else if (blackDuckRunData.isRapid()) {
logger.info("Rapid Scan is offline, nothing to do.");
- }
- } else {
- if (blackDuckRunData.isOnline()) {
+ } else if (blackDuckRunData.isOnline()) {
IntelligentModeStepRunner intelligentModeSteps = new IntelligentModeStepRunner(operationFactory, stepHelper);
intelligentModeSteps.runOnline(blackDuckRunData, bdio, nameVersion, productRunData.getDetectToolFilter(), universalToolsResult.getDockerTargetData());
} else {
@@ -81,23 +80,28 @@ public class DetectRun {
intelligentModeSteps.runOffline(nameVersion, universalToolsResult.getDockerTargetData());
}
}
- }
} catch (Exception e) {
+ logger.error("Detect run failed: {}", getExceptionMessage(e));
+ logger.debug("An exception was thrown during the detect run.", e);
+ exitCodeManager.requestExitCode(e);
+ checkForInterruptedException(e);
+ } finally {
+ operationSystem.ifPresent(OperationSystem::publishOperations);
+ }
+ }
+
+ private String getExceptionMessage(Exception e) {
if (e.getMessage() != null) {
- logger.error("Detect run failed: {}", e.getMessage());
+ return e.getMessage();
} else {
- logger.error("Detect run failed: {}", e.getClass().getSimpleName());
+ return e.getClass().getSimpleName();
}
- logger.debug("An exception was thrown during the detect run.", e);
- exitCodeManager.requestExitCode(e);
+ }
+
+ private void checkForInterruptedException(Exception e) {
if (e instanceof InterruptedException) {
// Restore interrupted state...
Thread.currentThread().interrupt();
}
- } finally {
- if (operationSystem != null) {
- operationSystem.publishOperations();
- }
- }
}
}
