View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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       * Task execution status.
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  }