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 2022/02/06 07:23:20 UTC

[shardingsphere] branch master updated: Remove DefaultDataSourcePoolMetaData (#15257)

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 60ab8e3  Remove DefaultDataSourcePoolMetaData (#15257)
60ab8e3 is described below

commit 60ab8e348995d0dcb5968914f2ca38d74033ec84
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Feb 6 15:22:28 2022 +0800

    Remove DefaultDataSourcePoolMetaData (#15257)
    
    * Remove DefaultDataSourcePoolMetaData
    
    * Remove DefaultDataSourcePoolMetaData
---
 .../pool/creator/DataSourcePoolCreator.java        | 21 ++++--
 .../pool/metadata/DataSourcePoolMetaData.java      |  3 +-
 .../metadata/DataSourcePoolMetaDataFactory.java    |  8 +--
 .../type/DefaultDataSourcePoolMetaData.java        | 81 ----------------------
 .../datasource/props/DataSourceProperties.java     | 10 ++-
 .../props/DataSourcePropertiesCreator.java         |  6 +-
 ...datasource.pool.metadata.DataSourcePoolMetaData |  1 -
 .../DataSourcePoolMetaDataFactoryTest.java         | 16 ++---
 8 files changed, 38 insertions(+), 108 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
index a6ebff9..14a9561 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourcePoolCreator.java
@@ -29,6 +29,7 @@ import javax.sql.DataSource;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
 
@@ -57,12 +58,16 @@ public final class DataSourcePoolCreator {
     @SuppressWarnings("rawtypes")
     public static DataSource create(final DataSourceProperties dataSourceProps) {
         DataSource result = createDataSource(dataSourceProps.getDataSourceClassName());
-        DataSourcePoolMetaData poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSourceProps.getDataSourceClassName());
+        Optional<DataSourcePoolMetaData> poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSourceProps.getDataSourceClassName());
         DataSourceReflection dataSourceReflection = new DataSourceReflection(result);
-        setDefaultFields(dataSourceReflection, poolMetaData);
-        setConfiguredFields(dataSourceProps, dataSourceReflection, poolMetaData);
-        appendJdbcUrlProperties(dataSourceProps.getCustomDataSourceProperties(), result, poolMetaData);
-        dataSourceReflection.addDefaultDataSourceProperties(poolMetaData);
+        if (poolMetaData.isPresent()) {
+            setDefaultFields(dataSourceReflection, poolMetaData.get());
+            setConfiguredFields(dataSourceProps, dataSourceReflection, poolMetaData.get());
+            appendJdbcUrlProperties(dataSourceProps.getCustomDataSourceProperties(), result, poolMetaData.get());
+            dataSourceReflection.addDefaultDataSourceProperties(poolMetaData.get());
+        } else {
+            setConfiguredFields(dataSourceProps, dataSourceReflection);
+        }
         return result;
     }
     
@@ -77,6 +82,12 @@ public final class DataSourcePoolCreator {
         }
     }
     
+    private static void setConfiguredFields(final DataSourceProperties dataSourceProps, final DataSourceReflection dataSourceReflection) {
+        for (Entry<String, Object> entry : dataSourceProps.getAllLocalProperties().entrySet()) {
+            dataSourceReflection.setField(entry.getKey(), entry.getValue());
+        }
+    }
+    
     private static void setConfiguredFields(final DataSourceProperties dataSourceProps, final DataSourceReflection dataSourceReflection, final DataSourcePoolMetaData<?> poolMetaData) {
         for (Entry<String, Object> entry : dataSourceProps.getAllLocalProperties().entrySet()) {
             String fieldName = entry.getKey();
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
index e0bbdc7..7e6f8ab 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaData.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.datasource.pool.metadata;
 
-import org.apache.shardingsphere.spi.required.RequiredSPI;
 import org.apache.shardingsphere.spi.typed.TypedSPI;
 
 import javax.sql.DataSource;
@@ -30,7 +29,7 @@ import java.util.Properties;
  * 
  * @param <T> type of target data source
  */
-public interface DataSourcePoolMetaData<T extends DataSource> extends TypedSPI, RequiredSPI {
+public interface DataSourcePoolMetaData<T extends DataSource> extends TypedSPI {
     
     /**
      * Get default properties.
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactory.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactory.java
index d5e1836..7896e42 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactory.java
@@ -20,9 +20,9 @@ package org.apache.shardingsphere.infra.datasource.pool.metadata;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.required.RequiredSPIRegistry;
 import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
 
+import java.util.Optional;
 import java.util.Properties;
 
 /**
@@ -41,8 +41,8 @@ public final class DataSourcePoolMetaDataFactory {
      * @param dataSourceClassName data source class name
      * @return new instance of data source pool meta data
      */
-    public static DataSourcePoolMetaData<?> newInstance(final String dataSourceClassName) {
-        return TypedSPIRegistry.findRegisteredService(
-                DataSourcePoolMetaData.class, dataSourceClassName, new Properties()).orElse(RequiredSPIRegistry.getRegisteredService(DataSourcePoolMetaData.class));
+    @SuppressWarnings("rawtypes")
+    public static Optional<DataSourcePoolMetaData> newInstance(final String dataSourceClassName) {
+        return TypedSPIRegistry.findRegisteredService(DataSourcePoolMetaData.class, dataSourceClassName, new Properties());
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/DefaultDataSourcePoolMetaData.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/DefaultDataSourcePoolMetaData.java
deleted file mode 100644
index 77f256c..0000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/DefaultDataSourcePoolMetaData.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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.datasource.pool.metadata.type;
-
-import org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
-
-import javax.sql.DataSource;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Properties;
-
-/**
- * Default data source pool meta data.
- */
-public final class DefaultDataSourcePoolMetaData implements DataSourcePoolMetaData<DataSource> {
-    
-    @Override
-    public Map<String, Object> getDefaultProperties() {
-        return Collections.emptyMap();
-    }
-    
-    @Override
-    public Map<String, Object> getInvalidProperties() {
-        return Collections.emptyMap();
-    }
-    
-    @Override
-    public Map<String, String> getPropertySynonyms() {
-        return Collections.emptyMap();
-    }
-    
-    @Override
-    public String getJdbcUrl(final DataSource targetDataSource) {
-        return null;
-    }
-    
-    @Override
-    public String getJdbcUrlPropertiesFieldName() {
-        return null;
-    }
-    
-    @Override
-    public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
-        return null;
-    }
-    
-    @Override
-    public void appendJdbcUrlProperties(final String key, final String value, final DataSource targetDataSource) {
-    }
-    
-    @Override
-    public Collection<String> getTransientFieldNames() {
-        return Collections.emptyList();
-    }
-    
-    @Override
-    public String getType() {
-        return "Default";
-    }
-    
-    @Override
-    public boolean isDefault() {
-        return true;
-    }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourceProperties.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourceProperties.java
index be7ccb4..0bb3a71 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourceProperties.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourceProperties.java
@@ -26,10 +26,12 @@ import org.apache.shardingsphere.infra.datasource.props.synonym.ConnectionProper
 import org.apache.shardingsphere.infra.datasource.props.synonym.PoolPropertySynonyms;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 /**
  * Data source properties.
@@ -45,13 +47,15 @@ public final class DataSourceProperties {
     
     private final CustomDataSourceProperties customDataSourceProperties;
     
+    @SuppressWarnings({"rawtypes", "unchecked"})
     public DataSourceProperties(final String dataSourceClassName, final Map<String, Object> props) {
         this.dataSourceClassName = dataSourceClassName;
-        DataSourcePoolMetaData<?> poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSourceClassName);
-        Map<String, String> propertySynonyms = poolMetaData.getPropertySynonyms();
+        Optional<DataSourcePoolMetaData> poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSourceClassName);
+        Map<String, String> propertySynonyms = poolMetaData.isPresent() ? poolMetaData.get().getPropertySynonyms() : Collections.emptyMap();
         connectionPropertySynonyms = new ConnectionPropertySynonyms(props, propertySynonyms);
         poolPropertySynonyms = new PoolPropertySynonyms(props, propertySynonyms);
-        customDataSourceProperties = new CustomDataSourceProperties(props, getStandardPropertyKeys(), poolMetaData.getTransientFieldNames(), propertySynonyms);
+        customDataSourceProperties = new CustomDataSourceProperties(
+                props, getStandardPropertyKeys(), poolMetaData.isPresent() ? poolMetaData.get().getTransientFieldNames() : Collections.emptyList(), propertySynonyms);
     }
     
     private Collection<String> getStandardPropertyKeys() {
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
index 887a48a..78af622 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesCreator.java
@@ -28,6 +28,7 @@ import javax.sql.DataSource;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
 /**
  * Data source properties creator.
@@ -74,13 +75,14 @@ public final class DataSourcePropertiesCreator {
         return result;
     }
     
+    @SuppressWarnings("rawtypes")
     private static Map<String, Object> createProperties(final DataSource dataSource) {
         Map<String, Object> result = new LinkedHashMap<>();
-        DataSourcePoolMetaData<?> poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSource.getClass().getName());
+        Optional<DataSourcePoolMetaData> poolMetaData = DataSourcePoolMetaDataFactory.newInstance(dataSource.getClass().getName());
         for (Entry<String, Object> entry : new DataSourceReflection(dataSource).convertToProperties().entrySet()) {
             String propertyName = entry.getKey();
             Object propertyValue = entry.getValue();
-            if (isValidProperty(propertyName, propertyValue, poolMetaData)) {
+            if (!poolMetaData.isPresent() || isValidProperty(propertyName, propertyValue, poolMetaData.get())) {
                 result.put(propertyName, propertyValue);
             }
         }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData b/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
index 728a397..42cef5e 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData
@@ -15,7 +15,6 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.infra.datasource.pool.metadata.type.DefaultDataSourcePoolMetaData
 org.apache.shardingsphere.infra.datasource.pool.metadata.type.HikariDataSourcePoolMetaData
 org.apache.shardingsphere.infra.datasource.pool.metadata.type.DBCPDataSourcePoolMetaData
 org.apache.shardingsphere.infra.datasource.pool.metadata.type.TomcatDBCPDataSourcePoolMetaData
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactoryTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactoryTest.java
index b3b7db1..afca645 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactoryTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataFactoryTest.java
@@ -17,23 +17,19 @@
 
 package org.apache.shardingsphere.infra.datasource.pool.metadata;
 
-import com.zaxxer.hikari.HikariDataSource;
-import org.apache.shardingsphere.infra.datasource.pool.metadata.type.DefaultDataSourcePoolMetaData;
-import org.apache.shardingsphere.infra.datasource.pool.metadata.type.HikariDataSourcePoolMetaData;
+import org.apache.shardingsphere.infra.datasource.pool.metadata.fixture.MockedDataSourcePoolMetaData;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 public final class DataSourcePoolMetaDataFactoryTest {
     
     @Test
-    public void assertNewInstanceForDefault() {
-        assertThat(DataSourcePoolMetaDataFactory.newInstance(""), instanceOf(DefaultDataSourcePoolMetaData.class));
-    }
-    
-    @Test
-    public void assertNewInstanceForHikari() {
-        assertThat(DataSourcePoolMetaDataFactory.newInstance(HikariDataSource.class.getName()), instanceOf(HikariDataSourcePoolMetaData.class));
+    public void assertNewInstance() {
+        assertTrue(DataSourcePoolMetaDataFactory.newInstance(MockedDataSource.class.getName()).isPresent());
+        assertThat(DataSourcePoolMetaDataFactory.newInstance(MockedDataSource.class.getName()).get(), instanceOf(MockedDataSourcePoolMetaData.class));
     }
 }