1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.InputStream;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.concurrent.Callable;
28 import javax.sql.DataSource;
29 import org.apache.syncope.common.keymaster.client.api.ConfParamOps;
30 import org.apache.syncope.core.persistence.api.content.ConfParamLoader;
31 import org.apache.syncope.core.spring.ApplicationContextProvider;
32 import org.apache.syncope.core.spring.security.AuthContextUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.transaction.annotation.Transactional;
36
37
38
39
40 public class KeymasterConfParamLoader implements ConfParamLoader {
41
42 protected static final Logger LOG = LoggerFactory.getLogger(KeymasterConfParamLoader.class);
43
44 protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
45
46 protected final ConfParamOps confParamOps;
47
48 public KeymasterConfParamLoader(final ConfParamOps confParamOps) {
49 this.confParamOps = confParamOps;
50 }
51
52 @Override
53 public int getOrder() {
54 return 450;
55 }
56
57 @Override
58 public void load(final String domain, final DataSource datasource) {
59 AuthContextUtils.callAsAdmin(domain, new Callable<Void>() {
60
61 @Transactional
62 @Override
63 public Void call() throws Exception {
64 boolean existingData;
65 try {
66 existingData = !confParamOps.list(domain).isEmpty();
67 } catch (Exception e) {
68 LOG.error("[{}] Could not access Keymaster", domain, e);
69 existingData = true;
70 }
71
72 if (existingData) {
73 LOG.info("[{}] Data found in Keymaster, leaving untouched", domain);
74 } else {
75 LOG.info("[{}] Empty Keymaster found, loading default content", domain);
76
77 try (InputStream contentJSON = ApplicationContextProvider.getBeanFactory().
78 getBean(domain + "KeymasterConfParamsJSON", InputStream.class)) {
79
80 JsonNode content = MAPPER.readTree(contentJSON);
81 for (Iterator<Map.Entry<String, JsonNode>> itor = content.fields(); itor.hasNext();) {
82 Map.Entry<String, JsonNode> param = itor.next();
83 Optional.ofNullable(MAPPER.treeToValue(param.getValue(), Object.class)).
84 ifPresent(value -> confParamOps.set(domain, param.getKey(), value));
85 }
86 } catch (Exception e) {
87 LOG.error("[{}] While loading default Keymaster content", domain, e);
88 }
89 }
90
91 return null;
92 }
93 });
94 }
95 }