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.config.EncryptRuleConfiguration;
21 import org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
22 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
23 import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration;
24 import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper;
25 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
27 import org.apache.shardingsphere.mode.spi.rule.RuleItemConfigurationChangedProcessor;
28 import org.apache.shardingsphere.mode.spi.rule.RuleChangedItemType;
29
30 import java.util.LinkedHashMap;
31 import java.util.LinkedList;
32
33
34
35
36 public final class EncryptTableChangedProcessor implements RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, EncryptTableRuleConfiguration> {
37
38 @Override
39 public EncryptTableRuleConfiguration swapRuleItemConfiguration(final String itemName, final String yamlContent) {
40 return new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class));
41 }
42
43 @Override
44 public EncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
45 return database.getRuleMetaData().findSingleRule(EncryptRule.class).map(EncryptRule::getConfiguration)
46 .orElseGet(() -> new EncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
47 }
48
49 @Override
50 public void changeRuleItemConfiguration(final String itemName, final EncryptRuleConfiguration currentRuleConfig, final EncryptTableRuleConfiguration toBeChangedItemConfig) {
51
52 currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
53 currentRuleConfig.getTables().add(toBeChangedItemConfig);
54 }
55
56 @Override
57 public void dropRuleItemConfiguration(final String itemName, final EncryptRuleConfiguration currentRuleConfig) {
58 currentRuleConfig.getTables().removeIf(each -> each.getName().equals(itemName));
59 }
60
61 @Override
62 public RuleChangedItemType getType() {
63 return new RuleChangedItemType("encrypt", "tables");
64 }
65 }