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 2023/02/04 13:15:47 UTC

[shardingsphere] branch master updated: Add TypedSPILoader.getAllServices (#24002)

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 9509fa9ef28 Add TypedSPILoader.getAllServices (#24002)
9509fa9ef28 is described below

commit 9509fa9ef286b8fb8d76c74e7b6cb65e1f702ef1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Feb 4 21:15:40 2023 +0800

    Add TypedSPILoader.getAllServices (#24002)
    
    * Refactor EncryptTable
    
    * Add TypedSPILoader.getAllServices
    
    * Add TypedSPILoader.getAllServices
---
 .../apache/shardingsphere/encrypt/rule/EncryptTable.java    |  5 +++--
 .../algorithm/shadow/hint/SQLHintShadowAlgorithmTest.java   |  2 +-
 .../infra/util/spi/type/typed/TypedSPILoader.java           | 13 +++++++++++++
 .../infra/util/spi/type/typed/TypedSPILoaderTest.java       | 12 ++++++++++++
 4 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
index ecfc6089fa9..b9249b046a3 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptTable.java
@@ -45,8 +45,9 @@ public final class EncryptTable {
     private Map<String, EncryptColumn> createEncryptColumns(final EncryptTableRuleConfiguration config) {
         Map<String, EncryptColumn> result = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
         for (EncryptColumnRuleConfiguration each : config.getColumns()) {
-            result.put(each.getLogicColumn(), new EncryptColumn(each.getCipherColumn(), each.getAssistedQueryColumn(), each.getPlainColumn(), each.getLikeQueryColumn(),
-                    each.getEncryptorName(), each.getAssistedQueryEncryptorName(), each.getLikeQueryEncryptorName(), each.getQueryWithCipherColumn()));
+            EncryptColumn encryptColumn = new EncryptColumn(each.getCipherColumn(), each.getAssistedQueryColumn(), each.getPlainColumn(),
+                    each.getLikeQueryColumn(), each.getEncryptorName(), each.getAssistedQueryEncryptorName(), each.getLikeQueryEncryptorName(), each.getQueryWithCipherColumn());
+            result.put(each.getLogicColumn(), encryptColumn);
         }
         return result;
     }
diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SQLHintShadowAlgorithmTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SQLHintShadowAlgorithmTest.java
index 33fea9065db..9298b854804 100644
--- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SQLHintShadowAlgorithmTest.java
+++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/algorithm/shadow/hint/SQLHintShadowAlgorithmTest.java
@@ -36,7 +36,7 @@ public final class SQLHintShadowAlgorithmTest {
     
     @Before
     public void init() {
-        shadowAlgorithm = (SQLHintShadowAlgorithm) TypedSPILoader.getService(ShadowAlgorithm.class, "SQL_HINT", new Properties());    
+        shadowAlgorithm = (SQLHintShadowAlgorithm) TypedSPILoader.getService(ShadowAlgorithm.class, "SQL_HINT", new Properties());
     }
     
     @Test
diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
index f63b5950ddf..ea87f2bb92a 100644
--- a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
+++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoader.java
@@ -22,8 +22,10 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.infra.util.spi.exception.ServiceProviderNotFoundServerException;
 
+import java.util.Collection;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 /**
  * Typed SPI loader.
@@ -43,6 +45,17 @@ public final class TypedSPILoader {
         return ShardingSphereServiceLoader.getServiceInstances(spiClass).stream().anyMatch(each -> matchesType(type, each));
     }
     
+    /**
+     * Get all services.
+     * 
+     * @param spiClass SPI class
+     * @param <T> SPI class type 
+     * @return all services
+     */
+    public static <T extends TypedSPI> Collection<T> getAllServices(final Class<T> spiClass) {
+        return ShardingSphereServiceLoader.getServiceInstances(spiClass).stream().map(each -> getService(spiClass, each.getType())).collect(Collectors.toList());
+    }
+    
     /**
      * Find service.
      *
diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoaderTest.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoaderTest.java
index 54dbdb5f6b0..1f809fbc558 100644
--- a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoaderTest.java
+++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/spi/type/typed/TypedSPILoaderTest.java
@@ -29,11 +29,23 @@ import java.util.Properties;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 public final class TypedSPILoaderTest {
     
+    @Test
+    public void assertContains() {
+        assertTrue(TypedSPILoader.contains(TypedSPIFixture.class, "TYPED.FIXTURE"));
+        assertFalse(TypedSPILoader.contains(TypedSPIFixture.class, "NOT_EXISTED"));
+    }
+    
+    @Test
+    public void assertGetAllServices() {
+        assertThat(TypedSPILoader.getAllServices(TypedSPIFixture.class).size(), is(1));
+    }
+    
     @Test
     public void assertFindServiceWithoutProperties() {
         assertTrue(TypedSPILoader.findService(TypedSPIFixture.class, "TYPED.FIXTURE").isPresent());