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

[shardingsphere] branch master updated: Use ShardingSphereSPI in DataSourcePropertyProviderFactory (#17494)

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

zhonghongsheng 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 0da12d86678 Use ShardingSphereSPI in DataSourcePropertyProviderFactory (#17494)
0da12d86678 is described below

commit 0da12d866781f1c9d66517e66d75f50bc7a98ab4
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon May 9 20:56:13 2022 +0800

    Use ShardingSphereSPI in DataSourcePropertyProviderFactory (#17494)
---
 .../swapper/DataSourcePropertyProvider.java        | 14 ++++++------
 ...java => DataSourcePropertyProviderFactory.java} | 25 ++++++++++------------
 .../jta/datasource/swapper/DataSourceSwapper.java  |  2 +-
 .../impl/DefaultDataSourcePropertyProvider.java    | 15 ++++++++-----
 .../swapper/impl/HikariCPPropertyProvider.java     | 10 ++++-----
 ...a.datasource.swapper.DataSourcePropertyProvider |  1 +
 ... => DataSourcePropertyProviderFactoryTest.java} |  6 +++---
 .../DefaultDataSourcePropertyProviderTest.java     |  5 -----
 .../swapper/impl/HikariCPPropertyProviderTest.java |  5 -----
 9 files changed, 37 insertions(+), 46 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProvider.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swappe [...]
index 68302bce30c..be6c4cf93ad 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProvider.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProvider.java
@@ -17,17 +17,15 @@
 
 package org.apache.shardingsphere.transaction.xa.jta.datasource.swapper;
 
+import org.apache.shardingsphere.spi.annotation.SingletonSPI;
+import org.apache.shardingsphere.spi.type.required.RequiredSPI;
+import org.apache.shardingsphere.spi.type.typed.TypedSPI;
+
 /**
  * Data source property provider.
  */
-public interface DataSourcePropertyProvider {
-    
-    /**
-     * Get data source class name.
-     *
-     * @return data source class name
-     */
-    String getDataSourceClassName();
+@SingletonSPI
+public interface DataSourcePropertyProvider extends TypedSPI, RequiredSPI {
     
     /**
      * Get URL property name.
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoader.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/ [...]
similarity index 54%
rename from shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoader.java
rename to shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderFactory.java
index 3e3c8334410..6c251135b70 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoader.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderFactory.java
@@ -19,33 +19,30 @@ package org.apache.shardingsphere.transaction.xa.jta.datasource.swapper;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.impl.DefaultDataSourcePropertyProvider;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.required.RequiredSPIRegistry;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
 
 import javax.sql.DataSource;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.ServiceLoader;
 
 /**
- * Data source property provider loader.
+ * Data source property provider factory.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class DataSourcePropertyProviderLoader {
-    
-    private static final Map<String, DataSourcePropertyProvider> DATA_SOURCE_PROPERTY_PROVIDERS = new HashMap<>();
+public final class DataSourcePropertyProviderFactory {
     
     static {
-        ServiceLoader.load(DataSourcePropertyProvider.class).forEach(each -> DATA_SOURCE_PROPERTY_PROVIDERS.put(each.getDataSourceClassName(), each));
+        ShardingSphereServiceLoader.register(DataSourcePropertyProvider.class);
     }
     
     /**
-     * Get data source property provider.
+     * Create new instance of data source property provider.
      *
      * @param dataSource data source
-     * @return data source property provider
+     * @return new instance of data source property provider
      */
-    public static DataSourcePropertyProvider getProvider(final DataSource dataSource) {
-        String dataSourceClassName = dataSource.getClass().getName();
-        return DATA_SOURCE_PROPERTY_PROVIDERS.containsKey(dataSourceClassName) ? DATA_SOURCE_PROPERTY_PROVIDERS.get(dataSourceClassName) : new DefaultDataSourcePropertyProvider();
+    public static DataSourcePropertyProvider newInstance(final DataSource dataSource) {
+        return TypedSPIRegistry.findRegisteredService(
+                DataSourcePropertyProvider.class, dataSource.getClass().getName()).orElseGet(() -> RequiredSPIRegistry.getRegisteredService(DataSourcePropertyProvider.class));
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSou [...]
index 86b244a7e52..0f7c216520e 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourceSwapper.java
@@ -98,7 +98,7 @@ public final class DataSourceSwapper {
     
     private Map<String, Object> getDatabaseAccessConfiguration(final DataSource dataSource) {
         Map<String, Object> result = new HashMap<>(3, 1);
-        DataSourcePropertyProvider provider = DataSourcePropertyProviderLoader.getProvider(dataSource);
+        DataSourcePropertyProvider provider = DataSourcePropertyProviderFactory.newInstance(dataSource);
         try {
             result.put("url", findGetterMethod(dataSource, provider.getURLPropertyName()).invoke(dataSource));
             result.put("user", findGetterMethod(dataSource, provider.getUsernamePropertyName()).invoke(dataSource));
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProvider.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datas [...]
index db90f8b1b15..5951663f3d2 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProvider.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProvider.java
@@ -24,11 +24,6 @@ import org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourc
  */
 public final class DefaultDataSourcePropertyProvider implements DataSourcePropertyProvider {
     
-    @Override
-    public String getDataSourceClassName() {
-        return "";
-    }
-    
     @Override
     public String getURLPropertyName() {
         return "url";
@@ -43,4 +38,14 @@ public final class DefaultDataSourcePropertyProvider implements DataSourceProper
     public String getPasswordPropertyName() {
         return "password";
     }
+    
+    @Override
+    public String getType() {
+        return "";
+    }
+    
+    @Override
+    public boolean isDefault() {
+        return true;
+    }
 }
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProvider.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swa [...]
index 2a9327c5f99..998cde693e1 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProvider.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProvider.java
@@ -24,11 +24,6 @@ import org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourc
  */
 public final class HikariCPPropertyProvider implements DataSourcePropertyProvider {
     
-    @Override
-    public String getDataSourceClassName() {
-        return "com.zaxxer.hikari.HikariDataSource";
-    }
-    
     @Override
     public String getURLPropertyName() {
         return "jdbcUrl";
@@ -43,4 +38,9 @@ public final class HikariCPPropertyProvider implements DataSourcePropertyProvide
     public String getPasswordPropertyName() {
         return "password";
     }
+    
+    @Override
+    public String getType() {
+        return "com.zaxxer.hikari.HikariDataSource";
+    }
 }
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourcePropertyProvider b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/resources/META-INF/services/org.apache.shardingsp [...]
index 333bffa3e67..d2bfd668711 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourcePropertyProvider
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.DataSourcePropertyProvider
@@ -15,4 +15,5 @@
 # limitations under the License.
 #
 
+org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.impl.DefaultDataSourcePropertyProvider
 org.apache.shardingsphere.transaction.xa.jta.datasource.swapper.impl.HikariCPPropertyProvider
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoaderTest.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasou [...]
similarity index 81%
rename from shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoaderTest.java
rename to shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderFactoryTest.java
index cac4d83a4c4..d1fd3fcc4d9 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderLoaderTest.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/DataSourcePropertyProviderFactoryTest.java
@@ -26,15 +26,15 @@ import org.junit.Test;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertThat;
 
-public final class DataSourcePropertyProviderLoaderTest {
+public final class DataSourcePropertyProviderFactoryTest {
     
     @Test
     public void assertGetProviderByDefault() {
-        assertThat(DataSourcePropertyProviderLoader.getProvider(new MockedDataSource()), instanceOf(DefaultDataSourcePropertyProvider.class));
+        assertThat(DataSourcePropertyProviderFactory.newInstance(new MockedDataSource()), instanceOf(DefaultDataSourcePropertyProvider.class));
     }
     
     @Test
     public void assertGetProviderBySPI() {
-        assertThat(DataSourcePropertyProviderLoader.getProvider(new HikariDataSource()), instanceOf(HikariCPPropertyProvider.class));
+        assertThat(DataSourcePropertyProviderFactory.newInstance(new HikariDataSource()), instanceOf(HikariCPPropertyProvider.class));
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProviderTest.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/d [...]
index 91753696dd1..0113709d475 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProviderTest.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/DefaultDataSourcePropertyProviderTest.java
@@ -24,11 +24,6 @@ import static org.junit.Assert.assertThat;
 
 public final class DefaultDataSourcePropertyProviderTest {
     
-    @Test
-    public void assertGetDataSourceClassName() {
-        assertThat(new DefaultDataSourcePropertyProvider().getDataSourceClassName(), is(""));
-    }
-    
     @Test
     public void assertGetURLPropertyName() {
         assertThat(new DefaultDataSourcePropertyProvider().getURLPropertyName(), is("url"));
diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProviderTest.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource [...]
index c13e0d00dde..d19074dcc9b 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProviderTest.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/swapper/impl/HikariCPPropertyProviderTest.java
@@ -24,11 +24,6 @@ import static org.junit.Assert.assertThat;
 
 public final class HikariCPPropertyProviderTest {
     
-    @Test
-    public void assertGetDataSourceClassName() {
-        assertThat(new HikariCPPropertyProvider().getDataSourceClassName(), is("com.zaxxer.hikari.HikariDataSource"));
-    }
-    
     @Test
     public void assertGetURLPropertyName() {
         assertThat(new HikariCPPropertyProvider().getURLPropertyName(), is("jdbcUrl"));