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.notification;
20  
21  import java.util.Optional;
22  import org.apache.syncope.core.persistence.api.DomainHolder;
23  import org.apache.syncope.core.provisioning.api.job.JobDelegate;
24  import org.apache.syncope.core.provisioning.api.job.JobManager;
25  import org.apache.syncope.core.provisioning.api.notification.NotificationJobDelegate;
26  import org.apache.syncope.core.provisioning.java.job.AbstractInterruptableJob;
27  import org.apache.syncope.core.spring.security.AuthContextUtils;
28  import org.apache.syncope.core.spring.security.SecurityProperties;
29  import org.quartz.JobExecutionContext;
30  import org.quartz.JobExecutionException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Periodically checks for notification to send.
36   *
37   * @see org.apache.syncope.core.persistence.api.entity.task.NotificationTask
38   */
39  public class NotificationJob extends AbstractInterruptableJob {
40  
41      public enum Status {
42  
43          SENT,
44          NOT_SENT
45  
46      }
47  
48      protected static final Logger LOG = LoggerFactory.getLogger(NotificationJob.class);
49  
50      public static final String DEFAULT_CRON_EXP = "0 0/5 * * * ?";
51  
52      protected final SecurityProperties securityProperties;
53  
54      protected final DomainHolder domainHolder;
55  
56      protected final NotificationJobDelegate delegate;
57  
58      public NotificationJob(
59              final SecurityProperties securityProperties,
60              final DomainHolder domainHolder,
61              final NotificationJobDelegate delegate) {
62  
63          this.securityProperties = securityProperties;
64          this.domainHolder = domainHolder;
65          this.delegate = delegate;
66      }
67  
68      @Override
69      public JobDelegate getDelegate() {
70          return delegate;
71      }
72  
73      @Override
74      public void execute(final JobExecutionContext context) throws JobExecutionException {
75          LOG.debug("Waking up...");
76          String executor = Optional.ofNullable(context.getMergedJobDataMap().getString(JobManager.EXECUTOR_KEY)).
77                  orElse(securityProperties.getAdminUser());
78          for (String domain : domainHolder.getDomains().keySet()) {
79              try {
80                  AuthContextUtils.callAsAdmin(domain, () -> {
81                      try {
82                          delegate.execute(executor);
83                      } catch (Exception e) {
84                          LOG.error("While sending out notifications", e);
85                          throw new RuntimeException(e);
86                      }
87  
88                      return null;
89                  });
90              } catch (RuntimeException e) {
91                  LOG.error("While sending out notifications", e);
92                  throw new JobExecutionException("While sending out notifications", e);
93              }
94          }
95  
96          LOG.debug("Sleeping again...");
97      }
98  }