From 30e8dc885c7a9550cb05b78bfb7ab2707ccffdaa Mon Sep 17 00:00:00 2001 From: harshanl Date: Thu, 3 Dec 2015 20:13:10 +0530 Subject: [PATCH] Fixed EMM-1034 --- .../operation/mgt/OperationManagerImpl.java | 1 - .../impl/PolicyAdministratorPointImpl.java | 46 ++++++++++++------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7ef4b8f5da..71ea7adf72 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -28,7 +28,6 @@ import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorization import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 5e63355f95..b96e8f1cd7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -41,6 +41,7 @@ import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @@ -123,36 +124,49 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE); if (log.isDebugEnabled()) { log.debug("Policy delegations task is started for the tenant id " + tenantId); } TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.DELEGATION_TASK_TYPE); - TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setRepeatCount(0); - Map properties = new HashMap<>(); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); - String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); - if (!taskManager.isTaskScheduled(taskName)) { - - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, - properties, triggerInfo); - - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + Set registeredTaskTypes = taskService.getRegisteredTaskTypes(); + //Check whether the TaskType is already registered. If not we'll register it here. + if (!registeredTaskTypes.contains(PolicyManagementConstants.DELEGATION_TASK_TYPE)) { + taskService.registerTaskType(PolicyManagementConstants.DELEGATION_TASK_TYPE); + TaskInfo registeredTaskInfo = null; + // getTask method will throw a TaskException if the task is not registered. Hence we'll handle the + // exception and register the task. + try { + registeredTaskInfo = taskManager.getTask(taskName); + } catch (TaskException e) { + // No need of any specific logic to handle this exception as it is thrown if the task is not registered. + } finally { + // If registeredTaskInfo is null that means there's no registered delegation-task. + if (registeredTaskInfo == null) { + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, + properties, triggerInfo); + taskManager.registerTask(taskInfo); + taskManager.scheduleTask(taskInfo.getName()); + } + } } else { - throw new PolicyManagementException("There is a task already running for policy changes. Please try " + - "to apply " + - "changes after few minutes."); + if (!taskManager.isTaskScheduled(taskName)) { + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, + properties, triggerInfo); + taskManager.scheduleTask(taskInfo.getName()); + } else { + throw new PolicyManagementException("There is a task already running for policy changes. Please try " + + "to apply " + + "changes after few minutes."); + } } - } catch (TaskException e) { String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext.