未验证 提交 a5b3e9c6 编写于 作者: ShardingSphere's avatar ShardingSphere 提交者: GitHub

Merge pull request #1650 from cherrylzhao/dev

Refactor DatasourceConfiguration & DataSourceParamter
......@@ -21,6 +21,7 @@ import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import io.shardingsphere.core.constant.PoolType;
import io.shardingsphere.core.exception.ShardingConfigurationException;
import io.shardingsphere.core.rule.DataSourceParameter;
import lombok.Getter;
......@@ -69,19 +70,8 @@ public final class DataSourceConfiguration {
* @return data source configuration
*/
public static DataSourceConfiguration getDataSourceConfiguration(final DataSource dataSource) {
Map<String, Object> properties = new LinkedHashMap<>();
try {
for (Method each : findAllGetterMethods(dataSource)) {
String propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, each.getName().substring(GETTER_PREFIX.length()));
if (GENERAL_CLASS_TYPE.contains(each.getReturnType()) && !SKIPPED_PROPERTY_NAMES.contains(propertyName)) {
properties.put(propertyName, each.invoke(dataSource));
}
}
} catch (final ReflectiveOperationException ex) {
throw new ShardingConfigurationException(ex);
}
DataSourceConfiguration result = new DataSourceConfiguration(dataSource.getClass().getName());
result.getProperties().putAll(properties);
result.getProperties().putAll(findAllGetterProperties(dataSource));
return result;
}
......@@ -92,20 +82,29 @@ public final class DataSourceConfiguration {
* @return data source configuration
*/
public static DataSourceConfiguration getDataSourceConfiguration(final DataSourceParameter dataSourceParameter) {
DataSourceConfiguration result = new DataSourceConfiguration(DataSourceParameter.DATA_SOURCE_POOL_CLASS_NAME);
for (Field each : dataSourceParameter.getClass().getDeclaredFields()) {
try {
each.setAccessible(true);
result.getProperties().put(each.getName(), each.get(dataSourceParameter));
} catch (final ReflectiveOperationException ignored) {
DataSourceConfiguration result = new DataSourceConfiguration(PoolType.HIKARI.getClassName());
result.getProperties().putAll(findAllGetterProperties(dataSourceParameter));
return result;
}
private static Map<String, Object> findAllGetterProperties(final Object target) {
Map<String, Object> result = new LinkedHashMap<>();
try {
for (Method each : findAllGetterMethods(target.getClass())) {
String propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, each.getName().substring(GETTER_PREFIX.length()));
if (GENERAL_CLASS_TYPE.contains(each.getReturnType()) && !SKIPPED_PROPERTY_NAMES.contains(propertyName)) {
result.put(propertyName, each.invoke(target));
}
}
} catch (final ReflectiveOperationException ex) {
throw new ShardingConfigurationException(ex);
}
return result;
}
private static Collection<Method> findAllGetterMethods(final DataSource dataSource) {
private static Collection<Method> findAllGetterMethods(final Class<?> clz) {
Collection<Method> result = new HashSet<>();
for (Method each : dataSource.getClass().getMethods()) {
for (Method each : clz.getMethods()) {
if (each.getName().startsWith(GETTER_PREFIX) && 0 == each.getParameterTypes().length) {
result.add(each);
}
......
......@@ -17,6 +17,7 @@
package io.shardingsphere.core.constant;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
......@@ -25,6 +26,7 @@ import lombok.RequiredArgsConstructor;
* @author zhaojun
*/
@RequiredArgsConstructor
@Getter
public enum PoolType {
HIKARI("com.zaxxer.hikari.HikariDataSource"),
......
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed 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.
* </p>
*/
package io.shardingsphere.core.constant.transaction;
/**
* Proxy datasource type which support XA transaction manager.
*
* @author zhaojun
*/
public enum ProxyPoolType {
VENDOR, TOMCAT_DBCP2
}
......@@ -17,7 +17,6 @@
package io.shardingsphere.core.rule;
import io.shardingsphere.core.constant.transaction.ProxyPoolType;
import lombok.Getter;
import lombok.Setter;
......@@ -30,8 +29,6 @@ import lombok.Setter;
@Setter
public final class DataSourceParameter {
public static final String DATA_SOURCE_POOL_CLASS_NAME = "com.zaxxer.hikari.HikariDataSource";
private static final long DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = 30 * 1000;
private static final long DEFAULT_IDLE_TIMEOUT_MILLISECONDS = 60 * 1000;
......@@ -44,16 +41,12 @@ public final class DataSourceParameter {
private static final int DEFAULT_MIN_POOL_SIZE = 1;
private ProxyPoolType proxyDatasourceType = ProxyPoolType.VENDOR;
private String url;
private String username;
private String password;
private boolean autoCommit;
private long connectionTimeoutMilliseconds = DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS;
private long idleTimeoutMilliseconds = DEFAULT_IDLE_TIMEOUT_MILLISECONDS;
......
......@@ -18,7 +18,7 @@
package io.shardingsphere.core.config;
import com.zaxxer.hikari.HikariDataSource;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.core.constant.PoolType;
import org.junit.Test;
import java.sql.SQLException;
......@@ -55,7 +55,7 @@ public final class DataSourceConfigurationTest {
properties.put("jdbcUrl", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
properties.put("username", "root");
properties.put("password", "root");
DataSourceConfiguration dataSourceConfig = new DataSourceConfiguration(DataSourceParameter.DATA_SOURCE_POOL_CLASS_NAME);
DataSourceConfiguration dataSourceConfig = new DataSourceConfiguration(PoolType.HIKARI.getClassName());
dataSourceConfig.getProperties().putAll(properties);
HikariDataSource actual = (HikariDataSource) dataSourceConfig.createDataSource();
assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
......
......@@ -34,8 +34,7 @@ import static org.junit.Assert.assertThat;
public final class DataSourceChangedListenerTest {
private static final String DATA_SOURCE_YAML = "master_ds: !!io.shardingsphere.orchestration.yaml.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + " properties:\n" + " DATA_SOURCE_POOL_CLASS_NAME: com.zaxxer.hikari.HikariDataSource\n"
+ " proxyDatasourceType: !!io.shardingsphere.core.constant.transaction.ProxyPoolType 'VENDOR'\n"
+ " dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + " properties:\n"
+ " url: jdbc:mysql://localhost:3306/demo_ds_master\n" + " username: root\n" + " password: null\n";
private DataSourceChangedListener dataSourceChangedListener;
......
......@@ -23,8 +23,8 @@ import io.shardingsphere.api.config.rule.ShardingRuleConfiguration;
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration;
import io.shardingsphere.core.config.DataSourceConfiguration;
import io.shardingsphere.core.constant.PoolType;
import io.shardingsphere.core.rule.Authentication;
import io.shardingsphere.core.rule.DataSourceParameter;
import org.junit.Test;
import java.util.Collections;
......@@ -40,8 +40,7 @@ import static org.junit.Assert.assertTrue;
public class ConfigurationYamlConverterTest {
private static final String DATA_SOURCE_YAML = "master_ds: !!io.shardingsphere.orchestration.yaml.YamlDataSourceConfiguration\n"
+ " dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + " properties:\n" + " DATA_SOURCE_POOL_CLASS_NAME: com.zaxxer.hikari.HikariDataSource\n"
+ " proxyDatasourceType: !!io.shardingsphere.core.constant.transaction.ProxyPoolType 'VENDOR'\n"
+ " dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + " properties:\n"
+ " url: jdbc:mysql://localhost:3306/demo_ds_master\n" + " username: root\n" + " password: null\n";
private static final String SHARDING_RULE_YAML = " tables:\n" + " t_order:\n" + " actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + " tableStrategy:\n"
......@@ -114,7 +113,7 @@ public class ConfigurationYamlConverterTest {
properties.put("jdbcUrl", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
properties.put("username", "root");
properties.put("password", "root");
DataSourceConfiguration result = new DataSourceConfiguration(DataSourceParameter.DATA_SOURCE_POOL_CLASS_NAME);
DataSourceConfiguration result = new DataSourceConfiguration(PoolType.DRUID.getClassName());
result.getProperties().putAll(properties);
return Collections.singletonMap("test", result);
}
......
......@@ -57,7 +57,6 @@ public final class JDBCRawBackendDataSourceFactory implements JDBCBackendDataSou
config.setJdbcUrl(dataSourceParameter.getUrl());
config.setUsername(dataSourceParameter.getUsername());
config.setPassword(dataSourceParameter.getPassword());
config.setAutoCommit(dataSourceParameter.isAutoCommit());
config.setConnectionTimeout(dataSourceParameter.getConnectionTimeoutMilliseconds());
config.setIdleTimeout(dataSourceParameter.getIdleTimeoutMilliseconds());
config.setMaxLifetime(dataSourceParameter.getMaxLifetimeMilliseconds());
......
......@@ -15,7 +15,6 @@
# url: jdbc:mysql://127.0.0.1:3306/demo_ds_master?serverTimezone=UTC&useSSL=false
# username: root
# password:
# autoCommit: true
# connectionTimeoutMilliseconds: 30000
# idleTimeoutMilliseconds: 60000
# maxLifetimeMilliseconds: 1800000
......@@ -24,7 +23,6 @@
# url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false
# username: root
# password:
# autoCommit: true
# connectionTimeoutMilliseconds: 30000
# idleTimeoutMilliseconds: 60000
# maxLifetimeMilliseconds: 1800000
......@@ -33,7 +31,6 @@
# url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false
# username: root
# password:
# autoCommit: true
# connectionTimeoutMilliseconds: 30000
# idleTimeoutMilliseconds: 60000
# maxLifetimeMilliseconds: 1800000
......
......@@ -15,7 +15,6 @@
# url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
# username: root
# password:
# autoCommit: true
# connectionTimeoutMilliseconds: 30000
# idleTimeoutMilliseconds: 60000
# maxLifetimeMilliseconds: 1800000
......@@ -24,7 +23,6 @@
# url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
# username: root
# password:
# autoCommit: true
# connectionTimeoutMilliseconds: 30000
# idleTimeoutMilliseconds: 60000
# maxLifetimeMilliseconds: 1800000
......
......@@ -91,7 +91,6 @@ public final class ShardingConfigurationLoaderTest {
assertThat(actual.getUrl(), is(expectedURL));
assertThat(actual.getUsername(), is("root"));
assertNull(actual.getPassword());
assertTrue(actual.isAutoCommit());
assertThat(actual.getConnectionTimeoutMilliseconds(), is(30000L));
assertThat(actual.getIdleTimeoutMilliseconds(), is(60000L));
assertThat(actual.getMaxLifetimeMilliseconds(), is(1800000L));
......
......@@ -19,6 +19,7 @@ package io.shardingsphere.shardingproxy.util;
import io.shardingsphere.core.config.DataSourceConfiguration;
import io.shardingsphere.core.rule.DataSourceParameter;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import java.util.HashMap;
......@@ -52,11 +53,34 @@ public final class DataSourceConverterTest {
@Test
public void assertGetDataSourceConfigurationMap() {
Map<String, DataSourceParameter> dataSourceParameterMap = new HashMap<>(2, 1);
dataSourceParameterMap.put("ds_0", new DataSourceParameter());
dataSourceParameterMap.put("ds_1", new DataSourceParameter());
dataSourceParameterMap.put("ds_0", crateDataSourceParameter());
dataSourceParameterMap.put("ds_1", crateDataSourceParameter());
Map<String, DataSourceConfiguration> actual = DataSourceConverter.getDataSourceConfigurationMap(dataSourceParameterMap);
assertThat(actual.size(), is(2));
assertNotNull(actual.get("ds_0"));
assertNotNull(actual.get("ds_1"));
assertThatParameter(actual.get("ds_0"));
assertThatParameter(actual.get("ds_1"));
}
private DataSourceParameter crateDataSourceParameter() {
DataSourceParameter result = new DataSourceParameter();
result.setUsername("root");
result.setUrl("jdbc:mysql://localhost:3306/demo_ds");
result.setPassword("root");
return result;
}
private void assertThatParameter(final DataSourceConfiguration actual) {
Map<String, Object> properties = actual.getProperties();
assertThat(properties.get("maxPoolSize"), CoreMatchers.<Object>is(50));
assertThat(properties.get("minPoolSize"), CoreMatchers.<Object>is(1));
assertThat(properties.get("connectionTimeoutMilliseconds"), CoreMatchers.<Object>is(30 * 1000L));
assertThat(properties.get("idleTimeoutMilliseconds"), CoreMatchers.<Object>is(60 * 1000L));
assertThat(properties.get("maxLifetimeMilliseconds"), CoreMatchers.<Object>is(0L));
assertThat(properties.get("maintenanceIntervalMilliseconds"), CoreMatchers.<Object>is(30 * 1000L));
assertThat(properties.get("url"), CoreMatchers.<Object>is("jdbc:mysql://localhost:3306/demo_ds"));
assertThat(properties.get("username"), CoreMatchers.<Object>is("root"));
assertThat(properties.get("password"), CoreMatchers.<Object>is("root"));
}
}
......@@ -5,7 +5,6 @@ dataSources:
url: jdbc:mysql://127.0.0.1:3306/master_ds
username: root
password:
autoCommit: true
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
......@@ -14,7 +13,6 @@ dataSources:
url: jdbc:mysql://127.0.0.1:3306/slave_ds_0
username: root
password:
autoCommit: true
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
......@@ -23,7 +21,6 @@ dataSources:
url: jdbc:mysql://127.0.0.1:3306/slave_ds_1
username: root
password:
autoCommit: true
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
......
......@@ -5,7 +5,6 @@ dataSources:
url: jdbc:mysql://127.0.0.1:3306/ds_0
username: root
password:
autoCommit: true
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
......@@ -14,7 +13,6 @@ dataSources:
url: jdbc:mysql://127.0.0.1:3306/ds_1
username: root
password:
autoCommit: true
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
......
......@@ -20,7 +20,6 @@ package io.shardingsphere.transaction.xa.manager;
import com.atomikos.beans.PropertyException;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.constant.transaction.ProxyPoolType;
import io.shardingsphere.core.rule.DataSourceParameter;
import io.shardingsphere.transaction.xa.convert.dialect.XADataSourceFactory;
import io.shardingsphere.transaction.xa.convert.dialect.XADatabaseType;
......@@ -51,7 +50,6 @@ public class AtomikosDataSourceBeanWrapperTest {
@Test
public void assertWrapToAtomikosDataSourceBean() throws PropertyException {
AtomikosDataSourceBeanWrapper atomikosDataSourceBeanWrapper = new AtomikosDataSourceBeanWrapper();
parameter.setProxyDatasourceType(ProxyPoolType.VENDOR);
AtomikosDataSourceBean targetDataSource = (AtomikosDataSourceBean) atomikosDataSourceBeanWrapper.wrap(xaDataSource, "ds1", parameter);
assertThat(targetDataSource, Matchers.instanceOf(AtomikosDataSourceBean.class));
assertThat(targetDataSource.getXaDataSource(), is(xaDataSource));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册