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.Collections;
23  import java.util.List;
24  import java.util.concurrent.CopyOnWriteArrayList;
25  import org.apache.syncope.common.lib.to.PropagationStatus;
26  import org.apache.syncope.common.lib.types.ExecStatus;
27  import org.apache.syncope.core.persistence.api.entity.task.PropagationTask;
28  import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter;
29  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
30  import org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils;
31  import org.identityconnectors.framework.common.objects.ConnectorObject;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  public class DefaultPropagationReporter implements PropagationReporter {
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(DefaultPropagationReporter.class);
38  
39      protected final List<PropagationStatus> statuses = new CopyOnWriteArrayList<>();
40  
41      protected boolean add(final PropagationStatus status) {
42          return statuses.stream().anyMatch(item -> item.getResource().equals(status.getResource()))
43                  ? false
44                  : statuses.add(status);
45      }
46  
47      @Override
48      public void onSuccessOrNonPriorityResourceFailures(
49              final PropagationTaskInfo taskInfo,
50              final ExecStatus executionStatus,
51              final String failureReason,
52              final String fiql,
53              final ConnectorObject beforeObj,
54              final ConnectorObject afterObj) {
55  
56          PropagationStatus status = new PropagationStatus();
57          status.setResource(taskInfo.getResource().getKey());
58          status.setStatus(executionStatus);
59          status.setFailureReason(failureReason);
60  
61          if (beforeObj != null) {
62              status.setBeforeObj(ConnObjectUtils.getConnObjectTO(fiql, beforeObj.getAttributes()));
63          }
64  
65          if (afterObj != null) {
66              status.setAfterObj(ConnObjectUtils.getConnObjectTO(fiql, afterObj.getAttributes()));
67          }
68  
69          add(status);
70      }
71  
72      @Override
73      public void onPriorityResourceFailure(
74              final String failingResource,
75              final Collection<PropagationTaskInfo> taskInfos) {
76  
77          LOG.debug("Propagation error: {} priority resource failed to propagate", failingResource);
78  
79          taskInfos.stream().filter(task -> task.getResource().getKey().equals(failingResource)).findFirst().
80                  ifPresentOrElse(task -> {
81                      PropagationStatus status = new PropagationStatus();
82                      status.setResource(task.getResource().getKey());
83                      status.setStatus(ExecStatus.FAILURE);
84                      status.setFailureReason(
85                              "Propagation error: " + failingResource + " priority resource failed to propagate.");
86                      add(status);
87                  }, () -> LOG.error("Could not find {} for {}", PropagationTask.class.getName(), failingResource));
88      }
89  
90      @Override
91      public List<PropagationStatus> getStatuses() {
92          return Collections.unmodifiableList(statuses);
93      }
94  }