You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/04/30 03:42:33 UTC
[shardingsphere] branch master updated: Add GlobalRuleBuilderFactory (#17219)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 a86b8467700 Add GlobalRuleBuilderFactory (#17219)
a86b8467700 is described below
commit a86b8467700b29e87f7dcf0eba9a29ea4d5d503b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 30 11:42:27 2022 +0800
Add GlobalRuleBuilderFactory (#17219)
---
.../builder/global/GlobalRuleBuilderFactory.java | 61 ++++++++++++++++++++++
.../rule/builder/global/GlobalRulesBuilder.java | 5 +-
2 files changed, 63 insertions(+), 3 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRuleBuilderFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRuleBuilderFactory.java
new file mode 100644
index 00000000000..4f1d7d8b472
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRuleBuilderFactory.java
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.infra.rule.builder.global;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+/**
+ * Global rule builder factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class GlobalRuleBuilderFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(GlobalRuleBuilder.class);
+ }
+
+ /**
+ * Create new instances of global rule builder map.
+ *
+ * @param ruleConfigs rule configurations
+ * @return new instance of global rule builder map
+ */
+ @SuppressWarnings("rawtypes")
+ public static Map<RuleConfiguration, GlobalRuleBuilder> newInstanceMap(final Collection<RuleConfiguration> ruleConfigs) {
+ return OrderedSPIRegistry.getRegisteredServices(GlobalRuleBuilder.class, ruleConfigs);
+ }
+
+ /**
+ * Create new instances of global rule builder.
+ *
+ * @param builderClasses builder classes
+ * @return new instance of global rule builders
+ */
+ @SuppressWarnings("rawtypes")
+ public static Collection<GlobalRuleBuilder> newInstances(final Collection<Class<GlobalRuleBuilder>> builderClasses) {
+ return OrderedSPIRegistry.getRegisteredServices(GlobalRuleBuilder.class).stream().filter(each -> !builderClasses.contains(each.getClass())).collect(Collectors.toList());
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
index 9f049838d9f..24195ae86f9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/global/GlobalRulesBuilder.java
@@ -39,7 +39,6 @@ import java.util.stream.Collectors;
public final class GlobalRulesBuilder {
static {
- ShardingSphereServiceLoader.register(GlobalRuleBuilder.class);
ShardingSphereServiceLoader.register(DefaultGlobalRuleConfigurationBuilder.class);
}
@@ -62,7 +61,7 @@ public final class GlobalRulesBuilder {
@SuppressWarnings("rawtypes")
private static Map<RuleConfiguration, GlobalRuleBuilder> getRuleBuilderMap(final Collection<RuleConfiguration> globalRuleConfigs) {
Map<RuleConfiguration, GlobalRuleBuilder> result = new LinkedHashMap<>();
- result.putAll(OrderedSPIRegistry.getRegisteredServices(GlobalRuleBuilder.class, globalRuleConfigs));
+ result.putAll(GlobalRuleBuilderFactory.newInstanceMap(globalRuleConfigs));
result.putAll(getMissedDefaultRuleBuilderMap(result));
return result;
}
@@ -81,6 +80,6 @@ public final class GlobalRulesBuilder {
@SuppressWarnings({"unchecked", "rawtypes"})
private static Collection<GlobalRuleBuilder> getMissedDefaultRuleBuilders(final Collection<GlobalRuleBuilder> configuredBuilders) {
Collection<Class<GlobalRuleBuilder>> configuredBuilderClasses = configuredBuilders.stream().map(each -> (Class<GlobalRuleBuilder>) each.getClass()).collect(Collectors.toSet());
- return OrderedSPIRegistry.getRegisteredServices(GlobalRuleBuilder.class).stream().filter(each -> !configuredBuilderClasses.contains(each.getClass())).collect(Collectors.toList());
+ return GlobalRuleBuilderFactory.newInstances(configuredBuilderClasses);
}
}