1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.authority.yaml.swapper;
19
20 import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
21 import org.apache.shardingsphere.authority.constant.AuthorityOrder;
22 import org.apache.shardingsphere.authority.rule.builder.DefaultAuthorityRuleConfigurationBuilder;
23 import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
24 import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
25 import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
26 import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
27 import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
28
29 import java.util.Collection;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.stream.Collectors;
33
34
35
36
37 public final class YamlAuthorityRuleConfigurationSwapper implements YamlRuleConfigurationSwapper<YamlAuthorityRuleConfiguration, AuthorityRuleConfiguration> {
38
39 private final YamlUserSwapper userSwapper = new YamlUserSwapper();
40
41 private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
42
43 @Override
44 public YamlAuthorityRuleConfiguration swapToYamlConfiguration(final AuthorityRuleConfiguration data) {
45 YamlAuthorityRuleConfiguration result = new YamlAuthorityRuleConfiguration();
46 result.setPrivilege(algorithmSwapper.swapToYamlConfiguration(data.getPrivilegeProvider()));
47 result.setUsers(data.getUsers().stream().map(userSwapper::swapToYamlConfiguration).collect(Collectors.toList()));
48 result.setDefaultAuthenticator(data.getDefaultAuthenticator());
49 data.getAuthenticators().forEach((key, value) -> result.getAuthenticators().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
50 return result;
51 }
52
53 @Override
54 public AuthorityRuleConfiguration swapToObject(final YamlAuthorityRuleConfiguration yamlConfig) {
55 Collection<ShardingSphereUser> users = yamlConfig.getUsers().stream().map(userSwapper::swapToObject).collect(Collectors.toList());
56 AlgorithmConfiguration provider = algorithmSwapper.swapToObject(yamlConfig.getPrivilege());
57 if (null == provider) {
58 provider = new DefaultAuthorityRuleConfigurationBuilder().build().getPrivilegeProvider();
59 }
60 Map<String, AlgorithmConfiguration> authenticators = yamlConfig.getAuthenticators().entrySet().stream()
61 .collect(Collectors.toMap(Entry::getKey, entry -> algorithmSwapper.swapToObject(entry.getValue())));
62 return new AuthorityRuleConfiguration(users, provider, authenticators, yamlConfig.getDefaultAuthenticator());
63 }
64
65 @Override
66 public Class<AuthorityRuleConfiguration> getTypeClass() {
67 return AuthorityRuleConfiguration.class;
68 }
69
70 @Override
71 public String getRuleTagName() {
72 return "AUTHORITY";
73 }
74
75 @Override
76 public int getOrder() {
77 return AuthorityOrder.ORDER;
78 }
79 }