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 2e731a0 Merge DataSourceReflection and DataSourcePropertiesHandler
(#14470)
2e731a0 is described below
commit 2e731a0180cc6f599b84150d902cf9cc5d40176c
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Dec 31 19:44:04 2021 +0800
Merge DataSourceReflection and DataSourcePropertiesHandler (#14470)
---
.../creator/impl => }/ConnectionURLParser.java | 2 +-
.../config/datasource/DataSourceReflection.java | 41 ++++++++++++
.../impl/AbstractDataSourcePoolCreator.java | 8 +--
.../creator/impl/DataSourcePropertiesHandler.java | 78 ----------------------
.../creator/impl => }/ConnectionURLParserTest.java | 2 +-
5 files changed, 47 insertions(+), 84 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParser.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParser.java
similarity index 96%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParser.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParser.java
index c16f3e6..3c0cff7 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParser.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParser.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.config.datasource.pool.creator.impl;
+package org.apache.shardingsphere.infra.config.datasource;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceReflection.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceReflection.java
index b12b483..6ae442c 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceReflection.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceReflection.java
@@ -29,6 +29,7 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
import java.util.Properties;
@@ -135,4 +136,44 @@ public final class DataSourceReflection {
String setterMethodName = SETTER_PREFIX +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
return Arrays.stream(dataSourceMethods).filter(each ->
each.getName().equals(setterMethodName) && 1 ==
each.getParameterTypes().length).findFirst();
}
+
+ /**
+ * Add default data source properties.
+ *
+ * @param dataSourcePropsFieldName data source properties field name
+ * @param jdbcUrlFieldName JDBC URL field name
+ * @param defaultDataSourceProps default data source properties
+ */
+ public void addDefaultDataSourceProperties(final String
dataSourcePropsFieldName, final String jdbcUrlFieldName, final Properties
defaultDataSourceProps) {
+ if (null == dataSourcePropsFieldName || null == jdbcUrlFieldName) {
+ return;
+ }
+ Properties targetDataSourceProps =
getDataSourcePropertiesFieldName(dataSourcePropsFieldName);
+ Map<String, String> jdbcUrlProps = new
ConnectionURLParser(getJdbcUrl(jdbcUrlFieldName)).getProperties();
+ for (Entry<Object, Object> entry : defaultDataSourceProps.entrySet()) {
+ String defaultPropertyKey = entry.getKey().toString();
+ String defaultPropertyValue = entry.getValue().toString();
+ if (!containsDefaultProperty(defaultPropertyKey,
targetDataSourceProps, jdbcUrlProps)) {
+ targetDataSourceProps.setProperty(defaultPropertyKey,
defaultPropertyValue);
+ }
+ }
+ }
+
+ private boolean containsDefaultProperty(final String defaultPropertyKey,
final Properties targetDataSourceProps, final Map<String, String> jdbcUrlProps)
{
+ return targetDataSourceProps.containsKey(defaultPropertyKey) ||
jdbcUrlProps.containsKey(defaultPropertyKey);
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private Properties getDataSourcePropertiesFieldName(final String
dataSourcePropsFieldName) {
+ return (Properties)
dataSource.getClass().getMethod(getGetterMethodName(dataSourcePropsFieldName)).invoke(dataSource);
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private String getJdbcUrl(final String jdbcUrlFieldName) {
+ return (String)
dataSource.getClass().getMethod(getGetterMethodName(jdbcUrlFieldName)).invoke(dataSource);
+ }
+
+ private String getGetterMethodName(final String fieldName) {
+ return GETTER_PREFIX +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
+ }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/AbstractDataSourcePoolCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/AbstractDataSourcePoolCreator.java
index a6950af..050abde 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/AbstractDataSourcePoolCreator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/AbstractDataSourcePoolCreator.java
@@ -53,8 +53,9 @@ public abstract class AbstractDataSourcePoolCreator
implements DataSourcePoolCre
public final DataSource createDataSource(final DataSourceConfiguration
dataSourceConfig) {
DataSource result =
buildDataSource(dataSourceConfig.getDataSourceClassName());
addPropertySynonym(dataSourceConfig);
- setConfiguredFields(dataSourceConfig, result);
- new
DataSourcePropertiesHandler(result).addDefaultProperties(getDataSourcePropertiesFieldName(),
getJdbcUrlFieldName(), getDefaultDataSourceProperties());
+ DataSourceReflection dataSourceReflection = new
DataSourceReflection(result);
+ setConfiguredFields(dataSourceConfig, dataSourceReflection);
+
dataSourceReflection.addDefaultDataSourceProperties(getDataSourcePropertiesFieldName(),
getJdbcUrlFieldName(), getDefaultDataSourceProperties());
return result;
}
@@ -69,8 +70,7 @@ public abstract class AbstractDataSourcePoolCreator
implements DataSourcePoolCre
}
}
- private void setConfiguredFields(final DataSourceConfiguration
dataSourceConfig, final DataSource dataSource) {
- DataSourceReflection dataSourceReflection = new
DataSourceReflection(dataSource);
+ private void setConfiguredFields(final DataSourceConfiguration
dataSourceConfig, final DataSourceReflection dataSourceReflection) {
for (Entry<String, Object> entry :
dataSourceConfig.getAllProperties().entrySet()) {
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/DataSourcePropertiesHandler.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/DataSourcePropertiesHandler.java
deleted file mode 100644
index 7545574..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/DataSourcePropertiesHandler.java
+++ /dev/null
@@ -1,78 +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.config.datasource.pool.creator.impl;
-
-import com.google.common.base.CaseFormat;
-import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
-
-import javax.sql.DataSource;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Properties;
-
-/**
- * Data source properties handler.
- */
-@RequiredArgsConstructor
-public final class DataSourcePropertiesHandler {
-
- private static final String GETTER_PREFIX = "get";
-
- private final DataSource dataSource;
-
- /**
- * Add default data source properties to target data source.
- *
- * @param dataSourcePropertiesFieldName data source properties field name
- * @param jdbcUrlFieldName JDBC URL field name
- * @param defaultDataSourceProps default data source properties
- */
- public void addDefaultProperties(final String
dataSourcePropertiesFieldName, final String jdbcUrlFieldName, final Properties
defaultDataSourceProps) {
- if (null == dataSourcePropertiesFieldName || null == jdbcUrlFieldName)
{
- return;
- }
- Properties targetDataSourceProps =
getDataSourceProperties(dataSourcePropertiesFieldName);
- Map<String, String> jdbcUrlProps = new
ConnectionURLParser(getJdbcUrl(jdbcUrlFieldName)).getProperties();
- for (Entry<Object, Object> entry : defaultDataSourceProps.entrySet()) {
- String defaultPropertyKey = entry.getKey().toString();
- String defaultPropertyValue = entry.getValue().toString();
- if (!containsDefaultProperty(defaultPropertyKey,
targetDataSourceProps, jdbcUrlProps)) {
- targetDataSourceProps.setProperty(defaultPropertyKey,
defaultPropertyValue);
- }
- }
- }
-
- private boolean containsDefaultProperty(final String defaultPropertyKey,
final Properties targetDataSourceProps, final Map<String, String> jdbcUrlProps)
{
- return targetDataSourceProps.containsKey(defaultPropertyKey) ||
jdbcUrlProps.containsKey(defaultPropertyKey);
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private Properties getDataSourceProperties(final String
dataSourcePropertiesFieldName) {
- return (Properties)
dataSource.getClass().getMethod(getGetterMethodName(dataSourcePropertiesFieldName)).invoke(dataSource);
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private String getJdbcUrl(final String jdbcUrlFieldName) {
- return (String)
dataSource.getClass().getMethod(getGetterMethodName(jdbcUrlFieldName)).invoke(dataSource);
- }
-
- private String getGetterMethodName(final String fieldName) {
- return GETTER_PREFIX +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, fieldName);
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParserTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParserTest.java
similarity index 97%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParserTest.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParserTest.java
index b895f06..b08168a 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/pool/creator/impl/ConnectionURLParserTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/datasource/ConnectionURLParserTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.config.datasource.pool.creator.impl;
+package org.apache.shardingsphere.infra.config.datasource;
import org.junit.Test;