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.sharding.rule.changed;
19  
20  import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
21  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
22  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent;
23  import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent;
24  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent;
25  import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent;
26  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
27  import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
28  import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
29  import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
30  import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
31  import org.apache.shardingsphere.sharding.metadata.nodepath.ShardingRuleNodePathProvider;
32  import org.apache.shardingsphere.sharding.rule.ShardingRule;
33  
34  /**
35   * Sharding auditor changed processor.
36   */
37  public final class ShardingAuditorChangedProcessor implements RuleItemConfigurationChangedProcessor<ShardingRuleConfiguration, AlgorithmConfiguration> {
38      
39      @Override
40      public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
41          return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class));
42      }
43      
44      @Override
45      public ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
46          return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(ShardingRule::getConfiguration).orElseGet(ShardingRuleConfiguration::new);
47      }
48      
49      @Override
50      public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) {
51          currentRuleConfig.getAuditors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig);
52      }
53      
54      @Override
55      public void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) {
56          currentRuleConfig.getAuditors().remove(((DropNamedRuleItemEvent) event).getItemName());
57      }
58      
59      @Override
60      public String getType() {
61          return ShardingRuleNodePathProvider.RULE_TYPE + "." + ShardingRuleNodePathProvider.AUDITORS;
62      }
63  }