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.report;
20  
21  import java.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  import org.apache.syncope.core.persistence.api.DomainHolder;
24  import org.apache.syncope.core.persistence.api.dao.ImplementationDAO;
25  import org.apache.syncope.core.persistence.api.entity.Implementation;
26  import org.apache.syncope.core.provisioning.api.job.JobDelegate;
27  import org.apache.syncope.core.provisioning.api.job.JobManager;
28  import org.apache.syncope.core.provisioning.api.job.report.ReportJobDelegate;
29  import org.apache.syncope.core.provisioning.java.job.AbstractInterruptableJob;
30  import org.apache.syncope.core.spring.ApplicationContextProvider;
31  import org.apache.syncope.core.spring.implementation.ImplementationManager;
32  import org.apache.syncope.core.spring.security.AuthContextUtils;
33  import org.quartz.JobExecutionContext;
34  import org.quartz.JobExecutionException;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.springframework.beans.factory.annotation.Autowired;
38  
39  /**
40   * Quartz job for executing a given report.
41   */
42  public class ReportJob extends AbstractInterruptableJob {
43  
44      private static final Logger LOG = LoggerFactory.getLogger(ReportJob.class);
45  
46      /**
47       * Report execution status.
48       */
49      public enum Status {
50  
51          SUCCESS,
52          FAILURE
53  
54      }
55  
56      private final Map<String, ReportJobDelegate> perContextReportJobDelegates = new ConcurrentHashMap<>();
57  
58      @Autowired
59      private DomainHolder domainHolder;
60  
61      private ReportJobDelegate delegate;
62  
63      @Override
64      public JobDelegate getDelegate() {
65          return delegate;
66      }
67  
68      @Override
69      public void execute(final JobExecutionContext context) throws JobExecutionException {
70          String reportKey = context.getMergedJobDataMap().getString(JobManager.REPORT_KEY);
71          try {
72              String domain = context.getMergedJobDataMap().getString(JobManager.DOMAIN_KEY);
73              if (domainHolder.getDomains().containsKey(domain)) {
74                  AuthContextUtils.callAsAdmin(domain, () -> {
75                      try {
76                          ImplementationDAO implementationDAO =
77                                  ApplicationContextProvider.getApplicationContext().getBean(ImplementationDAO.class);
78                          Implementation impl = implementationDAO.find(
79                                  context.getMergedJobDataMap().getString(JobManager.DELEGATE_IMPLEMENTATION));
80                          if (impl == null) {
81                              LOG.error("Could not find Implementation '{}', aborting",
82                                      context.getMergedJobDataMap().getString(JobManager.DELEGATE_IMPLEMENTATION));
83                          } else {
84                              delegate = ImplementationManager.buildReportJobDelegate(
85                                      impl,
86                                      () -> perContextReportJobDelegates.get(impl.getKey()),
87                                      instance -> perContextReportJobDelegates.put(impl.getKey(), instance)).
88                                      orElseThrow(() -> new IllegalArgumentException(
89                                      "Could not instantiate " + impl.getBody()));
90                              delegate.execute(
91                                      reportKey,
92                                      context.getMergedJobDataMap().getBoolean(JobManager.DRY_RUN_JOBDETAIL_KEY),
93                                      context);
94                          }
95                      } catch (Exception e) {
96                          LOG.error("While executing report {}", reportKey, e);
97                          throw new RuntimeException(e);
98                      }
99  
100                     return null;
101                 });
102             } else {
103                 LOG.debug("Domain {} not found, skipping", domain);
104             }
105         } catch (RuntimeException e) {
106             LOG.error("While executing report {}", reportKey, e);
107             throw new JobExecutionException("While executing report " + reportKey, e);
108         }
109     }
110 }