You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/05/09 12:25:02 UTC

[shardingsphere] branch master updated: Use ShardingSphere SPI in ExampleScenarioFactory (#17493)

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

totalo 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 24fc19affa4 Use ShardingSphere SPI in ExampleScenarioFactory (#17493)
24fc19affa4 is described below

commit 24fc19affa4fd33b2685dbce4b4d3b2abf23111d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon May 9 20:24:55 2022 +0800

    Use ShardingSphere SPI in ExampleScenarioFactory (#17493)
    
    * Use ShardingSphere SPI in ExampleGenerator
    
    * Use ShardingSphere SPI in ExampleScenarioFactory
---
 .../generator/scenario/ExampleScenario.java        | 11 +++-----
 .../generator/scenario/ExampleScenarioFactory.java | 31 +++++++++-------------
 2 files changed, 16 insertions(+), 26 deletions(-)

diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenario.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenario.java
index 3e7cfe6735d..011d6847bfe 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenario.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenario.java
@@ -17,13 +17,15 @@
 
 package org.apache.shardingsphere.example.generator.scenario;
 
+import org.apache.shardingsphere.spi.type.typed.TypedSPI;
+
 import java.util.Collection;
 import java.util.Map;
 
 /**
  * Example scenario.
  */
-public interface ExampleScenario {
+public interface ExampleScenario extends TypedSPI {
     
     /**
      * Get java class template map.
@@ -52,11 +54,4 @@ public interface ExampleScenario {
      * @return resource paths
      */
     Collection<String> getResourcePaths();
-    
-    /**
-     * Get scenario type.
-     * 
-     * @return scenario type
-     */
-    String getType();
 }
diff --git a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenarioFactory.java b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenarioFactory.java
index 076366a10a2..e6e07ab2b06 100644
--- a/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenarioFactory.java
+++ b/examples/shardingsphere-example-generator/src/main/java/org/apache/shardingsphere/example/generator/scenario/ExampleScenarioFactory.java
@@ -19,13 +19,16 @@ package org.apache.shardingsphere.example.generator.scenario;
 
 import org.apache.shardingsphere.example.generator.scenario.feature.FeatureExampleScenario;
 import org.apache.shardingsphere.example.generator.scenario.framework.FrameworkExampleScenario;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
 
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedList;
 import java.util.Map;
-import java.util.ServiceLoader;
+import java.util.stream.Collectors;
 
 /**
  * Example scenario factory.
@@ -36,31 +39,23 @@ public final class ExampleScenarioFactory {
     
     private final FrameworkExampleScenario frameworkScenario;
     
+    static {
+        ShardingSphereServiceLoader.register(FeatureExampleScenario.class);
+        ShardingSphereServiceLoader.register(FrameworkExampleScenario.class);
+    }
+    
     public ExampleScenarioFactory(final String feature, final String framework) {
         featureScenarios = getFeatureScenarios(feature);
         frameworkScenario = getFrameworkScenario(framework);
     }
     
     private Collection<FeatureExampleScenario> getFeatureScenarios(final String feature) {
-        Collection<FeatureExampleScenario> result = new LinkedList<>();
-        if (null == feature) {
-            return result;
-        }
-        for (FeatureExampleScenario each : ServiceLoader.load(FeatureExampleScenario.class)) {
-            if (feature.contains(each.getType())) {
-                result.add(each);
-            }
-        }
-        return result;
+        return null == feature ? Collections.emptyList() : 
+                Arrays.stream(feature.split(",")).map(each -> TypedSPIRegistry.getRegisteredService(FeatureExampleScenario.class, each.trim())).collect(Collectors.toList());
     }
     
     private FrameworkExampleScenario getFrameworkScenario(final String framework) {
-        for (FrameworkExampleScenario each : ServiceLoader.load(FrameworkExampleScenario.class)) {
-            if (each.getType().equals(framework)) {
-                return each;
-            }
-        }
-        throw new UnsupportedOperationException(String.format("Can not support example scenario with framework `%s`.", framework));
+        return TypedSPIRegistry.getRegisteredService(FrameworkExampleScenario.class, framework);
     }
     
     /**