You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/05/10 04:06:35 UTC

[shardingsphere] branch master updated: Add DatabaseTypeFactory for SPI creation only (#17506)

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

panjuan 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 9e57a2e4e9b Add DatabaseTypeFactory for SPI creation only (#17506)
9e57a2e4e9b is described below

commit 9e57a2e4e9bcb5e072d40f1f0f893eee9af03aa1
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue May 10 12:06:26 2022 +0800

    Add DatabaseTypeFactory for SPI creation only (#17506)
---
 .../infra/database/type/DatabaseTypeFactory.java   | 55 ++++++++++++++++++++++
 .../infra/database/type/DatabaseTypeRegistry.java  | 18 ++-----
 2 files changed, 60 insertions(+), 13 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeFactory.java
new file mode 100644
index 00000000000..85efab6f83a
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeFactory.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.database.type;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
+
+import java.util.Collection;
+
+/**
+ * Database type factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DatabaseTypeFactory {
+    
+    static {
+        ShardingSphereServiceLoader.register(DatabaseType.class);
+    }
+    
+    /**
+     * Create new instance of database type.
+     * 
+     * @param name name of database type
+     * @return new instance of database type
+     */
+    public static DatabaseType newInstance(final String name) {
+        return TypedSPIRegistry.getRegisteredService(DatabaseType.class, name);
+    }
+    
+    /**
+     * Create new instances of database type.
+     * 
+     * @return new instances of database type
+     */
+    public static Collection<DatabaseType> newInstances() {
+        return ShardingSphereServiceLoader.getServiceInstances(DatabaseType.class);
+    }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeRegistry.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeRegistry.java
index 447dd6e016d..df494952c97 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeRegistry.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseTypeRegistry.java
@@ -19,9 +19,6 @@ package org.apache.shardingsphere.infra.database.type;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPI;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
 
 import java.util.Collection;
 import java.util.stream.Collectors;
@@ -34,10 +31,6 @@ public final class DatabaseTypeRegistry {
     
     private static final String DEFAULT_DATABASE_TYPE = "MySQL";
     
-    static {
-        ShardingSphereServiceLoader.register(DatabaseType.class);
-    }
-    
     /**
      * Get name of trunk database type.
      * 
@@ -55,7 +48,7 @@ public final class DatabaseTypeRegistry {
      * @return trunk database type
      */
     public static DatabaseType getTrunkDatabaseType(final String name) {
-        DatabaseType databaseType = TypedSPIRegistry.getRegisteredService(DatabaseType.class, name);
+        DatabaseType databaseType = DatabaseTypeFactory.newInstance(name);
         return databaseType instanceof BranchDatabaseType ? ((BranchDatabaseType) databaseType).getTrunkDatabaseType() : getActualDatabaseType(name);
     }
     
@@ -66,7 +59,7 @@ public final class DatabaseTypeRegistry {
      * @return actual database type
      */
     public static DatabaseType getActualDatabaseType(final String name) {
-        return TypedSPIRegistry.getRegisteredService(DatabaseType.class, name);
+        return DatabaseTypeFactory.newInstance(name);
     }
     
     /**
@@ -76,8 +69,7 @@ public final class DatabaseTypeRegistry {
      * @return database type
      */
     public static DatabaseType getDatabaseTypeByURL(final String url) {
-        Collection<DatabaseType> databaseType = ShardingSphereServiceLoader.getServiceInstances(DatabaseType.class);
-        return databaseType.stream().filter(each -> matchURLs(url, each)).findAny().orElseGet(() -> TypedSPIRegistry.getRegisteredService(DatabaseType.class, "SQL92"));
+        return DatabaseTypeFactory.newInstances().stream().filter(each -> matchURLs(url, each)).findAny().orElseGet(() -> DatabaseTypeFactory.newInstance("SQL92"));
     }
     
     private static boolean matchURLs(final String url, final DatabaseType databaseType) {
@@ -90,7 +82,7 @@ public final class DatabaseTypeRegistry {
      * @return default database type
      */
     public static DatabaseType getDefaultDatabaseType() {
-        return TypedSPIRegistry.getRegisteredService(DatabaseType.class, DEFAULT_DATABASE_TYPE);
+        return DatabaseTypeFactory.newInstance(DEFAULT_DATABASE_TYPE);
     }
     
     /**
@@ -99,6 +91,6 @@ public final class DatabaseTypeRegistry {
      * @return database type names
      */
     public static Collection<String> getDatabaseTypeNames() {
-        return ShardingSphereServiceLoader.getServiceInstances(DatabaseType.class).stream().map(TypedSPI::getType).collect(Collectors.toList());
+        return DatabaseTypeFactory.newInstances().stream().map(DatabaseType::getType).collect(Collectors.toList());
     }
 }