This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 b43fd9a Add DataSourcePoolMetaDataReflection (#15814)
b43fd9a is described below
commit b43fd9af42ecbd0a3eb743ca8c7917d52fa9cd05
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Mar 6 00:18:42 2022 +0800
Add DataSourcePoolMetaDataReflection (#15814)
* Add DataSourcePoolMetaDataReflection
* Add DataSourcePoolMetaDataReflection
---
.../pool/creator/DataSourceReflection.java | 6 +-
.../pool/metadata/DataSourceJdbcUrlMetaData.java | 24 ------
.../metadata/DataSourcePoolMetaDataReflection.java | 94 ++++++++++++++++++++++
.../type/dbcp/DBCPDataSourceJdbcUrlMetaData.java | 15 ----
.../dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java | 15 ----
.../hikari/HikariDataSourceJdbcUrlMetaData.java | 15 ----
.../fixture/MockedDataSourceJdbcUrlMetaData.java | 24 ------
.../datasource/props/DataSourcePropertiesTest.java | 6 +-
.../config/NarayanaConfigurationFileGenerator.java | 11 +--
9 files changed, 106 insertions(+), 104 deletions(-)
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 b548eb1..9c472b3 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
@@ -23,6 +23,7 @@ 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.DataSourcePoolMetaDataReflection;
import javax.sql.DataSource;
import java.lang.reflect.Method;
@@ -156,11 +157,10 @@ public final class DataSourceReflection {
*
* @param dataSourcePoolMetaData data source pool meta data
*/
- @SuppressWarnings({"rawtypes", "unchecked"})
public void addDefaultDataSourceProperties(final DataSourcePoolMetaData
dataSourcePoolMetaData) {
- String jdbcUrl =
dataSourcePoolMetaData.getJdbcUrlMetaData().getJdbcUrl(dataSource);
+ String jdbcUrl = new
DataSourcePoolMetaDataReflection(dataSource).getJdbcUrl();
Properties jdbcUrlProps =
dataSourcePoolMetaData.getJdbcUrlMetaData().getJdbcUrlProperties(dataSource);
- if (null == jdbcUrl || null == jdbcUrlProps) {
+ if (null == jdbcUrlProps) {
return;
}
DataSourceMetaData dataSourceMetaData =
DatabaseTypeRegistry.getDatabaseTypeByURL(jdbcUrl).getDataSourceMetaData(jdbcUrl,
null);
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/DataSourceJdbcUrlMetaData.java
index b0b6330..1ee36a3 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/DataSourceJdbcUrlMetaData.java
@@ -26,30 +26,6 @@ import java.util.Properties;
public interface DataSourceJdbcUrlMetaData {
/**
- * Get JDBC URL.
- *
- * @param targetDataSource target data source
- * @return JDBC URL
- */
- String getJdbcUrl(DataSource targetDataSource);
-
- /**
- * Get username.
- *
- * @param targetDataSource target data source
- * @return username
- */
- String getUsername(DataSource targetDataSource);
-
- /**
- * Get password.
- *
- * @param targetDataSource target data source
- * @return password
- */
- String getPassword(DataSource targetDataSource);
-
- /**
* Get JDBC URL properties field name.
*
* @return JDBC URL properties field name
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
new file mode 100644
index 0000000..6661504
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/DataSourcePoolMetaDataReflection.java
@@ -0,0 +1,94 @@
+/*
+ * 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;
+
+import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Optional;
+
+/**
+ * Data source pool meta data reflection.
+ */
+@RequiredArgsConstructor
+public final class DataSourcePoolMetaDataReflection {
+
+ private final DataSource targetDataSource;
+
+ /**
+ * Get JDBC URL.
+ *
+ * @return got JDBC URL
+ */
+ public String getJdbcUrl() {
+ return getFieldValue("jdbcUrl", "url");
+ }
+
+ /**
+ * Get username.
+ *
+ * @return got username
+ */
+ public String getUsername() {
+ return getFieldValue("username", "user");
+ }
+
+ /**
+ * Get password.
+ *
+ * @return got password
+ */
+ public String getPassword() {
+ return getFieldValue("password");
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private <T> T getFieldValue(final String... fieldNames) {
+ for (String each : fieldNames) {
+ Optional<T> result = findFieldValue(each);
+ if (result.isPresent()) {
+ return result.get();
+ }
+ }
+ throw new ReflectiveOperationException(String.format("Can not find
field names `%s`", Arrays.asList(fieldNames)));
+ }
+
+ @SuppressWarnings("unchecked")
+ @SneakyThrows(ReflectiveOperationException.class)
+ private <T> Optional<T> findFieldValue(final String fieldName) {
+ Class<?> dataSourceClass = targetDataSource.getClass();
+ Field field = null;
+ boolean isFound = false;
+ while (!isFound) {
+ try {
+ field = dataSourceClass.getDeclaredField(fieldName);
+ isFound = true;
+ } catch (final ReflectiveOperationException ignored) {
+ dataSourceClass = dataSourceClass.getSuperclass();
+ if (Object.class == dataSourceClass) {
+ return Optional.empty();
+ }
+ }
+ }
+ field.setAccessible(true);
+ return Optional.of((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/DBCPDataSourceJdbcUrlMetaData.java
index 7433cf1..1887ef9 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/DBCPDataSourceJdbcUrlMetaData.java
@@ -31,21 +31,6 @@ import java.util.Properties;
public final class DBCPDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
@Override
- public String getJdbcUrl(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "url");
- }
-
- @Override
- public String getUsername(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "username");
- }
-
- @Override
- public String getPassword(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "password");
- }
-
- @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/TomcatDBCPDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/dbcp/TomcatDBCPDataSourceJdbcUrlMetaData.java
index b5f607b..6ac4741 100644
---
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
@@ -31,21 +31,6 @@ import java.util.Properties;
public final class TomcatDBCPDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
@Override
- public String getJdbcUrl(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "url");
- }
-
- @Override
- public String getUsername(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "username");
- }
-
- @Override
- public String getPassword(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "password");
- }
-
- @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/hikari/HikariDataSourceJdbcUrlMetaData.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/datasource/pool/metadata/type/hikari/HikariDataSourceJdbcUrlMetaData.java
index deb4d49..7c38abf 100644
---
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
@@ -30,21 +30,6 @@ import java.util.Properties;
public final class HikariDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
@Override
- public String getJdbcUrl(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "jdbcUrl");
- }
-
- @Override
- public String getUsername(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "username");
- }
-
- @Override
- public String getPassword(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "password");
- }
-
- @Override
public String getJdbcUrlPropertiesFieldName() {
return "dataSourceProperties";
}
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/MockedDataSourceJdbcUrlMetaData.java
index 8adef1e..b4a07ac 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/MockedDataSourceJdbcUrlMetaData.java
@@ -17,31 +17,14 @@
package org.apache.shardingsphere.infra.datasource.pool.metadata.fixture;
-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;
public final class MockedDataSourceJdbcUrlMetaData implements
DataSourceJdbcUrlMetaData {
@Override
- public String getJdbcUrl(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "url");
- }
-
- @Override
- public String getUsername(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "username");
- }
-
- @Override
- public String getPassword(final DataSource targetDataSource) {
- return (String) getFieldValue(targetDataSource, "password");
- }
-
- @Override
public String getJdbcUrlPropertiesFieldName() {
return null;
}
@@ -54,11 +37,4 @@ public final class MockedDataSourceJdbcUrlMetaData
implements DataSourceJdbcUrlM
@Override
public void appendJdbcUrlProperties(final String key, final String value,
final DataSource targetDataSource) {
}
-
- @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/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesTest.java
index 0162e65..5ea248c 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/props/DataSourcePropertiesTest.java
@@ -43,7 +43,7 @@ public final class DataSourcePropertiesTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
-
+
@SuppressWarnings("unchecked")
@Test
public void assertGetDataSourceConfigurationWithConnectionInitSqls() {
@@ -64,7 +64,7 @@ public final class DataSourcePropertiesTest {
assertThat(actualConnectionInitSql, hasItem("set names utf8mb4;"));
assertThat(actualConnectionInitSql, hasItem("set names utf8;"));
}
-
+
@Test
public void assertGetAllLocalProperties() {
DataSourceProperties originalDataSourceProps = new
DataSourceProperties(MockedDataSource.class.getName(), getProperties());
@@ -86,7 +86,7 @@ public final class DataSourcePropertiesTest {
assertTrue(actualAllProperties.containsKey("idleTimeout"));
assertTrue(actualAllProperties.containsValue("30000"));
}
-
+
private Map<String, Object> getProperties() {
Map<String, Object> result = new HashMap<>(7, 1);
result.put("driverClassName", MockedDataSource.class.getName());
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 e93802c..6521b56 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
@@ -33,6 +33,7 @@ import
org.apache.shardingsphere.infra.database.type.dialect.OpenGaussDatabaseTy
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;
import
org.apache.shardingsphere.transaction.spi.TransactionConfigurationFileGenerator;
@@ -155,20 +156,20 @@ public final class NarayanaConfigurationFileGenerator
implements TransactionConf
generateTransactionProps(url, user, password, dataSourceClass, props);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
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()) {
- String jdbcUrl =
poolMetaData.get().getJdbcUrlMetaData().getJdbcUrl(dataSource.get());
+ DataSourcePoolMetaDataReflection
dataSourcePoolMetaDataReflection = new
DataSourcePoolMetaDataReflection(dataSource.get());
+ String jdbcUrl = dataSourcePoolMetaDataReflection.getJdbcUrl();
int endIndex = jdbcUrl.indexOf("?");
jdbcUrl = jdbcUrl.substring(0, endIndex);
- String user =
poolMetaData.get().getJdbcUrlMetaData().getUsername(dataSource.get());
- String password =
poolMetaData.get().getJdbcUrlMetaData().getPassword(dataSource.get());
+ String username =
dataSourcePoolMetaDataReflection.getUsername();
+ String password =
dataSourcePoolMetaDataReflection.getPassword();
String dataSourceClassName =
getDataSourceClassNameByJdbcUrl(jdbcUrl);
- generateTransactionProps(jdbcUrl, user, password,
dataSourceClassName, props);
+ generateTransactionProps(jdbcUrl, username, password,
dataSourceClassName, props);
}
}
}