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 dcf78ec  Add JDBCDataSourceCreatorFactory (#14064)
dcf78ec is described below

commit dcf78ecfc8e2f131e7515502eba3aa785b22e723
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Dec 12 21:22:49 2021 +0800

    Add JDBCDataSourceCreatorFactory (#14064)
---
 .../jdbc/config/JDBCDataSourceConfiguration.java   | 15 +------
 .../jdbc/creator/JDBCDataSourceCreatorFactory.java | 46 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 13 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/config/JDBCDataSourceConfiguration.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/config/JDBCDataSourceConfiguration.java
index 17d3a98..5c6d7d4 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/config/JDBCDataSourceConfiguration.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/config/JDBCDataSourceConfiguration.java
@@ -17,27 +17,19 @@
 
 package org.apache.shardingsphere.infra.config.datasource.jdbc.config;
 
-import com.google.common.base.Preconditions;
-import 
org.apache.shardingsphere.infra.config.datasource.jdbc.creator.JDBCDataSourceCreator;
+import 
org.apache.shardingsphere.infra.config.datasource.jdbc.creator.JDBCDataSourceCreatorFactory;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 
 /**
  * JDBC data source configuration.
  */
 public abstract class JDBCDataSourceConfiguration {
     
-    static {
-        ShardingSphereServiceLoader.register(JDBCDataSourceCreator.class);
-    }
-    
     /**
      * Get type.
      *
@@ -109,9 +101,6 @@ public abstract class JDBCDataSourceConfiguration {
      * @throws SQLException SQL exception
      */
     public DataSource toDataSource() throws SQLException {
-        String type = getType();
-        Optional<JDBCDataSourceCreator> creator = 
TypedSPIRegistry.findRegisteredService(JDBCDataSourceCreator.class, type, null);
-        Preconditions.checkArgument(creator.isPresent(), "Unsupported data 
source type '%s'", type);
-        return creator.get().createDataSource(getParameter(), 
getDataSourceConfiguration());
+        return 
JDBCDataSourceCreatorFactory.getInstance(getType()).createDataSource(getParameter(),
 getDataSourceConfiguration());
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/creator/JDBCDataSourceCreatorFactory.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/creator/JDBCDataSourceCreatorFactory.java
new file mode 100644
index 0000000..40c3f91
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/jdbc/creator/JDBCDataSourceCreatorFactory.java
@@ -0,0 +1,46 @@
+/*
+ * 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.config.datasource.jdbc.creator;
+
+import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
+
+import java.util.Optional;
+
+/**
+ * JDBC data source creator factory.
+ */
+public final class JDBCDataSourceCreatorFactory {
+    
+    static {
+        ShardingSphereServiceLoader.register(JDBCDataSourceCreator.class);
+    }
+    
+    /**
+     * Get JDBC data source creator instance.
+     * 
+     * @param type type
+     * @return JDBC data source creator instance
+     */
+    public static JDBCDataSourceCreator getInstance(final String type) {
+        Optional<JDBCDataSourceCreator> result = 
TypedSPIRegistry.findRegisteredService(JDBCDataSourceCreator.class, type, null);
+        Preconditions.checkArgument(result.isPresent(), "Unsupported data 
source type '%s'", type);
+        return result.get();
+    }
+}

Reply via email to