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.encrypt.rule.changed;
19  
20  import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
21  import org.apache.shardingsphere.encrypt.metadata.nodepath.EncryptRuleNodePathProvider;
22  import org.apache.shardingsphere.encrypt.rule.EncryptRule;
23  import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
24  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
25  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent;
26  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
27  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent;
28  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
29  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
30  import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
31  import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
32  import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
33  
34  import java.util.LinkedHashMap;
35  import java.util.LinkedList;
36  
37  /**
38   * Encryptor changed processor.
39   */
40  public final class EncryptorChangedProcessor implements RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, AlgorithmConfiguration> {
41      
42      @Override
43      public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
44          return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class));
45      }
46      
47      @Override
48      public EncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
49          return database.getRuleMetaData().findSingleRule(EncryptRule.class)
50                  .map(optional -> getEncryptRuleConfiguration(optional.getConfiguration()))
51                  .orElseGet(() -> new EncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
52      }
53      
54      private EncryptRuleConfiguration getEncryptRuleConfiguration(final EncryptRuleConfiguration config) {
55          return null == config.getTables() ? new EncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config;
56      }
57      
58      @Override
59      public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) {
60          currentRuleConfig.getEncryptors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig);
61      }
62      
63      @Override
64      public void dropRuleItemConfiguration(final DropRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig) {
65          currentRuleConfig.getEncryptors().remove(((DropNamedRuleItemEvent) event).getItemName());
66      }
67      
68      @Override
69      public String getType() {
70          return EncryptRuleNodePathProvider.RULE_TYPE + "." + EncryptRuleNodePathProvider.ENCRYPTORS;
71      }
72  }