From 402b843e5d8b654d110d6a77bd44cd563f7a14ae Mon Sep 17 00:00:00 2001 From: harshanl Date: Wed, 23 Dec 2015 19:35:50 +0530 Subject: [PATCH] Updated task schedule service to fix EMM-1145, EMM-1100 --- .../mgt/core/task/TaskScheduleService.java | 2 + .../core/task/TaskScheduleServiceImpl.java | 37 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java index 29d45149eb..eb42b17fd2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java @@ -28,4 +28,6 @@ public interface TaskScheduleService { void stopTask() throws PolicyMonitoringTaskException; void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException; + + boolean isTaskScheduled() throws PolicyMonitoringTaskException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java index 75550744ca..a41edcf6ec 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -49,15 +49,13 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { @Override public void startTask(int monitoringFrequency) throws PolicyMonitoringTaskException { - - + int tenantId = getTenantId(); if (policyConfiguration.getMonitoringEnable()) { if (monitoringFrequency <= 0) { throw new PolicyMonitoringTaskException("Time interval cannot be 0 or less than 0."); } try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE); @@ -91,8 +89,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { } catch (TaskException e) { throw new PolicyMonitoringTaskException("Error occurred while creating the task for tenant " + - PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + tenantId, e); } } else { throw new PolicyMonitoringTaskException("Policy monitoring is not enabled in the cdm-config.xml."); @@ -102,23 +99,22 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { @Override public void stopTask() throws PolicyMonitoringTaskException { + int tenantId = getTenantId(); try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); } catch (TaskException e) { throw new PolicyMonitoringTaskException("Error occurred while deleting the task for tenant " + - PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + tenantId, e); } } @Override public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException { + int tenantId = getTenantId(); try { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); @@ -144,10 +140,27 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { } } catch (TaskException e) { - throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " + - PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(), e); + throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " + tenantId, e); + } + + } + + @Override + public boolean isTaskScheduled() throws PolicyMonitoringTaskException { + int tenantId = getTenantId(); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + TaskManager taskManager; + try { + taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + return taskManager.isTaskScheduled(taskName); + } catch (TaskException e) { + throw new PolicyMonitoringTaskException("Error occurred while checking task for tenant " + + tenantId, e); } + } + private int getTenantId() { + return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); } }