Search in sources :

Example 36 with ExecTO

use of org.apache.syncope.common.lib.to.ExecTO in project syncope by apache.

the class TaskLogic method execute.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_EXECUTE + "')")
@Override
public ExecTO execute(final String key, final Date startAt, final boolean dryRun) {
    Task task = taskDAO.find(key);
    if (task == null) {
        throw new NotFoundException("Task " + key);
    }
    if (startAt != null && startAt.before(new Date())) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add("Cannot schedule in the past");
        throw sce;
    }
    TaskUtils taskUtil = taskUtilsFactory.getInstance(task);
    ExecTO result = null;
    switch(taskUtil.getType()) {
        case PROPAGATION:
            TaskExec propExec = taskExecutor.execute((PropagationTaskTO) binder.getTaskTO(task, taskUtil, false));
            result = binder.getExecTO(propExec);
            break;
        case NOTIFICATION:
            TaskExec notExec = notificationJobDelegate.executeSingle((NotificationTask) task);
            result = binder.getExecTO(notExec);
            break;
        case SCHEDULED:
        case PULL:
        case PUSH:
            if (!((SchedTask) task).isActive()) {
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add("Task " + key + " is not active");
                throw sce;
            }
            try {
                Map<String, Object> jobDataMap = jobManager.register((SchedTask) task, startAt, confDAO.find("tasks.interruptMaxRetries", 1L));
                jobDataMap.put(TaskJob.DRY_RUN_JOBDETAIL_KEY, dryRun);
                if (startAt == null) {
                    scheduler.getScheduler().triggerJob(JobNamer.getJobKey(task), new JobDataMap(jobDataMap));
                }
            } catch (Exception e) {
                LOG.error("While executing task {}", task, e);
                SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
                sce.getElements().add(e.getMessage());
                throw sce;
            }
            result = new ExecTO();
            result.setJobType(JobType.TASK);
            result.setRefKey(task.getKey());
            result.setRefDesc(binder.buildRefDesc(task));
            result.setStart(new Date());
            result.setStatus("JOB_FIRED");
            result.setMessage("Job fired; waiting for results...");
            break;
        default:
    }
    return result;
}
Also used : TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) Task(org.apache.syncope.core.persistence.api.entity.task.Task) JobDataMap(org.quartz.JobDataMap) ExecTO(org.apache.syncope.common.lib.to.ExecTO) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Date(java.util.Date) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SchedulerException(org.quartz.SchedulerException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 37 with ExecTO

use of org.apache.syncope.common.lib.to.ExecTO in project syncope by apache.

the class GroupLogic method bulkMembersAction.

@PreAuthorize("hasRole('" + StandardEntitlement.TASK_CREATE + "') " + "and hasRole('" + StandardEntitlement.TASK_EXECUTE + "')")
@Transactional
public ExecTO bulkMembersAction(final String key, final BulkMembersActionType actionType) {
    Group group = groupDAO.find(key);
    if (group == null) {
        throw new NotFoundException("Group " + key);
    }
    Implementation jobDelegate = implementationDAO.find(ImplementationType.TASKJOB_DELEGATE).stream().filter(impl -> GroupMemberProvisionTaskJobDelegate.class.getName().equals(impl.getBody())).findFirst().orElse(null);
    if (jobDelegate == null) {
        jobDelegate = entityFactory.newEntity(Implementation.class);
        jobDelegate.setKey(GroupMemberProvisionTaskJobDelegate.class.getSimpleName());
        jobDelegate.setEngine(ImplementationEngine.JAVA);
        jobDelegate.setType(ImplementationType.TASKJOB_DELEGATE);
        jobDelegate.setBody(GroupMemberProvisionTaskJobDelegate.class.getName());
        jobDelegate = implementationDAO.save(jobDelegate);
    }
    SchedTask task = entityFactory.newEntity(SchedTask.class);
    task.setName("Bulk member provision for group " + group.getName());
    task.setActive(true);
    task.setJobDelegate(jobDelegate);
    task = taskDAO.save(task);
    try {
        Map<String, Object> jobDataMap = jobManager.register(task, null, confDAO.find("tasks.interruptMaxRetries", 1L));
        jobDataMap.put(TaskJob.DRY_RUN_JOBDETAIL_KEY, false);
        jobDataMap.put(GroupMemberProvisionTaskJobDelegate.GROUP_KEY_JOBDETAIL_KEY, key);
        jobDataMap.put(GroupMemberProvisionTaskJobDelegate.ACTION_TYPE_JOBDETAIL_KEY, actionType);
        scheduler.getScheduler().triggerJob(JobNamer.getJobKey(task), new JobDataMap(jobDataMap));
    } catch (Exception e) {
        LOG.error("While executing task {}", task, e);
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.Scheduling);
        sce.getElements().add(e.getMessage());
        throw sce;
    }
    ExecTO result = new ExecTO();
    result.setJobType(JobType.TASK);
    result.setRefKey(task.getKey());
    result.setRefDesc(taskDataBinder.buildRefDesc(task));
    result.setStart(new Date());
    result.setStatus("JOB_FIRED");
    result.setMessage("Job fired; waiting for results...");
    return result;
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) JobDataMap(org.quartz.JobDataMap) ExecTO(org.apache.syncope.common.lib.to.ExecTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) GroupMemberProvisionTaskJobDelegate(org.apache.syncope.core.provisioning.java.job.GroupMemberProvisionTaskJobDelegate) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) Date(java.util.Date) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ExecTO (org.apache.syncope.common.lib.to.ExecTO)37 Test (org.junit.jupiter.api.Test)22 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)13 UserTO (org.apache.syncope.common.lib.to.UserTO)13 Response (javax.ws.rs.core.Response)11 PullTaskTO (org.apache.syncope.common.lib.to.PullTaskTO)10 Date (java.util.Date)9 TaskService (org.apache.syncope.common.rest.api.service.TaskService)8 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Map (java.util.Map)6 GroupTO (org.apache.syncope.common.lib.to.GroupTO)6 ResourceTO (org.apache.syncope.common.lib.to.ResourceTO)6 IOException (java.io.IOException)5 TaskTO (org.apache.syncope.common.lib.to.TaskTO)5 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 AttrTO (org.apache.syncope.common.lib.to.AttrTO)4 ConnInstanceTO (org.apache.syncope.common.lib.to.ConnInstanceTO)4 ImplementationTO (org.apache.syncope.common.lib.to.ImplementationTO)4 ItemTO (org.apache.syncope.common.lib.to.ItemTO)4