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.client.ui.commons.actuate;
20  
21  import org.apache.syncope.client.lib.AnonymousAuthenticationHandler;
22  import org.apache.syncope.client.lib.SyncopeClientFactoryBean;
23  import org.apache.syncope.common.keymaster.client.api.ServiceOps;
24  import org.apache.syncope.common.keymaster.client.api.model.NetworkService;
25  import org.apache.syncope.common.rest.api.service.UserSelfService;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.boot.actuate.health.Health;
29  import org.springframework.boot.actuate.health.HealthIndicator;
30  import org.springframework.boot.actuate.health.Status;
31  
32  public class SyncopeCoreHealthIndicator implements HealthIndicator {
33  
34      protected static final Logger LOG = LoggerFactory.getLogger(SyncopeCoreHealthIndicator.class);
35  
36      protected final ServiceOps serviceOps;
37  
38      protected final String anonymousUser;
39  
40      protected final String anonymousKey;
41  
42      protected final boolean useGZIPCompression;
43  
44      private UserSelfService service;
45  
46      public SyncopeCoreHealthIndicator(
47              final ServiceOps serviceOps,
48              final String anonymousUser,
49              final String anonymousKey,
50              final boolean useGZIPCompression) {
51  
52          this.serviceOps = serviceOps;
53          this.anonymousUser = anonymousUser;
54          this.anonymousKey = anonymousKey;
55          this.useGZIPCompression = useGZIPCompression;
56      }
57  
58      protected UserSelfService service() {
59          synchronized (this) {
60              if (service == null) {
61                  service = new SyncopeClientFactoryBean().
62                          setAddress(serviceOps.get(NetworkService.Type.CORE).getAddress()).
63                          setUseCompression(useGZIPCompression).
64                          create(new AnonymousAuthenticationHandler(anonymousUser, anonymousKey)).
65                          getService(UserSelfService.class);
66              }
67          }
68          return service;
69      }
70  
71      @Override
72      public Health health() {
73          Health.Builder builder = new Health.Builder();
74  
75          try {
76              service().read();
77              builder.status(Status.UP);
78          } catch (Exception e) {
79              LOG.debug("When attempting to connect to Syncope Core", e);
80              builder.status(Status.DOWN);
81          }
82  
83          return builder.build();
84      }
85  }