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.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   * Encrypt table changed processor.
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          // TODO refactor DistSQL to only persist config
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  }