This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 0220ce46968 Add EncryptImportRuleConfigurationProvider (#30411)
0220ce46968 is described below

commit 0220ce46968843bdd61c9446209b794fd6778fa0
Author: yx9o <[email protected]>
AuthorDate: Thu Mar 7 10:31:50 2024 +0800

    Add EncryptImportRuleConfigurationProvider (#30411)
    
    * Add EncryptImportRuleConfigurationProvider
    
    * Update
---
 .../EncryptImportRuleConfigurationProvider.java    | 42 ++++++++++++++--------
 ...le.spi.database.ImportRuleConfigurationProvider | 18 ++++++++++
 .../YamlDatabaseConfigurationImportExecutor.java   | 27 ++++----------
 3 files changed, 51 insertions(+), 36 deletions(-)

diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/EncryptRuleConfigurationImportChecker.java
 
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptImportRuleConfigurationProvider.java
similarity index 73%
rename from 
proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/EncryptRuleConfigurationImportChecker.java
rename to 
features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptImportRuleConfigurationProvider.java
index 14276498b05..6f8efb580cf 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/EncryptRuleConfigurationImportChecker.java
+++ 
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/provider/EncryptImportRuleConfigurationProvider.java
@@ -15,17 +15,22 @@
  * limitations under the License.
  */
 
-package 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker;
+package org.apache.shardingsphere.encrypt.distsql.handler.provider;
 
+import 
org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider;
 import 
org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException;
 import 
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
 import 
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.instance.InstanceContext;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.util.Collection;
@@ -35,23 +40,25 @@ import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
- * Encrypt rule configuration import checker.
+ * Encrypt import rule configuration provider.
  */
-public final class EncryptRuleConfigurationImportChecker {
+public final class EncryptImportRuleConfigurationProvider implements 
ImportRuleConfigurationProvider {
     
-    /**
-     * Check encrypt rule configuration.
-     *
-     * @param database database
-     * @param currentRuleConfig current rule configuration
-     */
-    public void check(final ShardingSphereDatabase database, final 
EncryptRuleConfiguration currentRuleConfig) {
-        if (null == database || null == currentRuleConfig) {
+    @Override
+    public void check(final ShardingSphereDatabase database, final 
RuleConfiguration ruleConfig) {
+        if (null == database || null == ruleConfig) {
             return;
         }
-        checkTables(currentRuleConfig, database.getName());
-        checkEncryptors(currentRuleConfig);
-        checkTableEncryptorsExisted(currentRuleConfig, database.getName());
+        EncryptRuleConfiguration encryptRuleConfig = 
(EncryptRuleConfiguration) ruleConfig;
+        checkTables(encryptRuleConfig, database.getName());
+        checkEncryptors(encryptRuleConfig);
+        checkTableEncryptorsExisted(encryptRuleConfig, database.getName());
+    }
+    
+    @Override
+    public DatabaseRule build(final ShardingSphereDatabase database, final 
RuleConfiguration ruleConfig, final InstanceContext instanceContext) {
+        EncryptRuleConfiguration encryptRuleConfig = 
(EncryptRuleConfiguration) ruleConfig;
+        return new EncryptRule(database.getName(), encryptRuleConfig);
     }
     
     private void checkTables(final EncryptRuleConfiguration currentRuleConfig, 
final String databaseName) {
@@ -77,4 +84,9 @@ public final class EncryptRuleConfigurationImportChecker {
         notExistedEncryptors.removeIf(encryptors::contains);
         ShardingSpherePreconditions.checkState(notExistedEncryptors.isEmpty(), 
() -> new MissingRequiredAlgorithmException(databaseName, 
notExistedEncryptors));
     }
+    
+    @Override
+    public Class<? extends RuleConfiguration> getType() {
+        return EncryptRuleConfiguration.class;
+    }
 }
diff --git 
a/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
 
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
new file mode 100644
index 00000000000..544470758ce
--- /dev/null
+++ 
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.encrypt.distsql.handler.provider.EncryptImportRuleConfigurationProvider
diff --git 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
index 9c5d2dc9d86..5d9352320c7 100644
--- 
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
+++ 
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java
@@ -23,8 +23,6 @@ import 
org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.data
 import 
org.apache.shardingsphere.distsql.handler.exception.datasource.MissingRequiredDataSourcesException;
 import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
 import 
org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
-import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
-import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
@@ -52,13 +50,10 @@ import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceCo
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
 import 
org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.EncryptRuleConfigurationImportChecker;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.MaskRuleConfigurationImportChecker;
 import 
org.apache.shardingsphere.proxy.backend.handler.distsql.ral.common.checker.ShadowRuleConfigurationImportChecker;
-import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
 import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
 import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
 import org.apache.shardingsphere.single.rule.SingleRule;
 
@@ -78,8 +73,6 @@ import java.util.stream.Collectors;
  */
 public final class YamlDatabaseConfigurationImportExecutor {
     
-    private final EncryptRuleConfigurationImportChecker 
encryptRuleConfigImportChecker = new EncryptRuleConfigurationImportChecker();
-    
     private final ShadowRuleConfigurationImportChecker 
shadowRuleConfigImportChecker = new ShadowRuleConfigurationImportChecker();
     
     private final MaskRuleConfigurationImportChecker 
maskRuleConfigImportChecker = new MaskRuleConfigurationImportChecker();
@@ -163,14 +156,7 @@ public final class YamlDatabaseConfigurationImportExecutor 
{
             return;
         }
         InstanceContext instanceContext = 
ProxyContext.getInstance().getContextManager().getInstanceContext();
-        if (ruleConfig instanceof ShardingRuleConfiguration || ruleConfig 
instanceof ReadwriteSplittingRuleConfiguration) {
-            ImportRuleConfigurationProvider importRuleConfigurationProvider = 
TypedSPILoader.getService(ImportRuleConfigurationProvider.class, 
ruleConfig.getClass());
-            importRuleConfigurationProvider.check(database, ruleConfig);
-            allRuleConfigs.add(ruleConfig);
-            
database.getRuleMetaData().getRules().add(importRuleConfigurationProvider.build(database,
 ruleConfig, instanceContext));
-        } else if (ruleConfig instanceof EncryptRuleConfiguration) {
-            ruleConfigs.forEach(each -> 
addEncryptRuleConfiguration((EncryptRuleConfiguration) each, allRuleConfigs, 
database));
-        } else if (ruleConfig instanceof ShadowRuleConfiguration) {
+        if (ruleConfig instanceof ShadowRuleConfiguration) {
             ruleConfigs.forEach(each -> 
addShadowRuleConfiguration((ShadowRuleConfiguration) each, allRuleConfigs, 
database));
         } else if (ruleConfig instanceof MaskRuleConfiguration) {
             ruleConfigs.forEach(each -> 
addMaskRuleConfiguration((MaskRuleConfiguration) each, allRuleConfigs, 
database));
@@ -178,6 +164,11 @@ public final class YamlDatabaseConfigurationImportExecutor 
{
             ruleConfigs.forEach(each -> 
addBroadcastRuleConfiguration((BroadcastRuleConfiguration) each, 
allRuleConfigs, database));
         } else if (ruleConfig instanceof SingleRuleConfiguration) {
             ruleConfigs.forEach(each -> 
addSingleRuleConfiguration((SingleRuleConfiguration) each, allRuleConfigs, 
database));
+        } else {
+            ImportRuleConfigurationProvider importRuleConfigurationProvider = 
TypedSPILoader.getService(ImportRuleConfigurationProvider.class, 
ruleConfig.getClass());
+            importRuleConfigurationProvider.check(database, ruleConfig);
+            allRuleConfigs.add(ruleConfig);
+            
database.getRuleMetaData().getRules().add(importRuleConfigurationProvider.build(database,
 ruleConfig, instanceContext));
         }
     }
     
@@ -193,12 +184,6 @@ public final class YamlDatabaseConfigurationImportExecutor 
{
         return result;
     }
     
-    private void addEncryptRuleConfiguration(final EncryptRuleConfiguration 
encryptRuleConfig, final Collection<RuleConfiguration> allRuleConfigs, final 
ShardingSphereDatabase database) {
-        encryptRuleConfigImportChecker.check(database, encryptRuleConfig);
-        allRuleConfigs.add(encryptRuleConfig);
-        database.getRuleMetaData().getRules().add(new 
EncryptRule(database.getName(), encryptRuleConfig));
-    }
-    
     private void addShadowRuleConfiguration(final ShadowRuleConfiguration 
shadowRuleConfig, final Collection<RuleConfiguration> allRuleConfigs, final 
ShardingSphereDatabase database) {
         shadowRuleConfigImportChecker.check(database, shadowRuleConfig);
         allRuleConfigs.add(shadowRuleConfig);

Reply via email to