From a12e1f983427843d77f1e1fdcfbb8e2ef961e092 Mon Sep 17 00:00:00 2001 From: mabofu Date: Wed, 26 Feb 2025 17:49:02 +0800 Subject: [PATCH] Fixed sonar issue: fixed TaskContext issue --- .../com/dsms/common/taskmanager/TaskContext.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/dsms-engine-common/src/main/java/com/dsms/common/taskmanager/TaskContext.java b/dsms-engine-common/src/main/java/com/dsms/common/taskmanager/TaskContext.java index 9fd9b2e7..833e43ae 100644 --- a/dsms-engine-common/src/main/java/com/dsms/common/taskmanager/TaskContext.java +++ b/dsms-engine-common/src/main/java/com/dsms/common/taskmanager/TaskContext.java @@ -18,7 +18,6 @@ package com.dsms.common.taskmanager; import com.dsms.common.constant.TaskTypeEnum; import com.dsms.common.taskmanager.model.Task; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; @@ -26,13 +25,18 @@ import java.util.Optional; @Component public class TaskContext { + + private static final String INVALID_TASK_TYPE = "Invalid task type"; /** * Spring will automatically inject the implementation class of the TaskStrategy interface into this Map, * the key is the bean id and the value is the implementation class */ - @Autowired private Map dsmsStorageTaskMap; + public TaskContext(Map dsmsStorageTaskMap) { + this.dsmsStorageTaskMap = dsmsStorageTaskMap; + } + /** * execute task strategy dependencies task type * @@ -41,7 +45,7 @@ public class TaskContext { */ public Task execute(Task executeTask) { TaskStrategy taskStrategy = Optional.ofNullable(dsmsStorageTaskMap.get(executeTask.getTaskType())) - .orElseThrow(() -> new IllegalArgumentException("Invalid task type")); + .orElseThrow(() -> new IllegalArgumentException(INVALID_TASK_TYPE)); return taskStrategy.execute(executeTask); } @@ -53,7 +57,7 @@ public class TaskContext { */ public Task rollback(Task rollbackTask) { TaskStrategy taskStrategy = Optional.ofNullable(dsmsStorageTaskMap.get(rollbackTask.getTaskType())) - .orElseThrow(() -> new IllegalArgumentException("Invalid task type")); + .orElseThrow(() -> new IllegalArgumentException(INVALID_TASK_TYPE)); return taskStrategy.rollback(rollbackTask); } @@ -66,7 +70,7 @@ public class TaskContext { */ public boolean validateTask(TaskTypeEnum taskTypeEnum, String... validateParam) { TaskStrategy taskStrategy = Optional.ofNullable(dsmsStorageTaskMap.get(taskTypeEnum.getType())) - .orElseThrow(() -> new IllegalArgumentException("Invalid task type")); + .orElseThrow(() -> new IllegalArgumentException(INVALID_TASK_TYPE)); return taskStrategy.validateTask(validateParam); } -- Gitee