1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.brabenetz.lib.securedproperties.config;
21
22 import net.brabenetz.lib.securedproperties.core.Algorithm;
23 import net.brabenetz.lib.securedproperties.core.SupportedAlgorithm;
24 import org.apache.commons.lang3.StringUtils;
25
26 import java.io.File;
27 import java.util.Arrays;
28 import java.util.EnumMap;
29 import java.util.Map;
30 import java.util.function.BiConsumer;
31 import java.util.function.Function;
32
33
34
35
36 public abstract class AbstractConfigInitializer implements ConfigInitializer {
37
38 private static final Map<ConfigKey, BiConsumer<Config, String>> CONFIG_KEY_TO_INIT_FUNCTION_MAPPING = initConfigMappings();
39
40 private final Function<ConfigKey, String> keyFactory;
41
42 protected AbstractConfigInitializer() {
43 this((final ConfigKey key) -> ConfigKey.DEFAULT_PREFIX_KEBAB_CASE + '.' + key.getKebabCase());
44 }
45
46 protected AbstractConfigInitializer(final Function<ConfigKey, String> keyFactory) {
47 this.keyFactory = keyFactory;
48 }
49
50
51
52
53 protected AbstractConfigInitializer(final String configKeyPrefix,
54 final Function<ConfigKey, String> keyFactoryWithoutPrefix,
55 final Function<ConfigKey, String> keyFactoryWithPrefix) {
56 if (StringUtils.isEmpty(configKeyPrefix)) {
57 keyFactory = keyFactoryWithoutPrefix;
58 } else {
59 keyFactory = keyFactoryWithPrefix;
60 }
61 }
62
63 protected Function<ConfigKey, String> getKeyFactory() {
64 return keyFactory;
65 }
66
67 @Override
68 public void init(final Config config) {
69 for (final ConfigKey configKey : ConfigKey.values()) {
70 final String value = getValue(configKey);
71 if (value != null) {
72 CONFIG_KEY_TO_INIT_FUNCTION_MAPPING.get(configKey).accept(config, value);
73 }
74 }
75 }
76
77 private static Map<ConfigKey, BiConsumer<Config, String>> initConfigMappings() {
78 final Map<ConfigKey, BiConsumer<Config, String>> mapping = new EnumMap<>(ConfigKey.class);
79 mapping.put(ConfigKey.SECRET_FILE, AbstractConfigInitializer::initSecretFilePath);
80 mapping.put(ConfigKey.SALT_LENGTH, AbstractConfigInitializer::initSaltLength);
81 mapping.put(ConfigKey.ALLOWED_ALGORITHM, AbstractConfigInitializer::initAllowedAlgorithm);
82 mapping.put(ConfigKey.AUTO_CREATE_SECRET_KEY, AbstractConfigInitializer::initAutoCreateSecretKey);
83 return mapping;
84
85 }
86
87 protected static void initSecretFilePath(final Config config, final String value) {
88 config.withSecretFile(new File(value));
89 }
90
91 protected static void initSaltLength(final Config config, final String value) {
92 config.withSaltLength(Integer.parseInt(value));
93 }
94
95 protected static void initAllowedAlgorithm(final Config config, final String value) {
96 config.withAllowedAlgorithm(Arrays.stream(value.split("[, ]+")).map(SupportedAlgorithm::valueOf).toArray(Algorithm[]::new));
97 }
98
99 protected static void initAutoCreateSecretKey(final Config config, final String value) {
100 config.withAutoCreateSecretKey(Boolean.valueOf(value));
101 }
102
103
104
105
106
107 protected abstract String getValue(ConfigKey key);
108 }