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 javax.sql.DataSource;
22  import org.apache.syncope.common.lib.types.AnyTypeKind;
23  import org.apache.syncope.core.persistence.api.SyncopeCoreLoader;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.core.Ordered;
27  
28  public class OpenSearchIndexLoader implements SyncopeCoreLoader {
29  
30      protected static final Logger LOG = LoggerFactory.getLogger(OpenSearchIndexLoader.class);
31  
32      protected final OpenSearchIndexManager indexManager;
33  
34      public OpenSearchIndexLoader(final OpenSearchIndexManager indexManager) {
35          this.indexManager = indexManager;
36      }
37  
38      @Override
39      public int getOrder() {
40          return Ordered.LOWEST_PRECEDENCE;
41      }
42  
43      @Override
44      public void load(final String domain, final DataSource datasource) {
45          try {
46              if (!indexManager.existsAnyIndex(domain, AnyTypeKind.USER)) {
47                  indexManager.createAnyIndex(domain, AnyTypeKind.USER,
48                          indexManager.defaultSettings(), indexManager.defaultAnyMapping());
49              }
50              if (!indexManager.existsAnyIndex(domain, AnyTypeKind.GROUP)) {
51                  indexManager.createAnyIndex(domain, AnyTypeKind.GROUP,
52                          indexManager.defaultSettings(), indexManager.defaultAnyMapping());
53              }
54              if (!indexManager.existsAnyIndex(domain, AnyTypeKind.ANY_OBJECT)) {
55                  indexManager.createAnyIndex(domain, AnyTypeKind.ANY_OBJECT,
56                          indexManager.defaultSettings(), indexManager.defaultAnyMapping());
57              }
58  
59              if (!indexManager.existsRealmIndex(domain)) {
60                  indexManager.createRealmIndex(domain,
61                          indexManager.defaultSettings(), indexManager.defaultRealmMapping());
62              }
63  
64              if (!indexManager.existsAuditIndex(domain)) {
65                  indexManager.createAuditIndex(domain,
66                          indexManager.defaultSettings(), indexManager.defaultAuditMapping());
67              }
68          } catch (Exception e) {
69              LOG.error("While creating indexes for domain {}", domain, e);
70          }
71      }
72  }