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.mask.rule.changed;
19  
20  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
21  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
22  import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
23  import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
24  import org.apache.shardingsphere.mask.rule.MaskRule;
25  import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
26  import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
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   * Mask table changed processor.
35   */
36  public final class MaskTableChangedProcessor implements RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, MaskTableRuleConfiguration> {
37      
38      @Override
39      public MaskTableRuleConfiguration swapRuleItemConfiguration(final String itemName, final String yamlContent) {
40          return new YamlMaskTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlMaskTableRuleConfiguration.class));
41      }
42      
43      @Override
44      public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
45          return database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
46      }
47      
48      @Override
49      public void changeRuleItemConfiguration(final String itemName, final MaskRuleConfiguration currentRuleConfig, final MaskTableRuleConfiguration toBeChangedItemConfig) {
50          // TODO refactor DistSQL to only persist config
51          currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName()));
52          currentRuleConfig.getTables().add(toBeChangedItemConfig);
53      }
54      
55      @Override
56      public void dropRuleItemConfiguration(final String itemName, final MaskRuleConfiguration currentRuleConfig) {
57          currentRuleConfig.getTables().removeIf(each -> each.getName().equals(itemName));
58      }
59      
60      @Override
61      public RuleChangedItemType getType() {
62          return new RuleChangedItemType("mask", "tables");
63      }
64  }