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.mode.metadata.builder;
19  
20  import com.google.common.base.Strings;
21  import org.apache.shardingsphere.mode.path.rule.RuleNodePath;
22  import org.apache.shardingsphere.mode.path.rule.item.NamedRuleItemNodePath;
23  import org.apache.shardingsphere.mode.path.rule.item.UniqueRuleItemNodePath;
24  import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
25  import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
26  import org.apache.shardingsphere.mode.event.DataChangedEvent;
27  import org.apache.shardingsphere.mode.event.NamedRuleItemChangedEventCreator;
28  import org.apache.shardingsphere.mode.event.UniqueRuleItemChangedEventCreator;
29  import org.apache.shardingsphere.mode.spi.RuleNodePathProvider;
30  
31  import java.util.Map.Entry;
32  import java.util.Optional;
33  
34  /**
35   * Rule configuration event builder.
36   */
37  public final class RuleConfigurationEventBuilder {
38      
39      /**
40       * Build rule changed event.
41       *
42       * @param databaseName database name
43       * @param event data changed event
44       * @return rule changed event
45       */
46      public Optional<GovernanceEvent> build(final String databaseName, final DataChangedEvent event) {
47          for (RuleNodePathProvider each : ShardingSphereServiceLoader.getServiceInstances(RuleNodePathProvider.class)) {
48              Optional<GovernanceEvent> result = build(each.getRuleNodePath(), databaseName, event);
49              if (result.isPresent()) {
50                  return result;
51              }
52          }
53          return Optional.empty();
54      }
55      
56      private Optional<GovernanceEvent> build(final RuleNodePath ruleNodePath, final String databaseName, final DataChangedEvent event) {
57          if (!ruleNodePath.getRoot().isValidatedPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
58              return Optional.empty();
59          }
60          for (Entry<String, NamedRuleItemNodePath> entry : ruleNodePath.getNamedItems().entrySet()) {
61              Optional<String> itemName = entry.getValue().getNameByActiveVersion(event.getKey());
62              if (itemName.isPresent()) {
63                  return Optional.of(new NamedRuleItemChangedEventCreator().create(databaseName, itemName.get(), event, ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
64              }
65          }
66          for (Entry<String, UniqueRuleItemNodePath> entry : ruleNodePath.getUniqueItems().entrySet()) {
67              if (entry.getValue().isActiveVersionPath(event.getKey())) {
68                  return Optional.of(new UniqueRuleItemChangedEventCreator().create(databaseName, event, ruleNodePath.getRoot().getRuleType() + "." + entry.getKey()));
69              }
70          }
71          return Optional.empty();
72      }
73  }