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 java.util.Map;
22  import org.apache.syncope.core.provisioning.api.AuditManager;
23  import org.apache.syncope.core.provisioning.api.event.AfterHandlingEvent;
24  import org.apache.syncope.core.provisioning.api.job.JobManager;
25  import org.apache.syncope.core.provisioning.api.job.JobNamer;
26  import org.apache.syncope.core.provisioning.api.notification.NotificationManager;
27  import org.apache.syncope.core.spring.ApplicationContextProvider;
28  import org.apache.syncope.core.spring.security.AuthContextUtils;
29  import org.apache.syncope.core.spring.security.SecureRandomUtils;
30  import org.quartz.JobBuilder;
31  import org.quartz.JobDataMap;
32  import org.quartz.JobExecutionContext;
33  import org.quartz.JobExecutionException;
34  import org.quartz.Scheduler;
35  import org.quartz.SchedulerException;
36  import org.quartz.Trigger;
37  import org.quartz.TriggerBuilder;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.beans.factory.support.AbstractBeanDefinition;
42  import org.springframework.scheduling.quartz.SchedulerFactoryBean;
43  
44  /**
45   * Quartz job for asynchronous handling of notification / audit events.
46   * Instead of direct synchronous invocation - which occurs in the same transaction where the event is generated, the
47   * execution of the scheduled code happens in a new transaction.
48   */
49  public class AfterHandlingJob extends AbstractInterruptableJob {
50  
51      private static final Logger LOG = LoggerFactory.getLogger(AfterHandlingJob.class);
52  
53      public static void schedule(final SchedulerFactoryBean scheduler, final Map<String, Object> jobMap) {
54          @SuppressWarnings("unchecked")
55          AfterHandlingJob jobInstance = (AfterHandlingJob) ApplicationContextProvider.getBeanFactory().
56                  createBean(AfterHandlingJob.class, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false);
57          String jobName = AfterHandlingJob.class.getSimpleName() + SecureRandomUtils.generateRandomUUID();
58  
59          jobMap.put(JobManager.DOMAIN_KEY, AuthContextUtils.getDomain());
60  
61          ApplicationContextProvider.getBeanFactory().registerSingleton(jobName, jobInstance);
62  
63          JobBuilder jobDetailBuilder = JobBuilder.newJob(AfterHandlingJob.class).
64                  withIdentity(jobName, Scheduler.DEFAULT_GROUP).
65                  usingJobData(new JobDataMap(jobMap));
66  
67          TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger().
68                  withIdentity(JobNamer.getTriggerName(jobName), Scheduler.DEFAULT_GROUP).
69                  startNow();
70  
71          try {
72              scheduler.getScheduler().scheduleJob(jobDetailBuilder.build(), triggerBuilder.build());
73          } catch (SchedulerException e) {
74              LOG.error("Could not schedule, aborting", e);
75          }
76      }
77  
78      @Autowired
79      private NotificationManager notificationManager;
80  
81      @Autowired
82      private AuditManager auditManager;
83  
84      @Override
85      public void execute(final JobExecutionContext context) throws JobExecutionException {
86          try {
87              AuthContextUtils.callAsAdmin(context.getMergedJobDataMap().getString(JobManager.DOMAIN_KEY),
88                      () -> {
89                          notificationManager.createTasks(
90                                  (AfterHandlingEvent) context.getMergedJobDataMap().get(AfterHandlingEvent.JOBMAP_KEY));
91                          auditManager.audit(
92                                  (AfterHandlingEvent) context.getMergedJobDataMap().get(AfterHandlingEvent.JOBMAP_KEY));
93                          return null;
94                      });
95          } catch (RuntimeException e) {
96              throw new JobExecutionException("While handling notification / audit events", e);
97          } finally {
98              ApplicationContextProvider.getBeanFactory().destroySingleton(context.getJobDetail().getKey().getName());
99          }
100     }
101 }