You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/08/28 02:18:36 UTC

[shardingsphere] branch master updated: Add OptionalSPI (#12072)

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

wuweijie 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 17c7f42  Add OptionalSPI (#12072)
17c7f42 is described below

commit 17c7f424b352f5eff53d1d7db86f5617b0482ae8
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Aug 28 10:17:56 2021 +0800

    Add OptionalSPI (#12072)
---
 .../infra/aware/DataSourceNameAware.java           |  3 +-
 .../infra/aware/DataSourceNameAwareFactory.java    |  4 +--
 .../shardingsphere/spi/optional/OptionalSPI.java   | 33 ++----------------
 .../spi/optional/OptionalSPIRegistry.java          | 39 +++++++++-------------
 4 files changed, 22 insertions(+), 57 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
index 0720951..bc736c9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
@@ -18,13 +18,14 @@
 package org.apache.shardingsphere.infra.aware;
 
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.spi.optional.OptionalSPI;
 
 import java.util.Collection;
 
 /**
  * Data source name aware.
  */
-public interface DataSourceNameAware {
+public interface DataSourceNameAware extends OptionalSPI {
     
     /**
      * Set rule.
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAwareFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAwareFactory.java
index d56f785..cf7416d 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAwareFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAwareFactory.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.infra.aware;
 
 import lombok.Getter;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.optional.OptionalSPIRegistry;
 
 import java.util.Optional;
 
@@ -33,7 +33,7 @@ public final class DataSourceNameAwareFactory {
     private final DataSourceNameAware dataSourceNameAware;
     
     private DataSourceNameAwareFactory() {
-        dataSourceNameAware = ShardingSphereServiceLoader.newServiceInstances(DataSourceNameAware.class).stream().findFirst().orElse(null);
+        dataSourceNameAware = OptionalSPIRegistry.findRegisteredService(DataSourceNameAware.class).orElse(null);
     }
     
     /**
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPI.java
similarity index 51%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
copy to shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPI.java
index 0720951..1f49ced 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPI.java
@@ -15,37 +15,10 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.infra.aware;
-
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-
-import java.util.Collection;
+package org.apache.shardingsphere.spi.optional;
 
 /**
- * Data source name aware.
+ * Optional SPI.
  */
-public interface DataSourceNameAware {
-    
-    /**
-     * Set rule.
-     *
-     * @param rule rule
-     */
-    void setRule(ShardingSphereRule rule);
-    
-    /**
-     * Get primary data source name.
-     *
-     * @param dataSourceName data source name
-     * @return primary data source name
-     */
-    String getPrimaryDataSourceName(String dataSourceName);
-    
-    /**
-     * Get replica data source names.
-     *
-     * @param dataSourceName data source name
-     * @return replica data source names
-     */
-    Collection<String> getReplicaDataSourceNames(String dataSourceName);
+public interface OptionalSPI {
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPIRegistry.java
similarity index 53%
copy from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
copy to shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPIRegistry.java
index 0720951..bdf9af5 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/aware/DataSourceNameAware.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/optional/OptionalSPIRegistry.java
@@ -15,37 +15,28 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.infra.aware;
+package org.apache.shardingsphere.spi.optional;
 
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 
-import java.util.Collection;
+import java.util.Optional;
 
 /**
- * Data source name aware.
+ * Optional SPI registry.
  */
-public interface DataSourceNameAware {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class OptionalSPIRegistry {
     
     /**
-     * Set rule.
+     * Find registered service.
      *
-     * @param rule rule
+     * @param optionalSPIClass optional SPI class
+     * @param <T> type
+     * @return registered service
      */
-    void setRule(ShardingSphereRule rule);
-    
-    /**
-     * Get primary data source name.
-     *
-     * @param dataSourceName data source name
-     * @return primary data source name
-     */
-    String getPrimaryDataSourceName(String dataSourceName);
-    
-    /**
-     * Get replica data source names.
-     *
-     * @param dataSourceName data source name
-     * @return replica data source names
-     */
-    Collection<String> getReplicaDataSourceNames(String dataSourceName);
+    public static <T extends OptionalSPI> Optional<T> findRegisteredService(final Class<T> optionalSPIClass) {
+        return ShardingSphereServiceLoader.newServiceInstances(optionalSPIClass).stream().findFirst();
+    }
 }