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.persistence.jpa;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  import java.util.function.Function;
24  import java.util.stream.Collectors;
25  import org.apache.cxf.helpers.IOUtils;
26  import org.apache.syncope.common.keymaster.client.api.DomainOps;
27  import org.apache.syncope.common.keymaster.client.api.model.Domain;
28  import org.apache.syncope.core.persistence.api.DomainHolder;
29  import org.apache.syncope.core.persistence.api.DomainRegistry;
30  import org.apache.syncope.core.persistence.api.SyncopeCoreLoader;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  import org.springframework.core.Ordered;
34  import org.springframework.core.io.ResourceLoader;
35  
36  public class StartupDomainLoader implements SyncopeCoreLoader {
37  
38      protected static final Logger LOG = LoggerFactory.getLogger(StartupDomainLoader.class);
39  
40      protected final DomainOps domainOps;
41  
42      protected final DomainHolder domainHolder;
43  
44      protected final PersistenceProperties persistenceProperties;
45  
46      protected final ResourceLoader resourceLoader;
47  
48      protected final DomainRegistry domainRegistry;
49  
50      public StartupDomainLoader(
51              final DomainOps domainOps,
52              final DomainHolder domainHolder,
53              final PersistenceProperties persistenceProperties,
54              final ResourceLoader resourceLoader,
55              final DomainRegistry domainRegistry) {
56  
57          this.domainOps = domainOps;
58          this.domainHolder = domainHolder;
59          this.persistenceProperties = persistenceProperties;
60          this.resourceLoader = resourceLoader;
61          this.domainRegistry = domainRegistry;
62      }
63  
64      @Override
65      public int getOrder() {
66          return Ordered.LOWEST_PRECEDENCE;
67      }
68  
69      @Override
70      public void load() {
71          Map<String, Domain> keymasterDomains = domainOps.list().stream().
72                  collect(Collectors.toMap(Domain::getKey, Function.identity()));
73  
74          persistenceProperties.getDomain().stream().
75                  filter(d -> !domainHolder.getDomains().containsKey(d.getKey())).forEach(domainProps -> {
76  
77              if (keymasterDomains.containsKey(domainProps.getKey())) {
78                  LOG.info("Domain {} initialization", domainProps.getKey());
79  
80                  domainRegistry.register(keymasterDomains.get(domainProps.getKey()));
81  
82                  LOG.info("Domain {} successfully inited", domainProps.getKey());
83              } else {
84                  Domain.Builder builder = new Domain.Builder(domainProps.getKey()).
85                          adminPassword(domainProps.getAdminPassword()).
86                          adminCipherAlgorithm(domainProps.getAdminCipherAlgorithm()).
87                          jdbcDriver(domainProps.getJdbcDriver()).
88                          jdbcURL(domainProps.getJdbcURL()).
89                          dbSchema(domainProps.getDbSchema()).
90                          dbUsername(domainProps.getDbUsername()).
91                          dbPassword(domainProps.getDbPassword()).
92                          databasePlatform(domainProps.getDatabasePlatform()).
93                          orm(domainProps.getOrm()).
94                          poolMaxActive(domainProps.getPoolMaxActive()).
95                          poolMinIdle(domainProps.getPoolMinIdle()).
96                          auditSql(domainProps.getAuditSql());
97  
98                  try {
99                      builder.content(IOUtils.toString(
100                             resourceLoader.getResource(domainProps.getContent()).getInputStream()));
101                 } catch (IOException e) {
102                     LOG.error("While loading {}", domainProps.getContent(), e);
103                 }
104 
105                 try {
106                     builder.keymasterConfParams(IOUtils.toString(
107                             resourceLoader.getResource(domainProps.getKeymasterConfParams()).getInputStream()));
108                 } catch (IOException e) {
109                     LOG.error("While loading {}", domainProps.getKeymasterConfParams(), e);
110                 }
111 
112                 domainOps.create(builder.build());
113             }
114         });
115     }
116 }