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.starter.actuate;
20  
21  import java.util.Optional;
22  import java.util.concurrent.atomic.AtomicReference;
23  import java.util.stream.Stream;
24  import org.apache.syncope.common.keymaster.client.api.DomainOps;
25  import org.apache.syncope.common.keymaster.client.api.model.Domain;
26  import org.apache.syncope.common.lib.SyncopeConstants;
27  import org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO;
28  import org.apache.syncope.core.provisioning.api.ConnectorManager;
29  import org.apache.syncope.core.provisioning.api.data.ConnInstanceDataBinder;
30  import org.apache.syncope.core.spring.security.AuthContextUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.boot.actuate.health.Health;
34  import org.springframework.boot.actuate.health.HealthIndicator;
35  import org.springframework.boot.actuate.health.Status;
36  
37  public class ExternalResourcesHealthIndicator implements HealthIndicator {
38  
39      protected static final Logger LOG = LoggerFactory.getLogger(ExternalResourcesHealthIndicator.class);
40  
41      protected final DomainOps domainOps;
42  
43      protected final ExternalResourceDAO resourceDAO;
44  
45      protected final ConnInstanceDataBinder connInstanceDataBinder;
46  
47      protected final ConnectorManager connectorManager;
48  
49      public ExternalResourcesHealthIndicator(
50              final DomainOps domainOps,
51              final ExternalResourceDAO resourceDAO,
52              final ConnInstanceDataBinder connInstanceDataBinder,
53              final ConnectorManager connectorManager) {
54  
55          this.domainOps = domainOps;
56          this.resourceDAO = resourceDAO;
57          this.connInstanceDataBinder = connInstanceDataBinder;
58          this.connectorManager = connectorManager;
59      }
60  
61      @Override
62      public Health health() {
63          Health.Builder builder = new Health.Builder();
64  
65          AtomicReference<Boolean> anyDown = new AtomicReference<>(Boolean.FALSE);
66  
67          Stream.concat(Stream.of(SyncopeConstants.MASTER_DOMAIN), domainOps.list().stream().map(Domain::getKey)).
68                  forEach(domain -> AuthContextUtils.callAsAdmin(domain, () -> {
69  
70              resourceDAO.findAll().forEach(resource -> {
71                  Status status;
72                  try {
73                      connectorManager.createConnector(
74                              connectorManager.buildConnInstanceOverride(
75                                      connInstanceDataBinder.getConnInstanceTO(resource.getConnector()),
76                                      resource.getConfOverride(),
77                                      resource.isOverrideCapabilities()
78                                      ? Optional.of(resource.getCapabilitiesOverride()) : Optional.empty())).
79                              test();
80                      status = Status.UP;
81                  } catch (Exception e) {
82                      status = Status.DOWN;
83                      LOG.debug("When checking {} in Domain {}", resource.getKey(), domain, e);
84                  }
85  
86                  builder.withDetail(domain + "#" + resource.getKey(), status);
87                  if (status != Status.UP) {
88                      anyDown.set(true);
89                  }
90              });
91              return null;
92          }));
93  
94          builder.status(anyDown.get() ? Status.DOWN : Status.UP);
95  
96          return builder.build();
97      }
98  }