This is an automated email from the ASF dual-hosted git repository.
menghaoran 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 b3e90fc Refactor DataSourcePoolFieldMetaData (#15834)
b3e90fc is described below
commit b3e90fc9f8df3057b75ae3c7630a6c02b930d0d7
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Mar 7 07:03:01 2022 +0800
Refactor DataSourcePoolFieldMetaData (#15834)
* Refactor DataSourceJdbcUrlMetaData
* Refactor DataSourcePoolFieldMetaData
* Refactor DataSourcePoolMetaDataReflection
* Refactor DataSourcePoolFieldMetaData
* Refactor DataSourcePoolFieldMetaData
* Refactor DataSourcePoolFieldMetaData
* Refactor DataSourcePoolFieldMetaData
* Fix javadoc
---
.../pool/creator/DataSourcePoolCreator.java | 10 ++--
.../pool/creator/DataSourceReflection.java | 18 +++---
...aData.java => DataSourcePoolFieldMetaData.java} | 39 ++++++-------
.../pool/metadata/DataSourcePoolMetaData.java | 6 +-
.../metadata/DataSourcePoolMetaDataFactory.java | 1 -
.../metadata/DataSourcePoolMetaDataReflection.java | 34 +++++++----
...a.java => DBCPDataSourcePoolFieldMetaData.java} | 34 ++++-------
.../type/dbcp/DBCPDataSourcePoolMetaData.java | 4 +-
.../dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java | 56 ------------------
.../dbcp/TomcatDBCPDataSourcePoolMetaData.java | 4 +-
.../hikari/HikariDataSourceJdbcUrlMetaData.java | 66 ----------------------
.../hikari/HikariDataSourcePoolFieldMetaData.java} | 28 +++++----
.../type/hikari/HikariDataSourcePoolMetaData.java | 4 +-
.../datasource/props/DataSourceProperties.java | 1 -
...java => MockedDataSourcePoolFieldMetaData.java} | 23 ++++----
.../fixture/MockedDataSourcePoolMetaData.java | 4 +-
.../config/NarayanaConfigurationFileGenerator.java | 28 +++++----
17 files changed, 125 insertions(+), 235 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 bfcbb26..712529a 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
@@ -22,6 +22,7 @@ import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataFactory;
+import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataReflection;
import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
import
org.apache.shardingsphere.infra.datasource.props.custom.CustomDataSourceProperties;
@@ -62,7 +63,7 @@ public final class DataSourcePoolCreator {
setDefaultFields(dataSourceReflection, poolMetaData.get());
setConfiguredFields(dataSourceProps, dataSourceReflection,
poolMetaData.get());
appendJdbcUrlProperties(dataSourceProps.getCustomDataSourceProperties(),
result, poolMetaData.get());
-
dataSourceReflection.addDefaultDataSourceProperties(poolMetaData.get());
+ dataSourceReflection.addDefaultDataSourceProperties();
} else {
setConfiguredFields(dataSourceProps, dataSourceReflection);
}
@@ -90,7 +91,7 @@ public final class DataSourcePoolCreator {
for (Entry<String, Object> entry :
dataSourceProps.getAllLocalProperties().entrySet()) {
String fieldName = entry.getKey();
Object fieldValue = entry.getValue();
- if (isValidProperty(fieldName, fieldValue, poolMetaData) &&
!fieldName.equals(poolMetaData.getJdbcUrlMetaData().getJdbcUrlPropertiesFieldName()))
{
+ if (isValidProperty(fieldName, fieldValue, poolMetaData) &&
!fieldName.equals(poolMetaData.getFieldMetaData().getJdbcUrlPropertiesFieldName()))
{
dataSourceReflection.setField(fieldName, fieldValue);
}
}
@@ -102,11 +103,12 @@ public final class DataSourcePoolCreator {
@SuppressWarnings("unchecked")
private static void appendJdbcUrlProperties(final
CustomDataSourceProperties customDataSourceProps, final DataSource
targetDataSource, final DataSourcePoolMetaData poolMetaData) {
- String jdbcUrlPropertiesFieldName =
poolMetaData.getJdbcUrlMetaData().getJdbcUrlPropertiesFieldName();
+ String jdbcUrlPropertiesFieldName =
poolMetaData.getFieldMetaData().getJdbcUrlPropertiesFieldName();
if (null != jdbcUrlPropertiesFieldName &&
customDataSourceProps.getProperties().containsKey(jdbcUrlPropertiesFieldName)) {
Map<String, Object> jdbcUrlProps = (Map<String, Object>)
customDataSourceProps.getProperties().get(jdbcUrlPropertiesFieldName);
+ DataSourcePoolMetaDataReflection dataSourcePoolMetaDataReflection
= new DataSourcePoolMetaDataReflection(targetDataSource,
poolMetaData.getFieldMetaData());
for (Entry<String, Object> entry : jdbcUrlProps.entrySet()) {
-
poolMetaData.getJdbcUrlMetaData().appendJdbcUrlProperties(entry.getKey(),
entry.getValue().toString(), targetDataSource);
+
dataSourcePoolMetaDataReflection.getJdbcConnectionProperties().setProperty(entry.getKey(),
entry.getValue().toString());
}
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
index 9c472b3..56df31c 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/creator/DataSourceReflection.java
@@ -22,7 +22,7 @@ import com.google.common.collect.Sets;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.database.metadata.DataSourceMetaData;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
+import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataFactory;
import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataReflection;
import javax.sql.DataSource;
@@ -154,13 +154,13 @@ public final class DataSourceReflection {
/**
* Add default data source properties.
- *
- * @param dataSourcePoolMetaData data source pool meta data
*/
- public void addDefaultDataSourceProperties(final DataSourcePoolMetaData
dataSourcePoolMetaData) {
- String jdbcUrl = new
DataSourcePoolMetaDataReflection(dataSource).getJdbcUrl();
- Properties jdbcUrlProps =
dataSourcePoolMetaData.getJdbcUrlMetaData().getJdbcUrlProperties(dataSource);
- if (null == jdbcUrlProps) {
+ public void addDefaultDataSourceProperties() {
+ DataSourcePoolMetaDataReflection dataSourcePoolMetaDataReflection =
DataSourcePoolMetaDataFactory.newInstance(dataSource.getClass().getName()).map(
+ optional -> new DataSourcePoolMetaDataReflection(dataSource,
optional.getFieldMetaData())).orElseGet(() -> new
DataSourcePoolMetaDataReflection(dataSource));
+ String jdbcUrl = dataSourcePoolMetaDataReflection.getJdbcUrl();
+ Properties jdbcConnectionProps =
dataSourcePoolMetaDataReflection.getJdbcConnectionProperties();
+ if (null == jdbcUrl || null == jdbcConnectionProps) {
return;
}
DataSourceMetaData dataSourceMetaData =
DatabaseTypeRegistry.getDatabaseTypeByURL(jdbcUrl).getDataSourceMetaData(jdbcUrl,
null);
@@ -168,8 +168,8 @@ public final class DataSourceReflection {
for (Entry<Object, Object> entry :
dataSourceMetaData.getDefaultQueryProperties().entrySet()) {
String defaultPropertyKey = entry.getKey().toString();
String defaultPropertyValue = entry.getValue().toString();
- if (!containsDefaultProperty(defaultPropertyKey, jdbcUrlProps,
queryProps)) {
-
dataSourcePoolMetaData.getJdbcUrlMetaData().appendJdbcUrlProperties(defaultPropertyKey,
defaultPropertyValue, dataSource);
+ if (!containsDefaultProperty(defaultPropertyKey,
jdbcConnectionProps, queryProps)) {
+ jdbcConnectionProps.setProperty(defaultPropertyKey,
defaultPropertyValue);
}
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolFieldMetaData.java
similarity index 65%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourceJdbcUrlMetaData.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolFieldMetaData.java
index 1ee36a3..8877d13 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourceJdbcUrlMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolFieldMetaData.java
@@ -17,35 +17,36 @@
package org.apache.shardingsphere.infra.datasource.pool.metadata;
-import javax.sql.DataSource;
-import java.util.Properties;
-
/**
- * Data source JDBC URL meta data.
+ * Data source pool field meta data.
*/
-public interface DataSourceJdbcUrlMetaData {
+public interface DataSourcePoolFieldMetaData {
/**
- * Get JDBC URL properties field name.
+ * Get username field name.
*
- * @return JDBC URL properties field name
+ * @return username field name
*/
- String getJdbcUrlPropertiesFieldName();
+ String getUsernameFieldName();
/**
- * Get JDBC URL properties.
- *
- * @param targetDataSource target data source
- * @return JDBC URL properties
+ * Get password field name.
+ *
+ * @return password field name
*/
- Properties getJdbcUrlProperties(DataSource targetDataSource);
+ String getPasswordFieldName();
/**
- * Append JDBC URL properties.
- *
- * @param key key
- * @param value value
- * @param targetDataSource target data source
+ * Get JDBC URL field name.
+ *
+ * @return JDBC URL field name
*/
- void appendJdbcUrlProperties(String key, String value, DataSource
targetDataSource);
+ String getJdbcUrlFieldName();
+
+ /**
+ * Get JDBC URL properties field name.
+ *
+ * @return JDBC URL properties field name
+ */
+ String getJdbcUrlPropertiesFieldName();
}
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 390a1d3..5340232 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
@@ -56,9 +56,9 @@ public interface DataSourcePoolMetaData extends TypedSPI {
Collection<String> getTransientFieldNames();
/**
- * Get JDBC URL meta data.
+ * Get data source pool field meta data.
*
- * @return data source JDBC URL meta data
+ * @return data source pool field meta data
*/
- DataSourceJdbcUrlMetaData getJdbcUrlMetaData();
+ DataSourcePoolFieldMetaData getFieldMetaData();
}
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 7896e42..0d0de9a 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
@@ -41,7 +41,6 @@ public final class DataSourcePoolMetaDataFactory {
* @param dataSourceClassName data source class name
* @return new instance of data source pool meta data
*/
- @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/DataSourcePoolMetaDataReflection.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataReflection.java
index 6661504..417ab39 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataReflection.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataReflection.java
@@ -22,8 +22,8 @@ import lombok.SneakyThrows;
import javax.sql.DataSource;
import java.lang.reflect.Field;
-import java.util.Arrays;
import java.util.Optional;
+import java.util.Properties;
/**
* Data source pool meta data reflection.
@@ -33,34 +33,48 @@ public final class DataSourcePoolMetaDataReflection {
private final DataSource targetDataSource;
+ private final DataSourcePoolFieldMetaData dataSourcePoolFieldMetaData;
+
+ public DataSourcePoolMetaDataReflection(final DataSource targetDataSource)
{
+ this(targetDataSource, null);
+ }
+
/**
* Get JDBC URL.
*
- * @return got JDBC URL
+ * @return JDBC URL
*/
public String getJdbcUrl() {
- return getFieldValue("jdbcUrl", "url");
+ return null == dataSourcePoolFieldMetaData ? getFieldValue("jdbcUrl",
"url") : getFieldValue(dataSourcePoolFieldMetaData.getJdbcUrlFieldName());
}
/**
* Get username.
*
- * @return got username
+ * @return username
*/
public String getUsername() {
- return getFieldValue("username", "user");
+ return null == dataSourcePoolFieldMetaData ? getFieldValue("username",
"user") : getFieldValue(dataSourcePoolFieldMetaData.getUsernameFieldName());
}
/**
* Get password.
*
- * @return got password
+ * @return password
*/
public String getPassword() {
- return getFieldValue("password");
+ return null == dataSourcePoolFieldMetaData ? getFieldValue("password")
: getFieldValue(dataSourcePoolFieldMetaData.getPasswordFieldName());
+ }
+
+ /**
+ * Get JDBC connection properties.
+ *
+ * @return JDBC connection properties
+ */
+ public Properties getJdbcConnectionProperties() {
+ return null == dataSourcePoolFieldMetaData ?
getFieldValue("dataSourceProperties", "connectionProperties") :
getFieldValue(dataSourcePoolFieldMetaData.getJdbcUrlPropertiesFieldName());
}
- @SneakyThrows(ReflectiveOperationException.class)
private <T> T getFieldValue(final String... fieldNames) {
for (String each : fieldNames) {
Optional<T> result = findFieldValue(each);
@@ -68,7 +82,7 @@ public final class DataSourcePoolMetaDataReflection {
return result.get();
}
}
- throw new ReflectiveOperationException(String.format("Can not find
field names `%s`", Arrays.asList(fieldNames)));
+ return null;
}
@SuppressWarnings("unchecked")
@@ -89,6 +103,6 @@ public final class DataSourcePoolMetaDataReflection {
}
}
field.setAccessible(true);
- return Optional.of((T) field.get(targetDataSource));
+ return Optional.ofNullable((T) field.get(targetDataSource));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolFieldMetaData.java
similarity index 50%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourceJdbcUrlMetaData.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolFieldMetaData.java
index 1887ef9..544daaf 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourceJdbcUrlMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolFieldMetaData.java
@@ -17,40 +17,30 @@
package org.apache.shardingsphere.infra.datasource.pool.metadata.type.dbcp;
-import lombok.SneakyThrows;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourceJdbcUrlMetaData;
-
-import javax.sql.DataSource;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.Properties;
+import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
/**
- * Hikari data source JDBC URL meta data.
+ * DBCP data source JDBC URL meta data.
*/
-public final class DBCPDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
+public final class DBCPDataSourcePoolFieldMetaData implements
DataSourcePoolFieldMetaData {
@Override
- public String getJdbcUrlPropertiesFieldName() {
- return "connectionProperties";
+ public String getUsernameFieldName() {
+ return "username";
}
@Override
- public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
- return (Properties) getFieldValue(targetDataSource,
"connectionProperties");
+ public String getPasswordFieldName() {
+ return "password";
}
- @SneakyThrows(ReflectiveOperationException.class)
@Override
- public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
- Method method =
targetDataSource.getClass().getMethod("addConnectionProperty", String.class,
String.class);
- method.invoke(targetDataSource, key, value);
+ public String getJdbcUrlFieldName() {
+ return "url";
}
- @SneakyThrows(ReflectiveOperationException.class)
- private Object getFieldValue(final DataSource targetDataSource, final
String fieldName) {
- Field field = targetDataSource.getClass().getDeclaredField(fieldName);
- field.setAccessible(true);
- return field.get(targetDataSource);
+ @Override
+ public String getJdbcUrlPropertiesFieldName() {
+ return "connectionProperties";
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
index 96a32a8..10cfddf 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/DBCPDataSourcePoolMetaData.java
@@ -57,8 +57,8 @@ public final class DBCPDataSourcePoolMetaData implements
DataSourcePoolMetaData
}
@Override
- public DBCPDataSourceJdbcUrlMetaData getJdbcUrlMetaData() {
- return new DBCPDataSourceJdbcUrlMetaData();
+ public DBCPDataSourcePoolFieldMetaData getFieldMetaData() {
+ return new DBCPDataSourcePoolFieldMetaData();
}
@Override
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java
deleted file mode 100644
index 6ac4741..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java
+++ /dev/null
@@ -1,56 +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.dbcp;
-
-import lombok.SneakyThrows;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourceJdbcUrlMetaData;
-
-import javax.sql.DataSource;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.Properties;
-
-/**
- * Tomcat DBCP data source JDBC URL meta data.
- */
-public final class TomcatDBCPDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
-
- @Override
- public String getJdbcUrlPropertiesFieldName() {
- return "connectionProperties";
- }
-
- @Override
- public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
- return (Properties) getFieldValue(targetDataSource,
"connectionProperties");
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- @Override
- public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
- Method method =
targetDataSource.getClass().getMethod("addConnectionProperty", String.class,
String.class);
- method.invoke(targetDataSource, key, value);
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private Object getFieldValue(final DataSource targetDataSource, final
String fieldName) {
- Field field = targetDataSource.getClass().getDeclaredField(fieldName);
- field.setAccessible(true);
- return field.get(targetDataSource);
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourcePoolMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourcePoolMetaData.java
index 4c5d800..f2a2990 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourcePoolMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourcePoolMetaData.java
@@ -57,8 +57,8 @@ public final class TomcatDBCPDataSourcePoolMetaData
implements DataSourcePoolMet
}
@Override
- public TomcatDBCPDataSourceJdbcUrlMetaData getJdbcUrlMetaData() {
- return new TomcatDBCPDataSourceJdbcUrlMetaData();
+ public DBCPDataSourcePoolFieldMetaData getFieldMetaData() {
+ return new DBCPDataSourcePoolFieldMetaData();
}
@Override
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourceJdbcUrlMetaData.java
deleted file mode 100644
index 7c38abf..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourceJdbcUrlMetaData.java
+++ /dev/null
@@ -1,66 +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.hikari;
-
-import lombok.SneakyThrows;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourceJdbcUrlMetaData;
-
-import javax.sql.DataSource;
-import java.lang.reflect.Field;
-import java.util.Properties;
-
-/**
- * Hikari data source JDBC URL meta data.
- */
-public final class HikariDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
-
- @Override
- public String getJdbcUrlPropertiesFieldName() {
- return "dataSourceProperties";
- }
-
- @Override
- public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
- return (Properties) getFieldValue(targetDataSource,
"dataSourceProperties");
- }
-
- @Override
- public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
- ((Properties) getFieldValue(targetDataSource,
"dataSourceProperties")).put(key, value);
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private Object getFieldValue(final DataSource targetDataSource, final
String fieldName) {
- Class<?> dataSourceClass = targetDataSource.getClass();
- Field field = null;
- boolean found = false;
- while (!found) {
- try {
- field = dataSourceClass.getDeclaredField(fieldName);
- found = true;
- } catch (final ReflectiveOperationException ex) {
- dataSourceClass = dataSourceClass.getSuperclass();
- if (Object.class == dataSourceClass) {
- throw ex;
- }
- }
- }
- field.setAccessible(true);
- return field.get(targetDataSource);
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolFieldMetaData.java
similarity index 69%
copy from
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
copy to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolFieldMetaData.java
index b4a07ac..c4e3f5f 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolFieldMetaData.java
@@ -15,26 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.datasource.pool.metadata.fixture;
+package org.apache.shardingsphere.infra.datasource.pool.metadata.type.hikari;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourceJdbcUrlMetaData;
+import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
-import javax.sql.DataSource;
-import java.util.Properties;
-
-public final class MockedDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
+/**
+ * Hikari data source JDBC URL meta data.
+ */
+public final class HikariDataSourcePoolFieldMetaData implements
DataSourcePoolFieldMetaData {
@Override
- public String getJdbcUrlPropertiesFieldName() {
- return null;
+ public String getUsernameFieldName() {
+ return "username";
}
@Override
- public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
- return new Properties();
+ public String getPasswordFieldName() {
+ return "password";
}
@Override
- public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
+ public String getJdbcUrlFieldName() {
+ return "jdbcUrl";
+ }
+
+ @Override
+ public String getJdbcUrlPropertiesFieldName() {
+ return "dataSourceProperties";
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
index 3be4872..c361d75 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourcePoolMetaData.java
@@ -76,8 +76,8 @@ public final class HikariDataSourcePoolMetaData implements
DataSourcePoolMetaDat
}
@Override
- public HikariDataSourceJdbcUrlMetaData getJdbcUrlMetaData() {
- return new HikariDataSourceJdbcUrlMetaData();
+ public HikariDataSourcePoolFieldMetaData getFieldMetaData() {
+ return new HikariDataSourcePoolFieldMetaData();
}
@Override
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 0bb3a71..6615400 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
@@ -47,7 +47,6 @@ public final class DataSourceProperties {
private final CustomDataSourceProperties customDataSourceProperties;
- @SuppressWarnings({"rawtypes", "unchecked"})
public DataSourceProperties(final String dataSourceClassName, final
Map<String, Object> props) {
this.dataSourceClassName = dataSourceClassName;
Optional<DataSourcePoolMetaData> poolMetaData =
DataSourcePoolMetaDataFactory.newInstance(dataSourceClassName);
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolFieldMetaData.java
similarity index 71%
rename from
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
rename to
shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolFieldMetaData.java
index b4a07ac..a88e566 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourceJdbcUrlMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolFieldMetaData.java
@@ -17,24 +17,27 @@
package org.apache.shardingsphere.infra.datasource.pool.metadata.fixture;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourceJdbcUrlMetaData;
+import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolFieldMetaData;
-import javax.sql.DataSource;
-import java.util.Properties;
-
-public final class MockedDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
+public final class MockedDataSourcePoolFieldMetaData implements
DataSourcePoolFieldMetaData {
@Override
- public String getJdbcUrlPropertiesFieldName() {
- return null;
+ public String getUsernameFieldName() {
+ return "username";
+ }
+
+ @Override
+ public String getPasswordFieldName() {
+ return "password";
}
@Override
- public Properties getJdbcUrlProperties(final DataSource targetDataSource) {
- return new Properties();
+ public String getJdbcUrlFieldName() {
+ return "url";
}
@Override
- public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
+ public String getJdbcUrlPropertiesFieldName() {
+ return "jdbcUrlProperties";
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
index e5203cf..c1536ec 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/pool/metadata/fixture/MockedDataSourcePoolMetaData.java
@@ -55,8 +55,8 @@ public final class MockedDataSourcePoolMetaData implements
DataSourcePoolMetaDat
}
@Override
- public MockedDataSourceJdbcUrlMetaData getJdbcUrlMetaData() {
- return new MockedDataSourceJdbcUrlMetaData();
+ public MockedDataSourcePoolFieldMetaData getFieldMetaData() {
+ return new MockedDataSourcePoolFieldMetaData();
}
@Override
diff --git
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-x
[...]
index 6521b56..8750913 100644
---
a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
+++
b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-type/shardingsphere-transaction-xa/shardingsphere-transaction-xa-provider/shardingsphere-transaction-xa-narayana/src/main/java/org/apache/shardingsphere/transaction/xa/narayana/config/NarayanaConfigurationFileGenerator.java
@@ -31,7 +31,6 @@ import
org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseType;
import
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaData;
import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataFactory;
import
org.apache.shardingsphere.infra.datasource.pool.metadata.DataSourcePoolMetaDataReflection;
import org.apache.shardingsphere.infra.instance.InstanceContext;
@@ -159,19 +158,18 @@ public final class NarayanaConfigurationFileGenerator
implements TransactionConf
private void generateDefaultJdbcStoreConfiguration(final
SchemaConfiguration schemaConfiguration, final Properties props) {
Map<String, DataSource> datasourceMap =
schemaConfiguration.getDataSources();
Optional<DataSource> dataSource =
datasourceMap.values().stream().findFirst();
- if (dataSource.isPresent()) {
- Optional<DataSourcePoolMetaData> poolMetaData =
DataSourcePoolMetaDataFactory.newInstance(dataSource.get().getClass().getName());
- if (poolMetaData.isPresent()) {
- DataSourcePoolMetaDataReflection
dataSourcePoolMetaDataReflection = new
DataSourcePoolMetaDataReflection(dataSource.get());
- String jdbcUrl = dataSourcePoolMetaDataReflection.getJdbcUrl();
- int endIndex = jdbcUrl.indexOf("?");
- jdbcUrl = jdbcUrl.substring(0, endIndex);
- String username =
dataSourcePoolMetaDataReflection.getUsername();
- String password =
dataSourcePoolMetaDataReflection.getPassword();
- String dataSourceClassName =
getDataSourceClassNameByJdbcUrl(jdbcUrl);
- generateTransactionProps(jdbcUrl, username, password,
dataSourceClassName, props);
- }
+ if (!dataSource.isPresent()) {
+ return;
}
+ DataSourcePoolMetaDataReflection dataSourcePoolMetaDataReflection =
DataSourcePoolMetaDataFactory.newInstance(dataSource.get().getClass().getName()).map(
+ optional -> new DataSourcePoolMetaDataReflection(dataSource.get(),
optional.getFieldMetaData())).orElseGet(() -> new
DataSourcePoolMetaDataReflection(dataSource.get()));
+ String jdbcUrl = dataSourcePoolMetaDataReflection.getJdbcUrl();
+ int endIndex = jdbcUrl.indexOf("?");
+ jdbcUrl = jdbcUrl.substring(0, endIndex);
+ String username = dataSourcePoolMetaDataReflection.getUsername();
+ String password = dataSourcePoolMetaDataReflection.getPassword();
+ String dataSourceClassName = getDataSourceClassNameByJdbcUrl(jdbcUrl);
+ generateTransactionProps(jdbcUrl, username, password,
dataSourceClassName, props);
}
private String getDataSourceClassNameByJdbcUrl(final String jdbcUrl) {
@@ -184,8 +182,8 @@ public final class NarayanaConfigurationFileGenerator
implements TransactionConf
throw new UnsupportedOperationException(String.format("Cannot support
database type: `%s` as narayana recovery store", type));
}
- private void generateTransactionProps(final String recoveryStoreUrl, final
String recoveryStoreUser, final String recoveryStorePassword, final String
recoveryStoreDataSource,
- final Properties props) {
+ private void generateTransactionProps(final String recoveryStoreUrl, final
String recoveryStoreUser, final String recoveryStorePassword,
+ final String
recoveryStoreDataSource, final Properties props) {
props.setProperty("recoveryStoreUrl", recoveryStoreUrl);
props.setProperty("recoveryStoreUser", recoveryStoreUser);
props.setProperty("recoveryStorePassword", recoveryStorePassword);