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.content;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.fasterxml.jackson.databind.json.JsonMapper;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Iterator;
26  import java.util.Map;
27  import javax.sql.DataSource;
28  import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
29  import org.apache.syncope.core.persistence.api.content.ConfParamLoader;
30  import org.apache.syncope.core.spring.ApplicationContextProvider;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Initialize Keymaster with default content if no data is present already.
36   */
37  public class KeymasterConfParamLoader implements ConfParamLoader {
38  
39      protected static final Logger LOG = LoggerFactory.getLogger(KeymasterConfParamLoader.class);
40  
41      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
42  
43      protected final ConfParamOps confParamOps;
44  
45      public KeymasterConfParamLoader(final ConfParamOps confParamOps) {
46          this.confParamOps = confParamOps;
47      }
48  
49      @Override
50      public int getOrder() {
51          return 450;
52      }
53  
54      @Override
55      public void load(final String domain, final DataSource datasource) {
56          boolean existingData;
57          try {
58              existingData = !confParamOps.list(domain).isEmpty();
59          } catch (Exception e) {
60              LOG.error("[{}] Could not access Keymaster", domain, e);
61              existingData = true;
62          }
63  
64          if (existingData) {
65              LOG.info("[{}] Data found in Keymaster, leaving untouched", domain);
66          } else {
67              LOG.info("[{}] Empty Keymaster found, loading default content", domain);
68  
69              try {
70                  InputStream contentJSON = ApplicationContextProvider.getBeanFactory().
71                          getBean(domain + "KeymasterConfParamsJSON", InputStream.class);
72                  loadDefaultContent(domain, contentJSON);
73              } catch (Exception e) {
74                  LOG.error("[{}] While loading default Keymaster content", domain, e);
75              }
76          }
77      }
78  
79      protected void loadDefaultContent(final String domain, final InputStream contentJSON)
80              throws IOException {
81  
82          try (contentJSON) {
83              JsonNode content = MAPPER.readTree(contentJSON);
84              for (Iterator<Map.Entry<String, JsonNode>> itor = content.fields(); itor.hasNext();) {
85                  Map.Entry<String, JsonNode> param = itor.next();
86                  Object value = MAPPER.treeToValue(param.getValue(), Object.class);
87                  if (value != null) {
88                      confParamOps.set(domain, param.getKey(), value);
89                  }
90              }
91          }
92      }
93  }