Search in sources :

Example 1 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskDataBinderImpl method getTaskTO.

@Override
public <T extends TaskTO> T getTaskTO(final Task task, final TaskUtils taskUtils, final boolean details) {
    T taskTO = taskUtils.newTaskTO();
    BeanUtils.copyProperties(task, taskTO, IGNORE_TASK_PROPERTIES);
    TaskExec latestExec = taskExecDAO.findLatestStarted(task);
    if (latestExec == null) {
        taskTO.setLatestExecStatus(StringUtils.EMPTY);
    } else {
        taskTO.setLatestExecStatus(latestExec.getStatus());
        taskTO.setStart(latestExec.getStart());
        taskTO.setEnd(latestExec.getEnd());
    }
    if (details) {
        task.getExecs().stream().filter(execution -> execution != null).forEachOrdered(execution -> taskTO.getExecutions().add(getExecTO(execution)));
    }
    switch(taskUtils.getType()) {
        case PROPAGATION:
            PropagationTask propagationTask = (PropagationTask) task;
            PropagationTaskTO propagationTaskTO = (PropagationTaskTO) taskTO;
            propagationTaskTO.setAnyTypeKind(propagationTask.getAnyTypeKind());
            propagationTaskTO.setEntityKey(propagationTask.getEntityKey());
            propagationTaskTO.setResource(propagationTask.getResource().getKey());
            propagationTaskTO.setAttributes(propagationTask.getSerializedAttributes());
            break;
        case SCHEDULED:
            SchedTask schedTask = (SchedTask) task;
            SchedTaskTO schedTaskTO = (SchedTaskTO) taskTO;
            setExecTime(schedTaskTO, task);
            if (schedTask.getJobDelegate() != null) {
                schedTaskTO.setJobDelegate(schedTask.getJobDelegate().getKey());
            }
            break;
        case PULL:
            PullTask pullTask = (PullTask) task;
            PullTaskTO pullTaskTO = (PullTaskTO) taskTO;
            setExecTime(pullTaskTO, task);
            pullTaskTO.setDestinationRealm(pullTask.getDestinatioRealm().getFullPath());
            pullTaskTO.setResource(pullTask.getResource().getKey());
            pullTaskTO.setMatchingRule(pullTask.getMatchingRule() == null ? MatchingRule.UPDATE : pullTask.getMatchingRule());
            pullTaskTO.setUnmatchingRule(pullTask.getUnmatchingRule() == null ? UnmatchingRule.PROVISION : pullTask.getUnmatchingRule());
            if (pullTask.getReconFilterBuilder() != null) {
                pullTaskTO.setReconFilterBuilder(pullTask.getReconFilterBuilder().getKey());
            }
            pullTaskTO.getActions().addAll(pullTask.getActions().stream().map(Entity::getKey).collect(Collectors.toList()));
            pullTask.getTemplates().forEach(template -> {
                pullTaskTO.getTemplates().put(template.getAnyType().getKey(), template.get());
            });
            pullTaskTO.setRemediation(pullTask.isRemediation());
            break;
        case PUSH:
            PushTask pushTask = (PushTask) task;
            PushTaskTO pushTaskTO = (PushTaskTO) taskTO;
            setExecTime(pushTaskTO, task);
            pushTaskTO.setSourceRealm(pushTask.getSourceRealm().getFullPath());
            pushTaskTO.setResource(pushTask.getResource().getKey());
            pushTaskTO.setMatchingRule(pushTask.getMatchingRule() == null ? MatchingRule.LINK : pushTask.getMatchingRule());
            pushTaskTO.setUnmatchingRule(pushTask.getUnmatchingRule() == null ? UnmatchingRule.ASSIGN : pushTask.getUnmatchingRule());
            pushTaskTO.getActions().addAll(pushTask.getActions().stream().map(Entity::getKey).collect(Collectors.toList()));
            pushTask.getFilters().forEach(filter -> {
                pushTaskTO.getFilters().put(filter.getAnyType().getKey(), filter.getFIQLCond());
            });
            break;
        case NOTIFICATION:
            NotificationTask notificationTask = (NotificationTask) task;
            NotificationTaskTO notificationTaskTO = (NotificationTaskTO) taskTO;
            notificationTaskTO.setNotification(notificationTask.getNotification().getKey());
            notificationTaskTO.setAnyTypeKind(notificationTask.getAnyTypeKind());
            notificationTaskTO.setEntityKey(notificationTask.getEntityKey());
            if (notificationTask.isExecuted() && StringUtils.isBlank(taskTO.getLatestExecStatus())) {
                taskTO.setLatestExecStatus("[EXECUTED]");
            }
            break;
        default:
    }
    return taskTO;
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) TaskTO(org.apache.syncope.common.lib.to.TaskTO) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) Entity(org.apache.syncope.core.persistence.api.entity.Entity) StringUtils(org.apache.commons.lang3.StringUtils) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) Scheduler(org.quartz.Scheduler) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) SchedTaskTO(org.apache.syncope.common.lib.to.SchedTaskTO) ProvisioningTaskTO(org.apache.syncope.common.lib.to.ProvisioningTaskTO) TaskExecDAO(org.apache.syncope.core.persistence.api.dao.TaskExecDAO) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) NotificationTaskTO(org.apache.syncope.common.lib.to.NotificationTaskTO) PullJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PullJobDelegate) ExecTO(org.apache.syncope.common.lib.to.ExecTO) Task(org.apache.syncope.core.persistence.api.entity.task.Task) JobNamer(org.apache.syncope.core.provisioning.api.job.JobNamer) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Collectors(java.util.stream.Collectors) UnmatchingRule(org.apache.syncope.common.lib.types.UnmatchingRule) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) ImplementationEngine(org.apache.syncope.common.lib.types.ImplementationEngine) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) ProvisioningTask(org.apache.syncope.core.persistence.api.entity.task.ProvisioningTask) TriggerKey(org.quartz.TriggerKey) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) BeanUtils(org.apache.syncope.core.spring.BeanUtils) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) SchedulerException(org.quartz.SchedulerException) Trigger(org.quartz.Trigger) ImplementationType(org.apache.syncope.common.lib.types.ImplementationType) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) TemplateUtils(org.apache.syncope.core.provisioning.java.utils.TemplateUtils) PushJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate) PushTaskTO(org.apache.syncope.common.lib.to.PushTaskTO) Logger(org.slf4j.Logger) PushTaskAnyFilter(org.apache.syncope.core.persistence.api.entity.task.PushTaskAnyFilter) TaskUtils(org.apache.syncope.core.persistence.api.entity.task.TaskUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) Component(org.springframework.stereotype.Component) PullTaskTO(org.apache.syncope.common.lib.to.PullTaskTO) JobType(org.apache.syncope.common.lib.types.JobType) MatchingRule(org.apache.syncope.common.lib.types.MatchingRule) TaskType(org.apache.syncope.common.lib.types.TaskType) Entity(org.apache.syncope.core.persistence.api.entity.Entity) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) SchedTaskTO(org.apache.syncope.common.lib.to.SchedTaskTO) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) PushTaskTO(org.apache.syncope.common.lib.to.PushTaskTO) PullTaskTO(org.apache.syncope.common.lib.to.PullTaskTO) NotificationTaskTO(org.apache.syncope.common.lib.to.NotificationTaskTO)

Example 2 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskLogic method getReference.

@Override
protected Triple<JobType, String, String> getReference(final JobKey jobKey) {
    String key = JobNamer.getTaskKeyFromJobName(jobKey.getName());
    Task task = taskDAO.find(key);
    return task == null || !(task instanceof SchedTask) ? null : Triple.of(JobType.TASK, key, binder.buildRefDesc(task));
}
Also used : 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) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask)

Example 3 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class JobManagerImpl method register.

@Override
public Map<String, Object> register(final SchedTask task, final Date startAt, final long interruptMaxRetries) throws SchedulerException {
    TaskJob job = createSpringBean(TaskJob.class);
    job.setTaskKey(task.getKey());
    Implementation jobDelegate = task.getJobDelegate() == null ? task instanceof PullTask ? implementationDAO.find(ImplementationType.TASKJOB_DELEGATE).stream().filter(impl -> PullJobDelegate.class.getName().equals(impl.getBody())).findFirst().orElse(null) : task instanceof PushTask ? implementationDAO.find(ImplementationType.TASKJOB_DELEGATE).stream().filter(impl -> PushJobDelegate.class.getName().equals(impl.getBody())).findFirst().orElse(null) : null : task.getJobDelegate();
    if (jobDelegate == null) {
        throw new IllegalArgumentException("Task " + task + " does not provide any " + SchedTaskJobDelegate.class.getSimpleName());
    }
    Map<String, Object> jobMap = new HashMap<>();
    jobMap.put(JobManager.DOMAIN_KEY, AuthContextUtils.getDomain());
    jobMap.put(TaskJob.DELEGATE_IMPLEMENTATION, jobDelegate.getKey());
    registerJob(JobNamer.getJobKey(task).getName(), job, task.getCronExpression(), startAt, jobMap);
    return jobMap;
}
Also used : Connection(java.sql.Connection) SyncopeLoader(org.apache.syncope.core.persistence.api.SyncopeLoader) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) StringUtils(org.apache.commons.lang3.StringUtils) CPlainAttr(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttr) JobBuilder(org.quartz.JobBuilder) Scheduler(org.quartz.Scheduler) Pair(org.apache.commons.lang3.tuple.Pair) ResultSet(java.sql.ResultSet) Map(java.util.Map) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) SchedulerFactoryBean(org.springframework.scheduling.quartz.SchedulerFactoryBean) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) PullJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PullJobDelegate) Constants(org.quartz.impl.jdbcjobstore.Constants) Task(org.apache.syncope.core.persistence.api.entity.task.Task) SchedTaskJobDelegate(org.apache.syncope.core.provisioning.api.job.SchedTaskJobDelegate) JobNamer(org.apache.syncope.core.provisioning.api.job.JobNamer) Set(java.util.Set) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) PreparedStatement(java.sql.PreparedStatement) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) JobDataMap(org.quartz.JobDataMap) Optional(java.util.Optional) TriggerKey(org.quartz.TriggerKey) ConfDAO(org.apache.syncope.core.persistence.api.dao.ConfDAO) DataSourceUtils(org.springframework.jdbc.datasource.DataSourceUtils) IOUtil(org.identityconnectors.common.IOUtil) HashMap(java.util.HashMap) Job(org.quartz.Job) JobKey(org.quartz.JobKey) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) SchedulerException(org.quartz.SchedulerException) TriggerBuilder(org.quartz.TriggerBuilder) BeanCreationException(org.springframework.beans.factory.BeanCreationException) DataSource(javax.sql.DataSource) ImplementationType(org.apache.syncope.common.lib.types.ImplementationType) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) ReportJob(org.apache.syncope.core.provisioning.java.job.report.ReportJob) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) PushJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate) Report(org.apache.syncope.core.persistence.api.entity.Report) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) NotificationJob(org.apache.syncope.core.provisioning.java.job.notification.NotificationJob) ReportDAO(org.apache.syncope.core.persistence.api.dao.ReportDAO) JobManager(org.apache.syncope.core.provisioning.api.job.JobManager) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) DomainsHolder(org.apache.syncope.core.persistence.api.DomainsHolder) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) TaskType(org.apache.syncope.common.lib.types.TaskType) CronScheduleBuilder(org.quartz.CronScheduleBuilder) Transactional(org.springframework.transaction.annotation.Transactional) PushJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate) HashMap(java.util.HashMap) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) PushTask(org.apache.syncope.core.persistence.api.entity.task.PushTask) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation)

Example 4 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class JobManagerImpl method load.

@Transactional
@Override
public void load() {
    if (disableQuartzInstance) {
        String instanceId = "AUTO";
        try {
            instanceId = scheduler.getScheduler().getSchedulerInstanceId();
            scheduler.getScheduler().standby();
            LOG.info("Successfully put Quartz instance {} in standby", instanceId);
        } catch (SchedulerException e) {
            LOG.error("Could not put Quartz instance {} in standby", instanceId, e);
        }
        return;
    }
    final Pair<String, Long> conf = AuthContextUtils.execWithAuthContext(SyncopeConstants.MASTER_DOMAIN, () -> {
        String notificationJobCronExpression = StringUtils.EMPTY;
        Optional<? extends CPlainAttr> notificationJobCronExp = confDAO.find("notificationjob.cronExpression");
        if (!notificationJobCronExp.isPresent()) {
            notificationJobCronExpression = NotificationJob.DEFAULT_CRON_EXP;
        } else if (!notificationJobCronExp.get().getValuesAsStrings().isEmpty()) {
            notificationJobCronExpression = notificationJobCronExp.get().getValuesAsStrings().get(0);
        }
        long interruptMaxRetries = confDAO.find("tasks.interruptMaxRetries", 1L);
        return Pair.of(notificationJobCronExpression, interruptMaxRetries);
    });
    for (String domain : domainsHolder.getDomains().keySet()) {
        AuthContextUtils.execWithAuthContext(domain, () -> {
            // 1. jobs for SchedTasks
            Set<SchedTask> tasks = new HashSet<>(taskDAO.<SchedTask>findAll(TaskType.SCHEDULED));
            tasks.addAll(taskDAO.<PullTask>findAll(TaskType.PULL));
            tasks.addAll(taskDAO.<PushTask>findAll(TaskType.PUSH));
            boolean loadException = false;
            for (Iterator<SchedTask> it = tasks.iterator(); it.hasNext() && !loadException; ) {
                SchedTask task = it.next();
                try {
                    register(task, task.getStartAt(), conf.getRight());
                } catch (Exception e) {
                    LOG.error("While loading job instance for task " + task.getKey(), e);
                    loadException = true;
                }
            }
            if (loadException) {
                LOG.debug("Errors while loading job instances for tasks, aborting");
            } else {
                // 2. jobs for Reports
                for (Iterator<Report> it = reportDAO.findAll().iterator(); it.hasNext() && !loadException; ) {
                    Report report = it.next();
                    try {
                        register(report, null, conf.getRight());
                    } catch (Exception e) {
                        LOG.error("While loading job instance for report " + report.getName(), e);
                        loadException = true;
                    }
                }
                if (loadException) {
                    LOG.debug("Errors while loading job instances for reports, aborting");
                }
            }
            return null;
        });
    }
    Map<String, Object> jobMap = new HashMap<>();
    jobMap.put(JobManager.DOMAIN_KEY, AuthContextUtils.getDomain());
    // 3. NotificationJob
    if (StringUtils.isBlank(conf.getLeft())) {
        LOG.debug("Empty value provided for {}'s cron, not registering anything on Quartz", NotificationJob.class.getSimpleName());
    } else {
        LOG.debug("{}'s cron expression: {} - registering Quartz job and trigger", NotificationJob.class.getSimpleName(), conf.getLeft());
        try {
            NotificationJob job = createSpringBean(NotificationJob.class);
            registerJob(NOTIFICATION_JOB.getName(), job, conf.getLeft(), null, jobMap);
        } catch (Exception e) {
            LOG.error("While loading {} instance", NotificationJob.class.getSimpleName(), e);
        }
    }
    // 4. SystemLoadReporterJob (fixed schedule, every minute)
    LOG.debug("Registering {}", SystemLoadReporterJob.class);
    try {
        SystemLoadReporterJob job = createSpringBean(SystemLoadReporterJob.class);
        registerJob("systemLoadReporterJob", job, "0 * * * * ?", null, jobMap);
    } catch (Exception e) {
        LOG.error("While loading {} instance", SystemLoadReporterJob.class.getSimpleName(), e);
    }
}
Also used : SchedulerException(org.quartz.SchedulerException) Report(org.apache.syncope.core.persistence.api.entity.Report) HashMap(java.util.HashMap) NotificationJob(org.apache.syncope.core.provisioning.java.job.notification.NotificationJob) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) SQLException(java.sql.SQLException) SchedulerException(org.quartz.SchedulerException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with SchedTask

use of org.apache.syncope.core.persistence.api.entity.task.SchedTask in project syncope by apache.

the class TaskDataBinderImpl method createSchedTask.

@Override
public SchedTask createSchedTask(final SchedTaskTO taskTO, final TaskUtils taskUtils) {
    Class<? extends TaskTO> taskTOClass = taskUtils.taskTOClass();
    if (taskTOClass == null || !taskTOClass.equals(taskTO.getClass())) {
        throw new IllegalArgumentException(String.format("Expected %s, found %s", taskTOClass, taskTO.getClass()));
    }
    SchedTask task = taskUtils.newTask();
    task.setStartAt(taskTO.getStartAt());
    task.setCronExpression(taskTO.getCronExpression());
    task.setName(taskTO.getName());
    task.setDescription(taskTO.getDescription());
    task.setActive(taskTO.isActive());
    if (taskUtils.getType() == TaskType.SCHEDULED) {
        Implementation implementation = implementationDAO.find(taskTO.getJobDelegate());
        if (implementation == null) {
            throw new NotFoundException("Implementation " + taskTO.getJobDelegate());
        }
        task.setJobDelegate(implementation);
    } else if (taskTO instanceof ProvisioningTaskTO) {
        ProvisioningTaskTO provisioningTaskTO = (ProvisioningTaskTO) taskTO;
        ExternalResource resource = resourceDAO.find(provisioningTaskTO.getResource());
        if (resource == null) {
            throw new NotFoundException("Resource " + provisioningTaskTO.getResource());
        }
        ((ProvisioningTask) task).setResource(resource);
        fill((ProvisioningTask) task, provisioningTaskTO);
    }
    return task;
}
Also used : SchedTask(org.apache.syncope.core.persistence.api.entity.task.SchedTask) ProvisioningTaskTO(org.apache.syncope.common.lib.to.ProvisioningTaskTO) ProvisioningTask(org.apache.syncope.core.persistence.api.entity.task.ProvisioningTask) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation)

Aggregations

SchedTask (org.apache.syncope.core.persistence.api.entity.task.SchedTask)9 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)8 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)5 SchedulerException (org.quartz.SchedulerException)5 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)4 Task (org.apache.syncope.core.persistence.api.entity.task.Task)4 TaskUtils (org.apache.syncope.core.persistence.api.entity.task.TaskUtils)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Date (java.util.Date)3 ExecTO (org.apache.syncope.common.lib.to.ExecTO)3 JobDataMap (org.quartz.JobDataMap)3 Transactional (org.springframework.transaction.annotation.Transactional)3 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 StringUtils (org.apache.commons.lang3.StringUtils)2 ProvisioningTaskTO (org.apache.syncope.common.lib.to.ProvisioningTaskTO)2 ImplementationType (org.apache.syncope.common.lib.types.ImplementationType)2 TaskType (org.apache.syncope.common.lib.types.TaskType)2 ImplementationDAO (org.apache.syncope.core.persistence.api.dao.ImplementationDAO)2