private org.wso2.carbon.ntask.core.TaskManager getTaskManager(boolean system) throws Exception {
   TaskService taskService = NtaskService.getTaskService();
   if (taskService == null) {
     return null;
   }
   // TODO return our own task manager instance which wraps the Clustered
   // task manager here.
   return taskService.getTaskManager(
       system ? TaskBuilder.TASK_TYPE_SYSTEM : TaskBuilder.TASK_TYPE_USER);
 }
  public boolean init(Properties properties) {
    synchronized (lock) {
      try {
        TaskService taskService = NtaskService.getTaskService();
        if (taskService == null || NtaskService.getCcServiceInstance() == null) {
          // Cannot proceed with the initialization because the
          // TaskService is not yet
          // available. Register this as an observer so that this can
          // be reinitialized
          // within the NtaskService when the TaskService is
          // available.
          NtaskService.addObserver(this);
          return false;
        }
        boolean isSingleNode =
            NtaskService.getCcServiceInstance()
                    .getServerConfigContext()
                    .getAxisConfiguration()
                    .getClusteringAgent()
                == null;
        boolean isWorkerNode = !isSingleNode && CarbonUtils.isWorkerNode();
        logger.debug("#init Single-Node: " + isSingleNode + " Worker-Node: " + isWorkerNode);
        if (!isSingleNode && !isWorkerNode) {
          // Skip running tasks on the management node
          logger.debug("#init Skipping task registration");
          initialized = true;
          return true;
        }

        if ((taskManager = getTaskManager(false)) == null) {
          return false;
        }

        // Register pending tasks..
        Iterator tasks = pendingTasks.iterator();
        while (tasks.hasNext()) {
          TaskInfo taskInfo = (TaskInfo) tasks.next();
          try {
            List<TaskInfo> taskInfos = taskManager.getAllTasks();
            boolean hasTask = false;
            for (TaskInfo task : taskInfos) {
              if (task.getName().equals(taskInfo.getName())) {
                hasTask = true;
                break;
              }
            }
            if (hasTask) {
              logger.debug(
                  "#init Pending task ["
                      + taskInfo.getName()
                      + "] is already available in the registry.");
              continue;
            }
            taskManager.registerTask(taskInfo);
            taskManager.scheduleTask(taskInfo.getName());
            tasks.remove();
            logger.debug("#init Scheduled pending task [" + taskInfo.getName() + "] SUCCESSFUL.");
          } catch (TaskException e) {
            logger.error(
                "Could not schedule task ["
                    + taskInfo.getName()
                    + "]. Error: "
                    + e.getLocalizedMessage(),
                e);
          }
        }
        // Run already deployed tasks..
        taskService.registerTaskType(TaskBuilder.TASK_TYPE_USER);
        taskService.registerTaskType(TaskBuilder.TASK_TYPE_SYSTEM);
        initialized = true;
        logger.info("initialized");
        return true;
      } catch (Exception e) {
        logger.error("Cannot initialize task manager. Error: " + e.getLocalizedMessage(), e);
        initialized = false;
      }
    }
    return false;
  }