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.sql.Connection;
22  import java.util.concurrent.atomic.AtomicReference;
23  import org.apache.syncope.core.persistence.api.DomainHolder;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.boot.actuate.health.Health;
27  import org.springframework.boot.actuate.health.HealthIndicator;
28  import org.springframework.boot.actuate.health.Status;
29  import org.springframework.jdbc.datasource.DataSourceUtils;
30  
31  public class DomainsHealthIndicator implements HealthIndicator {
32  
33      protected static final Logger LOG = LoggerFactory.getLogger(DomainsHealthIndicator.class);
34  
35      protected final DomainHolder domainHolder;
36  
37      public DomainsHealthIndicator(final DomainHolder domainHolder) {
38          this.domainHolder = domainHolder;
39      }
40  
41      @Override
42      public Health health() {
43          Health.Builder builder = new Health.Builder();
44  
45          AtomicReference<Boolean> anyDown = new AtomicReference<>(Boolean.FALSE);
46  
47          domainHolder.getDomains().forEach((key, ds) -> {
48              Status status;
49  
50              Connection conn = null;
51              try {
52                  conn = DataSourceUtils.getConnection(ds);
53                  status = conn.isValid(0) ? Status.UP : Status.OUT_OF_SERVICE;
54              } catch (Exception e) {
55                  status = Status.DOWN;
56                  LOG.debug("When attempting to connect to Domain {}", key, e);
57              } finally {
58                  if (conn != null) {
59                      DataSourceUtils.releaseConnection(conn, ds);
60                  }
61              }
62  
63              builder.withDetail(key, status);
64              if (status != Status.UP) {
65                  anyDown.set(true);
66              }
67          });
68  
69          builder.status(anyDown.get() ? Status.DOWN : Status.UP);
70  
71          return builder.build();
72      }
73  }