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