1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.encrypt.rule.changed;
19
20 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
21 import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
22 import org.apache.shardingsphere.encrypt.metadata.nodepath.EncryptRuleNodePathProvider;
23 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
24 import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
25 import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper;
26 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
27 import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
28 import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent;
29 import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
30 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
31 import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
32
33 import java.util.LinkedHashMap;
34 import java.util.LinkedList;
35
36
37
38
39 public final class EncryptTableChangedProcessor implements RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, EncryptTableRuleConfiguration> {
40
41 @Override
42 public EncryptTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
43 return new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class));
44 }
45
46 @Override
47 public EncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
48 return database.getRuleMetaData().findSingleRule(EncryptRule.class)
49 .map(optional -> getEncryptRuleConfiguration(optional.getConfiguration()))
50 .orElseGet(() -> new EncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
51 }
52
53 private EncryptRuleConfiguration getEncryptRuleConfiguration(final EncryptRuleConfiguration config) {
54 return null == config.getTables() ? new EncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config;
55 }
56
57 @Override
58 public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig, final EncryptTableRuleConfiguration toBeChangedItemConfig) {
59
60 currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
61 currentRuleConfig.getTables().add(toBeChangedItemConfig);
62 }
63
64 @Override
65 public void dropRuleItemConfiguration(final DropRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig) {
66 currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName()));
67 }
68
69 @Override
70 public String getType() {
71 return EncryptRuleNodePathProvider.RULE_TYPE + "." + EncryptRuleNodePathProvider.TABLES;
72 }
73 }