1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.provisioning.java.job;
20
21 import org.apache.syncope.common.lib.types.TaskType;
22 import org.apache.syncope.core.persistence.api.DomainHolder;
23 import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
24 import org.apache.syncope.core.persistence.api.entity.Implementation;
25 import org.apache.syncope.core.provisioning.api.job.JobDelegate;
26 import org.apache.syncope.core.provisioning.api.job.JobManager;
27 import org.apache.syncope.core.provisioning.api.job.SchedTaskJobDelegate;
28 import org.apache.syncope.core.spring.ApplicationContextProvider;
29 import org.apache.syncope.core.spring.implementation.ImplementationManager;
30 import org.apache.syncope.core.spring.security.AuthContextUtils;
31 import org.quartz.JobExecutionContext;
32 import org.quartz.JobExecutionException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36
37 public class TaskJob extends AbstractInterruptableJob {
38
39 private static final Logger LOG = LoggerFactory.getLogger(TaskJob.class);
40
41
42
43
44 public enum Status {
45
46 SUCCESS,
47 FAILURE
48
49 }
50
51 @Autowired
52 private DomainHolder domainHolder;
53
54 private SchedTaskJobDelegate delegate;
55
56 @Override
57 public JobDelegate getDelegate() {
58 return delegate;
59 }
60
61 @Override
62 public void execute(final JobExecutionContext context) throws JobExecutionException {
63 String taskKey = context.getMergedJobDataMap().getString(JobManager.TASK_KEY);
64 try {
65 String domain = context.getMergedJobDataMap().getString(JobManager.DOMAIN_KEY);
66 if (domainHolder.getDomains().containsKey(domain)) {
67 AuthContextUtils.callAsAdmin(domain, () -> {
68 try {
69 ImplementationDAO implementationDAO =
70 ApplicationContextProvider.getApplicationContext().getBean(ImplementationDAO.class);
71 Implementation impl = implementationDAO.find(
72 context.getMergedJobDataMap().getString(JobManager.DELEGATE_IMPLEMENTATION));
73 if (impl == null) {
74 LOG.error("Could not find Implementation '{}', aborting",
75 context.getMergedJobDataMap().getString(JobManager.DELEGATE_IMPLEMENTATION));
76 } else {
77 delegate = ImplementationManager.build(impl);
78 delegate.execute(
79 (TaskType) context.getMergedJobDataMap().get(JobManager.TASK_TYPE),
80 taskKey,
81 context.getMergedJobDataMap().getBoolean(JobManager.DRY_RUN_JOBDETAIL_KEY),
82 context);
83 }
84 } catch (Exception e) {
85 LOG.error("While executing task {}", taskKey, e);
86 throw new RuntimeException(e);
87 }
88
89 return null;
90 });
91 } else {
92 LOG.debug("Domain {} not found, skipping", domain);
93 }
94 } catch (RuntimeException e) {
95 LOG.error("While executing task {}", taskKey, e);
96 throw new JobExecutionException("While executing task " + taskKey, e);
97 }
98 }
99 }