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.propagation;
20  
21  import java.util.Collection;
22  import java.util.stream.Collectors;
23  import org.apache.syncope.core.persistence.api.entity.task.PropagationTask;
24  import org.apache.syncope.core.persistence.api.entity.task.TaskExec;
25  import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter;
26  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskCallable;
27  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor;
28  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
29  import org.apache.syncope.core.spring.security.AuthContextUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.beans.factory.annotation.Autowired;
33  import org.springframework.security.core.GrantedAuthority;
34  import org.springframework.security.core.context.SecurityContext;
35  import org.springframework.security.core.context.SecurityContextHolder;
36  
37  public class DefaultPropagationTaskCallable implements PropagationTaskCallable {
38  
39      protected static final Logger LOG = LoggerFactory.getLogger(PropagationTaskCallable.class);
40  
41      @Autowired
42      protected PropagationTaskExecutor taskExecutor;
43  
44      protected final String domain;
45  
46      protected final Collection<String> authorities;
47  
48      protected PropagationTaskInfo taskInfo;
49  
50      protected PropagationReporter reporter;
51  
52      protected String executor;
53  
54      public DefaultPropagationTaskCallable() {
55          SecurityContext ctx = SecurityContextHolder.getContext();
56          domain = AuthContextUtils.getDomain();
57          authorities = ctx.getAuthentication().getAuthorities().stream().
58                  map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
59      }
60  
61      @Override
62      public void setTaskInfo(final PropagationTaskInfo taskInfo) {
63          this.taskInfo = taskInfo;
64      }
65  
66      @Override
67      public void setReporter(final PropagationReporter reporter) {
68          this.reporter = reporter;
69      }
70  
71      @Override
72      public void setExecutor(final String executor) {
73          this.executor = executor;
74      }
75  
76      @Override
77      public TaskExec<PropagationTask> call() throws Exception {
78          return AuthContextUtils.callAs(domain, executor, authorities, () -> {
79              LOG.debug("Execution started for {}", taskInfo);
80  
81              TaskExec<PropagationTask> execution = taskExecutor.execute(taskInfo, reporter, executor);
82  
83              LOG.debug("Execution completed for {}, {}", taskInfo, execution);
84  
85              return execution;
86          });
87      }
88  }