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.ext.opensearch.client;
20  
21  import java.util.stream.Collectors;
22  import org.apache.http.HttpHost;
23  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
24  import org.apache.syncope.core.persistence.api.dao.GroupDAO;
25  import org.apache.syncope.core.persistence.api.dao.UserDAO;
26  import org.identityconnectors.common.CollectionUtil;
27  import org.opensearch.client.opensearch.OpenSearchClient;
28  import org.springframework.boot.actuate.health.HealthContributor;
29  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
30  import org.springframework.boot.context.properties.EnableConfigurationProperties;
31  import org.springframework.context.annotation.Bean;
32  import org.springframework.context.annotation.Configuration;
33  import org.springframework.context.annotation.Lazy;
34  
35  @EnableConfigurationProperties(OpenSearchProperties.class)
36  @Configuration(proxyBeanMethods = false)
37  public class OpenSearchClientContext {
38  
39      @ConditionalOnMissingBean
40      @Bean
41      public OpenSearchClientFactoryBean openSearchClientFactoryBean(final OpenSearchProperties props) {
42          return new OpenSearchClientFactoryBean(
43                  CollectionUtil.nullAsEmpty(props.getHosts()).stream().
44                          map(HttpHost::create).collect(Collectors.toList()));
45      }
46  
47      @ConditionalOnMissingBean
48      @Bean
49      public OpenSearchUtils openSearchUtils(
50              final @Lazy UserDAO userDAO,
51              final @Lazy GroupDAO groupDAO,
52              final @Lazy AnyObjectDAO anyObjectDAO) {
53  
54          return new OpenSearchUtils(userDAO, groupDAO, anyObjectDAO);
55      }
56  
57      @ConditionalOnMissingBean
58      @Bean
59      public OpenSearchIndexManager openSearchIndexManager(
60              final OpenSearchProperties props,
61              final OpenSearchClient client,
62              final OpenSearchUtils openSearchUtils) {
63  
64          return new OpenSearchIndexManager(
65                  client,
66                  openSearchUtils,
67                  props.getNumberOfShards(),
68                  props.getNumberOfReplicas());
69      }
70  
71      @ConditionalOnMissingBean
72      @Bean
73      public OpenSearchIndexLoader openSearchIndexLoader(final OpenSearchIndexManager indexManager) {
74          return new OpenSearchIndexLoader(indexManager);
75      }
76  
77      @ConditionalOnMissingBean(name = "syncopeOpenSearchHealthContributor")
78      @Bean(name = {
79          "syncopeOpenSearchHealthContributor", "openSearchHealthIndicator", "openSearchHealthContributor" })
80      public HealthContributor syncopeOpenSearchHealthContributor(final OpenSearchClient client) {
81          return new SyncopeOpenSearchHealthContributor(client);
82      }
83  }