You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/08/27 22:32:20 UTC

[shardingsphere] branch master updated: Remove TypedSPI for DataSourceNameAware (#12071)

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

duanzhengqiang 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 4cc3179  Remove TypedSPI for DataSourceNameAware (#12071)
4cc3179 is described below

commit 4cc3179bd423bc0729511f42f4db0b3914708237
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Aug 28 06:31:43 2021 +0800

    Remove TypedSPI for DataSourceNameAware (#12071)
    
    * Remove TypedSPI for DataSourceNameAware
    
    * Refactor TypedSPIRegistry
---
 .../DatabaseDiscoveryDataSourceNameAware.java      |  5 ----
 .../infra/aware/DataSourceNameAware.java           |  3 +-
 .../infra/aware/DataSourceNameAwareFactory.java    | 15 ++++++++--
 .../shardingsphere/spi/typed/TypedSPIRegistry.java | 34 ++++++++--------------
 4 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/aware/DatabaseDiscoveryDataSourceNameAware.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/aware/DatabaseDiscoveryDataSourceNameAware.java
index 11b064a..d33ead9 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/aware/DatabaseDiscoveryDataSourceNameAware.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/aware/DatabaseDiscoveryDataSourceNameAware.java
@@ -44,9 +44,4 @@ public final class DatabaseDiscoveryDataSourceNameAware implements DataSourceNam
     public Collection<String> getReplicaDataSourceNames(final String dataSourceName) {
         return rule.getDataSourceRules().get(dataSourceName).getReplicaDataSourceNames();
     }
-    
-    @Override
-    public String getType() {
-        return "database-discovery";
-    }
 }
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 ef2683a..0720951 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,14 +18,13 @@
 package org.apache.shardingsphere.infra.aware;
 
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.spi.typed.TypedSPI;
 
 import java.util.Collection;
 
 /**
  * Data source name aware.
  */
-public interface DataSourceNameAware extends TypedSPI {
+public interface DataSourceNameAware {
     
     /**
      * 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 b6f636d..d56f785 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.typed.TypedSPIRegistry;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 
 import java.util.Optional;
 
@@ -30,10 +30,19 @@ public final class DataSourceNameAwareFactory {
     
     private static volatile DataSourceNameAwareFactory instance;
     
-    private final Optional<DataSourceNameAware> dataSourceNameAware;
+    private final DataSourceNameAware dataSourceNameAware;
     
     private DataSourceNameAwareFactory() {
-        dataSourceNameAware = TypedSPIRegistry.findRegisteredService(DataSourceNameAware.class);
+        dataSourceNameAware = ShardingSphereServiceLoader.newServiceInstances(DataSourceNameAware.class).stream().findFirst().orElse(null);
+    }
+    
+    /**
+     * Get data source name aware.
+     * 
+     * @return data source name aware
+     */
+    public Optional<DataSourceNameAware> getDataSourceNameAware() {
+        return Optional.ofNullable(dataSourceNameAware);
     }
     
     /**
diff --git a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
index a75e663..da22342 100644
--- a/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
+++ b/shardingsphere-spi/src/main/java/org/apache/shardingsphere/spi/typed/TypedSPIRegistry.java
@@ -42,23 +42,21 @@ public final class TypedSPIRegistry {
      */
     public static <T extends TypedSPI> Optional<T> findRegisteredService(final Class<T> typedSPIClass, final String type, final Properties props) {
         Optional<T> serviceInstance = ShardingSphereServiceLoader.newServiceInstances(typedSPIClass).stream().filter(each -> each.getType().equalsIgnoreCase(type)).findFirst();
-        if (serviceInstance.isPresent()) {
-            T result = serviceInstance.get();
-            convertPropertiesValueType(props, result);
-            return Optional.of(result);
+        if (!serviceInstance.isPresent()) {
+            return Optional.empty();
         }
-        return Optional.empty();
+        T result = serviceInstance.get();
+        setProperties(result, props);
+        return Optional.of(result);
     }
     
-    /**
-     * Find registered service.
-     *
-     * @param typedSPIClass typed SPI class
-     * @param <T> type
-     * @return registered service
-     */
-    public static <T extends TypedSPI> Optional<T> findRegisteredService(final Class<T> typedSPIClass) { 
-        return ShardingSphereServiceLoader.newServiceInstances(typedSPIClass).stream().findFirst();
+    private static <T extends TypedSPI> void setProperties(final T service, final Properties props) {
+        if (null == props) {
+            return;
+        }
+        Properties newProps = new Properties();
+        props.forEach((key, value) -> newProps.setProperty(key.toString(), null == value ? null : value.toString()));
+        service.setProps(newProps);
     }
     
     /**
@@ -92,12 +90,4 @@ public final class TypedSPIRegistry {
         }
         throw new ServiceProviderNotFoundException(typedSPIClass);
     }
-    
-    private static <T extends TypedSPI> void convertPropertiesValueType(final Properties props, final T service) {
-        if (null != props) {
-            Properties newProps = new Properties();
-            props.forEach((key, value) -> newProps.setProperty(key.toString(), null == value ? null : value.toString()));
-            service.setProps(newProps);
-        }
-    }
 }