View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * YAML Authority rule configuration swapper.
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  }