This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 4f40d1a Refactor DataSourceConfiguration (#12268)
4f40d1a is described below
commit 4f40d1a2c4eb281892f2d8a296c71119366cb7ab
Author: Haoran Meng <[email protected]>
AuthorDate: Tue Sep 7 20:52:23 2021 +0800
Refactor DataSourceConfiguration (#12268)
* Refactor DataSourceConfiguration
* For code style
---
.../config/datasource/DataSourceConfiguration.java | 124 ++--------------
.../config/datasource/DataSourceConverter.java | 31 +++-
.../config/datasource/DataSourceValidator.java | 2 +-
.../datasource/creator/DataSourceCreator.java | 45 ++++++
.../creator/DataSourceCreatorFactory.java | 44 ++++++
.../impl/AbstractDataSourceCreator.java} | 159 ++++++---------------
.../creator/impl/DefaultDataSourceCreator.java | 50 +++++++
.../creator/impl/HikariDataSourceCreator.java | 66 +++++++++
...fra.config.datasource.creator.DataSourceCreator | 18 +++
.../infra/config/DataSourceConfigurationTest.java | 66 +--------
.../infra/config/DataSourceConverterTest.java | 82 +++++++++--
.../infra/datasource/DataSourceValidatorTest.java | 27 ++--
.../mode/persist/PersistServiceTest.java | 3 +-
.../service/impl/DataSourcePersistServiceTest.java | 7 +-
.../cluster/ClusterContextManagerBuilder.java | 10 +-
.../cluster/ClusterContextManagerBuilderTest.java | 13 +-
.../StandaloneContextManagerBuilder.java | 10 +-
.../proxy/initializer/BootstrapInitializer.java | 12 +-
.../scaling/core/util/JobConfigurationUtil.java | 3 +-
19 files changed, 415 insertions(+), 357 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
index f4037a2..b533460 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
@@ -17,27 +17,14 @@
package org.apache.shardingsphere.infra.config.datasource;
-import com.google.common.base.CaseFormat;
import com.google.common.base.Objects;
-import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import lombok.SneakyThrows;
-import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import javax.sql.DataSource;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
-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;
/**
@@ -49,20 +36,6 @@ public final class DataSourceConfiguration {
public static final String CUSTOM_POOL_PROPS_KEY = "customPoolProps";
- private static final String GETTER_PREFIX = "get";
-
- private static final String SETTER_PREFIX = "set";
-
- private static final Collection<Class<?>> GENERAL_CLASS_TYPE;
-
- private static final Collection<String> SKIPPED_PROPERTY_NAMES;
-
- static {
- ShardingSphereServiceLoader.register(JDBCParameterDecorator.class);
- GENERAL_CLASS_TYPE = Sets.newHashSet(boolean.class, Boolean.class,
int.class, Integer.class, long.class, Long.class, String.class,
Collection.class, List.class);
- SKIPPED_PROPERTY_NAMES = Sets.newHashSet("loginTimeout");
- }
-
private final String dataSourceClassName;
private final Map<String, Object> props = new LinkedHashMap<>();
@@ -70,92 +43,6 @@ public final class DataSourceConfiguration {
private final Properties customPoolProps = new Properties();
/**
- * Get data source configuration.
- *
- * @param dataSource data source
- * @return data source configuration
- */
- public static DataSourceConfiguration getDataSourceConfiguration(final
DataSource dataSource) {
- DataSourceConfiguration result = new
DataSourceConfiguration(dataSource.getClass().getName());
- result.props.putAll(findAllGetterProperties(dataSource));
- return result;
- }
-
- @SneakyThrows(ReflectiveOperationException.class)
- private static Map<String, Object> findAllGetterProperties(final Object
target) {
- Collection<Method> allGetterMethods =
findAllGetterMethods(target.getClass());
- Map<String, Object> result = new
LinkedHashMap<>(allGetterMethods.size(), 1);
- for (Method each : allGetterMethods) {
- 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));
- }
- }
- return result;
- }
-
- private static Collection<Method> findAllGetterMethods(final Class<?>
clazz) {
- Method[] methods = clazz.getMethods();
- Collection<Method> result = new HashSet<>(methods.length);
- for (Method each : methods) {
- if (each.getName().startsWith(GETTER_PREFIX) && 0 ==
each.getParameterTypes().length) {
- result.add(each);
- }
- }
- return result;
- }
-
- /**
- * Create data source.
- *
- * @return data source
- */
- @SuppressWarnings({"unchecked", "rawtypes"})
- @SneakyThrows(ReflectiveOperationException.class)
- public DataSource createDataSource() {
- DataSource result = (DataSource)
Class.forName(dataSourceClassName).getConstructor().newInstance();
- Method[] methods = result.getClass().getMethods();
- Map<String, Object> allProps = new HashMap<>(props);
- allProps.putAll((Map) customPoolProps);
- for (Entry<String, Object> entry : allProps.entrySet()) {
- if (SKIPPED_PROPERTY_NAMES.contains(entry.getKey())) {
- continue;
- }
- try {
- Optional<Method> setterMethod = findSetterMethod(methods,
entry.getKey());
- if (setterMethod.isPresent() && null != entry.getValue()) {
- setDataSourceField(setterMethod.get(), result,
entry.getValue());
- }
- } catch (final IllegalArgumentException ex) {
- throw new ShardingSphereConfigurationException("Incorrect
configuration item: the property %s of the dataSource, because %s",
entry.getKey(), ex.getMessage());
- }
- }
- return JDBCParameterDecoratorHelper.decorate(result);
- }
-
- private void setDataSourceField(final Method method, final DataSource
target, final Object value) throws InvocationTargetException,
IllegalAccessException {
- Class<?> paramType = method.getParameterTypes()[0];
- if (paramType == int.class) {
- method.invoke(target, Integer.parseInt(value.toString()));
- } else if (paramType == long.class) {
- method.invoke(target, Long.parseLong(value.toString()));
- } else if (paramType == boolean.class || paramType == Boolean.class) {
- method.invoke(target, Boolean.parseBoolean(value.toString()));
- } else if (paramType == String.class) {
- method.invoke(target, value.toString());
- } else {
- method.invoke(target, value);
- }
- }
-
- private Optional<Method> findSetterMethod(final Method[] methods, final
String property) {
- String setterMethodName = SETTER_PREFIX +
CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, property);
- return Arrays.stream(methods)
- .filter(each -> each.getName().equals(setterMethodName) && 1
== each.getParameterTypes().length)
- .findFirst();
- }
-
- /**
* Add property synonym to shared configuration.
*
* @param originalName original key for data source configuration property
@@ -171,6 +58,17 @@ public final class DataSourceConfiguration {
}
}
+ /**
+ * Get all props.
+ *
+ * @return map of all props
+ */
+ public Map<String, Object> getAllProps() {
+ Map<String, Object> result = new HashMap<>(props);
+ result.putAll((Map) customPoolProps);
+ return result;
+ }
+
@Override
public boolean equals(final Object obj) {
return this == obj || null != obj && getClass() == obj.getClass() &&
equalsByProperties((DataSourceConfiguration) obj);
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
index a09dd1e..12d116f 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConverter.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.infra.config.datasource;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import
org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreatorFactory;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import javax.sql.DataSource;
import java.util.LinkedHashMap;
@@ -32,6 +34,31 @@ import java.util.stream.Collectors;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceConverter {
+ static {
+ ShardingSphereServiceLoader.register(JDBCParameterDecorator.class);
+ }
+
+ /**
+ * Get data source.
+ *
+ * @param dataSourceConfiguration data source configuration
+ * @return data source
+ */
+ public static DataSource getDataSource(final DataSourceConfiguration
dataSourceConfiguration) {
+ return
JDBCParameterDecoratorHelper.decorate(DataSourceCreatorFactory.getDataSourceCreator(dataSourceConfiguration.getDataSourceClassName())
+ .createDataSource(dataSourceConfiguration));
+ }
+
+ /**
+ * Get data source configuration.
+ *
+ * @param dataSource data source
+ * @return data source configuration
+ */
+ public static DataSourceConfiguration getDataSourceConfiguration(final
DataSource dataSource) {
+ return
DataSourceCreatorFactory.getDataSourceCreator(dataSource.getClass().getName()).createDataSourceConfiguration(dataSource);
+ }
+
/**
* Get data source map.
*
@@ -40,7 +67,7 @@ public final class DataSourceConverter {
*/
public static Map<String, DataSource> getDataSourceMap(final Map<String,
DataSourceConfiguration> dataSourceConfigMap) {
return
dataSourceConfigMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
- entry -> entry.getValue().createDataSource(), (oldValue,
currentValue) -> oldValue, LinkedHashMap::new));
+ entry -> getDataSource(entry.getValue()), (oldValue, currentValue)
-> oldValue, LinkedHashMap::new));
}
/**
@@ -51,6 +78,6 @@ public final class DataSourceConverter {
*/
public static Map<String, DataSourceConfiguration>
getDataSourceConfigurationMap(final Map<String, DataSource> dataSourceMap) {
return dataSourceMap.entrySet().stream().collect(
- Collectors.toMap(Entry::getKey, entry ->
DataSourceConfiguration.getDataSourceConfiguration(entry.getValue()),
(oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+ Collectors.toMap(Entry::getKey, entry ->
getDataSourceConfiguration(entry.getValue()), (oldValue, currentValue) ->
oldValue, LinkedHashMap::new));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
index ad97190..c64907a 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
@@ -33,7 +33,7 @@ public final class DataSourceValidator {
public boolean validate(final DataSourceConfiguration
dataSourceConfiguration) {
DataSource dataSource = null;
try {
- dataSource = dataSourceConfiguration.createDataSource();
+ dataSource =
DataSourceConverter.getDataSource(dataSourceConfiguration);
return true;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreator.java
new file mode 100644
index 0000000..044c94e
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreator.java
@@ -0,0 +1,45 @@
+/*
+ * 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.creator;
+
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.spi.typed.TypedSPI;
+
+import javax.sql.DataSource;
+
+/**
+ * Data source creator.
+ */
+public interface DataSourceCreator extends TypedSPI {
+
+ /**
+ * Create data source configuration by data source.
+ *
+ * @param dataSource data source
+ * @return data source configuration
+ */
+ DataSourceConfiguration createDataSourceConfiguration(DataSource
dataSource);
+
+ /**
+ * Create data source by data source configuration.
+ *
+ * @param dataSourceConfig data source configuration
+ * @return data source
+ */
+ DataSource createDataSource(DataSourceConfiguration dataSourceConfig);
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreatorFactory.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreatorFactory.java
new file mode 100644
index 0000000..9bb18d5
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/DataSourceCreatorFactory.java
@@ -0,0 +1,44 @@
+/*
+ * 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.creator;
+
+import
org.apache.shardingsphere.infra.config.datasource.creator.impl.DefaultDataSourceCreator;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
+
+import java.util.Properties;
+
+/**
+ * Data source creator factory.
+ */
+public final class DataSourceCreatorFactory {
+
+ static {
+ ShardingSphereServiceLoader.register(DataSourceCreator.class);
+ }
+
+ /**
+ * Get data source creator.
+ *
+ * @param dataSourceClassName data source class name
+ * @return data source creator
+ */
+ public static DataSourceCreator getDataSourceCreator(final String
dataSourceClassName) {
+ return TypedSPIRegistry.findRegisteredService(DataSourceCreator.class,
dataSourceClassName, new Properties()).orElse(new DefaultDataSourceCreator());
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
similarity index 50%
copy from
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
copy to
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
index f4037a2..a7b57c2 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceConfiguration.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/AbstractDataSourceCreator.java
@@ -15,122 +15,62 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.config.datasource;
+package org.apache.shardingsphere.infra.config.datasource.creator.impl;
import com.google.common.base.CaseFormat;
-import com.google.common.base.Objects;
import com.google.common.collect.Sets;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import
org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreator;
import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
import javax.sql.DataSource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
-import java.util.HashMap;
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;
/**
- * Data source configuration.
+ * Abstract data source creator.
*/
-@RequiredArgsConstructor
-@Getter
-public final class DataSourceConfiguration {
-
- public static final String CUSTOM_POOL_PROPS_KEY = "customPoolProps";
-
- private static final String GETTER_PREFIX = "get";
-
- private static final String SETTER_PREFIX = "set";
-
- private static final Collection<Class<?>> GENERAL_CLASS_TYPE;
-
- private static final Collection<String> SKIPPED_PROPERTY_NAMES;
+public abstract class AbstractDataSourceCreator implements DataSourceCreator {
static {
- ShardingSphereServiceLoader.register(JDBCParameterDecorator.class);
GENERAL_CLASS_TYPE = Sets.newHashSet(boolean.class, Boolean.class,
int.class, Integer.class, long.class, Long.class, String.class,
Collection.class, List.class);
SKIPPED_PROPERTY_NAMES = Sets.newHashSet("loginTimeout");
}
- private final String dataSourceClassName;
+ protected static final Collection<Class<?>> GENERAL_CLASS_TYPE;
- private final Map<String, Object> props = new LinkedHashMap<>();
+ protected static final Collection<String> SKIPPED_PROPERTY_NAMES;
- private final Properties customPoolProps = new Properties();
+ private static final String GETTER_PREFIX = "get";
- /**
- * Get data source configuration.
- *
- * @param dataSource data source
- * @return data source configuration
- */
- public static DataSourceConfiguration getDataSourceConfiguration(final
DataSource dataSource) {
- DataSourceConfiguration result = new
DataSourceConfiguration(dataSource.getClass().getName());
- result.props.putAll(findAllGetterProperties(dataSource));
- return result;
- }
+ private static final String SETTER_PREFIX = "set";
@SneakyThrows(ReflectiveOperationException.class)
- private static Map<String, Object> findAllGetterProperties(final Object
target) {
- Collection<Method> allGetterMethods =
findAllGetterMethods(target.getClass());
- Map<String, Object> result = new
LinkedHashMap<>(allGetterMethods.size(), 1);
- for (Method each : allGetterMethods) {
- 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));
- }
- }
- return result;
- }
-
- private static Collection<Method> findAllGetterMethods(final Class<?>
clazz) {
- Method[] methods = clazz.getMethods();
- Collection<Method> result = new HashSet<>(methods.length);
- for (Method each : methods) {
- if (each.getName().startsWith(GETTER_PREFIX) && 0 ==
each.getParameterTypes().length) {
- result.add(each);
- }
- }
- return result;
+ protected DataSource buildDataSource(final String dataSourceClassName) {
+ return (DataSource)
Class.forName(dataSourceClassName).getConstructor().newInstance();
}
- /**
- * Create data source.
- *
- * @return data source
- */
- @SuppressWarnings({"unchecked", "rawtypes"})
@SneakyThrows(ReflectiveOperationException.class)
- public DataSource createDataSource() {
- DataSource result = (DataSource)
Class.forName(dataSourceClassName).getConstructor().newInstance();
- Method[] methods = result.getClass().getMethods();
- Map<String, Object> allProps = new HashMap<>(props);
- allProps.putAll((Map) customPoolProps);
- for (Entry<String, Object> entry : allProps.entrySet()) {
- if (SKIPPED_PROPERTY_NAMES.contains(entry.getKey())) {
- continue;
+ protected void setField(final DataSource dataSource, final Method[]
methods, final String property, final Object value) {
+ try {
+ if (isSkip(property)) {
+ return;
}
- try {
- Optional<Method> setterMethod = findSetterMethod(methods,
entry.getKey());
- if (setterMethod.isPresent() && null != entry.getValue()) {
- setDataSourceField(setterMethod.get(), result,
entry.getValue());
- }
- } catch (final IllegalArgumentException ex) {
- throw new ShardingSphereConfigurationException("Incorrect
configuration item: the property %s of the dataSource, because %s",
entry.getKey(), ex.getMessage());
+ Optional<Method> setterMethod = findSetterMethod(methods,
property);
+ if (setterMethod.isPresent() && null != value) {
+ setDataSourceField(setterMethod.get(), dataSource, value);
}
+ } catch (final IllegalArgumentException ex) {
+ throw new ShardingSphereConfigurationException("Incorrect
configuration item: the property %s of the dataSource, because %s", property,
ex.getMessage());
}
- return JDBCParameterDecoratorHelper.decorate(result);
}
private void setDataSourceField(final Method method, final DataSource
target, final Object value) throws InvocationTargetException,
IllegalAccessException {
@@ -155,48 +95,37 @@ public final class DataSourceConfiguration {
.findFirst();
}
- /**
- * Add property synonym to shared configuration.
- *
- * @param originalName original key for data source configuration property
- * @param synonym property synonym for configuration
- */
- public void addPropertySynonym(final String originalName, final String
synonym) {
- if (props.containsKey(originalName)) {
- props.put(synonym, props.get(originalName));
- }
- // TODO fixes by #6709
- if (props.containsKey(synonym)) {
- props.put(originalName, props.get(synonym));
- }
+ protected DataSourceConfiguration buildDataSourceConfig(final DataSource
dataSource) {
+ DataSourceConfiguration result = new
DataSourceConfiguration(dataSource.getClass().getName());
+ result.getProps().putAll(findAllGetterProperties(dataSource));
+ return result;
}
- @Override
- public boolean equals(final Object obj) {
- return this == obj || null != obj && getClass() == obj.getClass() &&
equalsByProperties((DataSourceConfiguration) obj);
+ @SneakyThrows(ReflectiveOperationException.class)
+ private Map<String, Object> findAllGetterProperties(final Object target) {
+ Collection<Method> allGetterMethods =
findAllGetterMethods(target.getClass());
+ Map<String, Object> result = new
LinkedHashMap<>(allGetterMethods.size(), 1);
+ for (Method each : allGetterMethods) {
+ 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));
+ }
+ }
+ return result;
}
- private boolean equalsByProperties(final DataSourceConfiguration
dataSourceConfig) {
- if (!dataSourceClassName.equals(dataSourceConfig.dataSourceClassName))
{
- return false;
- }
- for (Entry<String, Object> entry : props.entrySet()) {
- if (!dataSourceConfig.props.containsKey(entry.getKey())) {
- continue;
- }
- if
(!String.valueOf(entry.getValue()).equals(String.valueOf(dataSourceConfig.props.get(entry.getKey()))))
{
- return false;
+ private static Collection<Method> findAllGetterMethods(final Class<?>
clazz) {
+ Method[] methods = clazz.getMethods();
+ Collection<Method> result = new HashSet<>(methods.length);
+ for (Method each : methods) {
+ if (each.getName().startsWith(GETTER_PREFIX) && 0 ==
each.getParameterTypes().length) {
+ result.add(each);
}
}
- return true;
+ return result;
}
- @Override
- public int hashCode() {
- StringBuilder stringBuilder = new StringBuilder();
- for (Entry<String, Object> entry : props.entrySet()) {
- stringBuilder.append(entry.getKey()).append(entry.getValue());
- }
- return Objects.hashCode(dataSourceClassName, stringBuilder.toString());
+ private boolean isSkip(final String property) {
+ return SKIPPED_PROPERTY_NAMES.contains(property);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/DefaultDataSourceCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/DefaultDataSourceCreator.java
new file mode 100644
index 0000000..d2e0543
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/DefaultDataSourceCreator.java
@@ -0,0 +1,50 @@
+/*
+ * 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.creator.impl;
+
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Method;
+import java.util.Map.Entry;
+
+/**
+ * Default data source creator.
+ */
+public final class DefaultDataSourceCreator extends AbstractDataSourceCreator {
+
+ @Override
+ public DataSourceConfiguration createDataSourceConfiguration(final
DataSource dataSource) {
+ return buildDataSourceConfig(dataSource);
+ }
+
+ @Override
+ public DataSource createDataSource(final DataSourceConfiguration
dataSourceConfig) {
+ DataSource result =
buildDataSource(dataSourceConfig.getDataSourceClassName());
+ Method[] methods = result.getClass().getMethods();
+ for (Entry<String, Object> entry :
dataSourceConfig.getAllProps().entrySet()) {
+ setField(result, methods, entry.getKey(), entry.getValue());
+ }
+ return result;
+ }
+
+ @Override
+ public String getType() {
+ return "";
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/HikariDataSourceCreator.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/HikariDataSourceCreator.java
new file mode 100644
index 0000000..fa82637
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/creator/impl/HikariDataSourceCreator.java
@@ -0,0 +1,66 @@
+/*
+ * 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.creator.impl;
+
+import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Hikari data source creator.
+ */
+public final class HikariDataSourceCreator extends AbstractDataSourceCreator {
+
+ private final Map<String, Object> skippedProperties = new HashMap<>();
+
+ public HikariDataSourceCreator() {
+ skippedProperties.put("minimumIdle", -1);
+ skippedProperties.put("maximumPoolSize", -1);
+ }
+
+ @Override
+ public DataSource createDataSource(final DataSourceConfiguration
dataSourceConfig) {
+ DataSource result =
buildDataSource(dataSourceConfig.getDataSourceClassName());
+ Method[] methods = result.getClass().getMethods();
+ for (Entry<String, Object> entry :
dataSourceConfig.getAllProps().entrySet()) {
+ if (isInvalidProperty(entry.getKey(), entry.getValue())) {
+ continue;
+ }
+ setField(result, methods, entry.getKey(), entry.getValue());
+ }
+ return result;
+ }
+
+ private boolean isInvalidProperty(final String property, final Object
value) {
+ return skippedProperties.containsKey(property) && null != value &&
value.equals(skippedProperties.get(property));
+ }
+
+ @Override
+ public DataSourceConfiguration createDataSourceConfiguration(final
DataSource dataSource) {
+ return buildDataSourceConfig(dataSource);
+ }
+
+ @Override
+ public String getType() {
+ return "com.zaxxer.hikari.HikariDataSource";
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreator
b/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreator
new file mode 100644
index 0000000..94136aa
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.datasource.creator.DataSourceCreator
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.config.datasource.creator.impl.HikariDataSourceCreator
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConfigurationTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConfigurationTest.java
index 707b010..2ec765f 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConfigurationTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConfigurationTest.java
@@ -20,11 +20,11 @@ package org.apache.shardingsphere.infra.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -45,71 +45,13 @@ public final class DataSourceConfigurationTest {
public ExpectedException thrown = ExpectedException.none();
@Test
- public void assertGetDataSourceConfiguration() throws SQLException {
- HikariDataSource actualDataSource = new HikariDataSource();
- actualDataSource.setDriverClassName("org.h2.Driver");
-
actualDataSource.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- actualDataSource.setUsername("root");
- actualDataSource.setPassword("root");
- actualDataSource.setLoginTimeout(1);
- DataSourceConfiguration actual =
DataSourceConfiguration.getDataSourceConfiguration(actualDataSource);
- assertThat(actual.getDataSourceClassName(),
is(HikariDataSource.class.getName()));
- assertThat(actual.getProps().get("driverClassName").toString(),
is("org.h2.Driver"));
- assertThat(actual.getProps().get("jdbcUrl").toString(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
- assertThat(actual.getProps().get("username").toString(), is("root"));
- assertThat(actual.getProps().get("password").toString(), is("root"));
- assertNull(actual.getProps().get("loginTimeout"));
- }
-
- @Test
- public void assertCreateDataSource() {
- Map<String, Object> props = new HashMap<>(16, 1);
- props.put("driverClassName", "org.h2.Driver");
- props.put("jdbcUrl",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- props.put("username", "root");
- props.put("password", "root");
- props.put("loginTimeout", "5000");
- props.put("maximumPoolSize", "50");
- props.put("minimumIdle", "1");
- props.put("maxLifetime", "60000");
- props.put("test", "test");
- DataSourceConfiguration dataSourceConfig = new
DataSourceConfiguration(HikariDataSource.class.getName());
- dataSourceConfig.getProps().putAll(props);
- HikariDataSource actual = (HikariDataSource)
dataSourceConfig.createDataSource();
- assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
- assertThat(actual.getJdbcUrl(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
- assertThat(actual.getUsername(), is("root"));
- assertThat(actual.getPassword(), is("root"));
- assertThat(actual.getMaximumPoolSize(), is(50));
- assertThat(actual.getMinimumIdle(), is(1));
- assertThat(actual.getMaxLifetime(), is(60000L));
- }
-
- @Test
- public void assertCreateDataSourceWithIntegerPassword() {
- Map<String, Object> props = new HashMap<>(16, 1);
- props.put("driverClassName", "org.h2.Driver");
- props.put("jdbcUrl",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
- props.put("username", "root");
- props.put("password", 123);
- props.put("loginTimeout", "5000");
- DataSourceConfiguration dataSourceConfig = new
DataSourceConfiguration(HikariDataSource.class.getName());
- dataSourceConfig.getProps().putAll(props);
- HikariDataSource actual = (HikariDataSource)
dataSourceConfig.createDataSource();
- assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
- assertThat(actual.getJdbcUrl(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
- assertThat(actual.getUsername(), is("root"));
- assertThat(actual.getPassword(), is("123"));
- }
-
- @Test
public void assertAddSynonym() {
HikariDataSource actualDataSource = new HikariDataSource();
actualDataSource.setDriverClassName("org.h2.Driver");
actualDataSource.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
actualDataSource.setUsername("root");
actualDataSource.setPassword("root");
- DataSourceConfiguration actual =
DataSourceConfiguration.getDataSourceConfiguration(actualDataSource);
+ DataSourceConfiguration actual =
DataSourceConverter.getDataSourceConfiguration(actualDataSource);
actual.addPropertySynonym("url", "jdbcUrl");
actual.addPropertySynonym("user", "username");
assertThat(actual.getDataSourceClassName(),
is(HikariDataSource.class.getName()));
@@ -183,7 +125,7 @@ public final class DataSourceConfigurationTest {
actualDataSource.setUsername("root");
actualDataSource.setPassword("root");
actualDataSource.setConnectionInitSqls(Arrays.asList("set names
utf8mb4;", "set names utf8;"));
- DataSourceConfiguration actual =
DataSourceConfiguration.getDataSourceConfiguration(actualDataSource);
+ DataSourceConfiguration actual =
DataSourceConverter.getDataSourceConfiguration(actualDataSource);
assertThat(actual.getDataSourceClassName(),
is(BasicDataSource.class.getName()));
assertThat(actual.getProps().get("driverClassName").toString(),
is("org.h2.Driver"));
assertThat(actual.getProps().get("url").toString(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
@@ -210,7 +152,7 @@ public final class DataSourceConfigurationTest {
DataSourceConfiguration dataSourceConfig = new
DataSourceConfiguration(HikariDataSource.class.getName());
dataSourceConfig.getProps().putAll(props);
dataSourceConfig.getProps().putAll(new HashMap(customPoolProps));
- HikariDataSource actual = (HikariDataSource)
dataSourceConfig.createDataSource();
+ HikariDataSource actual = (HikariDataSource)
DataSourceConverter.getDataSource(dataSourceConfig);
assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
assertThat(actual.getJdbcUrl(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
assertThat(actual.getUsername(), is("root"));
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConverterTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConverterTest.java
index 7042594..b49dd61 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConverterTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/config/DataSourceConverterTest.java
@@ -17,39 +17,31 @@
package org.apache.shardingsphere.infra.config;
+import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.junit.Test;
import javax.sql.DataSource;
+import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class DataSourceConverterTest {
@Test
public void assertGetDataSourceMap() {
- Map<String, DataSourceConfiguration> dataSourceConfigurationMap = new
HashMap<>(2, 1);
- DataSourceConfiguration dataSourceConfiguration0 =
mock(DataSourceConfiguration.class);
- DataSource dataSource0 = mock(DataSource.class);
-
when(dataSourceConfiguration0.createDataSource()).thenReturn(dataSource0);
- dataSourceConfigurationMap.put("ds_0", dataSourceConfiguration0);
- DataSourceConfiguration dataSourceConfiguration1 =
mock(DataSourceConfiguration.class);
- DataSource dataSource1 = mock(DataSource.class);
-
when(dataSourceConfiguration1.createDataSource()).thenReturn(dataSource1);
- dataSourceConfigurationMap.put("ds_1", dataSourceConfiguration1);
+ Map<String, DataSourceConfiguration> dataSourceConfigurationMap = new
HashMap<>(1, 1);
+ dataSourceConfigurationMap.put("ds_0",
createDataSourceConfiguration());
Map<String, DataSource> actual =
DataSourceConverter.getDataSourceMap(dataSourceConfigurationMap);
- assertThat(actual.size(), is(2));
- assertThat(actual.get("ds_0"), is(dataSource0));
- assertThat(actual.get("ds_1"), is(dataSource1));
+ assertThat(actual.size(), is(1));
}
@Test
@@ -60,6 +52,52 @@ public final class DataSourceConverterTest {
assertNotNull(actual.get("ds_1"));
}
+ @Test
+ public void assertGetDataSourceConfiguration() throws SQLException {
+ HikariDataSource actualDataSource = new HikariDataSource();
+ actualDataSource.setDriverClassName("org.h2.Driver");
+
actualDataSource.setJdbcUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ actualDataSource.setUsername("root");
+ actualDataSource.setPassword("root");
+ actualDataSource.setLoginTimeout(1);
+ DataSourceConfiguration actual =
DataSourceConverter.getDataSourceConfiguration(actualDataSource);
+ assertThat(actual.getDataSourceClassName(),
is(HikariDataSource.class.getName()));
+ assertThat(actual.getProps().get("driverClassName").toString(),
is("org.h2.Driver"));
+ assertThat(actual.getProps().get("jdbcUrl").toString(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
+ assertThat(actual.getProps().get("username").toString(), is("root"));
+ assertThat(actual.getProps().get("password").toString(), is("root"));
+ assertNull(actual.getProps().get("loginTimeout"));
+ }
+
+ @Test
+ public void assertGetDataSource() {
+ HikariDataSource actual = (HikariDataSource)
DataSourceConverter.getDataSource(createDataSourceConfiguration());
+ assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
+ assertThat(actual.getJdbcUrl(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
+ assertThat(actual.getUsername(), is("root"));
+ assertThat(actual.getPassword(), is("root"));
+ assertThat(actual.getMaximumPoolSize(), is(50));
+ assertThat(actual.getMinimumIdle(), is(1));
+ assertThat(actual.getMaxLifetime(), is(60000L));
+ }
+
+ @Test
+ public void assertCreateDataSourceWithIntegerPassword() {
+ Map<String, Object> props = new HashMap<>(16, 1);
+ props.put("driverClassName", "org.h2.Driver");
+ props.put("jdbcUrl",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ props.put("username", "root");
+ props.put("password", 123);
+ props.put("loginTimeout", "5000");
+ DataSourceConfiguration dataSourceConfig = new
DataSourceConfiguration(HikariDataSource.class.getName());
+ dataSourceConfig.getProps().putAll(props);
+ HikariDataSource actual = (HikariDataSource)
DataSourceConverter.getDataSource(dataSourceConfig);
+ assertThat(actual.getDriverClassName(), is("org.h2.Driver"));
+ assertThat(actual.getJdbcUrl(),
is("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL"));
+ assertThat(actual.getUsername(), is("root"));
+ assertThat(actual.getPassword(), is("123"));
+ }
+
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new LinkedHashMap<>(2, 1);
result.put("ds_0", createDataSource("ds_0"));
@@ -75,4 +113,20 @@ public final class DataSourceConverterTest {
result.setPassword("root");
return result;
}
+
+ private DataSourceConfiguration createDataSourceConfiguration() {
+ Map<String, Object> props = new HashMap<>(16, 1);
+ props.put("driverClassName", "org.h2.Driver");
+ props.put("jdbcUrl",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ props.put("username", "root");
+ props.put("password", "root");
+ props.put("loginTimeout", "5000");
+ props.put("maximumPoolSize", "50");
+ props.put("minimumIdle", "1");
+ props.put("maxLifetime", "60000");
+ props.put("test", "test");
+ DataSourceConfiguration result = new
DataSourceConfiguration(HikariDataSource.class.getName());
+ result.getProps().putAll(props);
+ return result;
+ }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
index e8c0916..a98dcce 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
@@ -17,28 +17,33 @@
package org.apache.shardingsphere.infra.datasource;
+import com.zaxxer.hikari.HikariDataSource;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceValidator;
import org.junit.Test;
-import javax.sql.DataSource;
+import java.util.HashMap;
+import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public final class DataSourceValidatorTest {
-
+
@Test
public void assertValidate() {
DataSourceValidator dataSourceValidator = new DataSourceValidator();
- DataSourceConfiguration dataSourceConfiguration =
mock(DataSourceConfiguration.class);
-
when(dataSourceConfiguration.createDataSource()).thenReturn(mock(DataSource.class));
- assertThat(dataSourceValidator.validate(dataSourceConfiguration),
is(Boolean.TRUE));
- when(dataSourceConfiguration.createDataSource()).thenReturn(null);
- assertThat(dataSourceValidator.validate(dataSourceConfiguration),
is(Boolean.TRUE));
- when(dataSourceConfiguration.createDataSource()).thenThrow(new
RuntimeException());
- assertThat(dataSourceValidator.validate(dataSourceConfiguration),
is(Boolean.FALSE));
+
assertThat(dataSourceValidator.validate(createDataSourceConfiguration()),
is(Boolean.TRUE));
+ }
+
+ private DataSourceConfiguration createDataSourceConfiguration() {
+ Map<String, Object> props = new HashMap<>(16, 1);
+ props.put("driverClassName", "org.h2.Driver");
+ props.put("jdbcUrl",
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ props.put("username", "root");
+ props.put("password", "root");
+ DataSourceConfiguration result = new
DataSourceConfiguration(HikariDataSource.class.getName());
+ result.getProps().putAll(props);
+ return result;
}
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/PersistServiceTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/PersistServiceTest.java
index 927e299..777323c 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/PersistServiceTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/PersistServiceTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.mode.persist;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import
org.apache.shardingsphere.mode.persist.service.impl.DataSourcePersistService;
import
org.apache.shardingsphere.mode.persist.service.impl.GlobalRulePersistService;
@@ -101,7 +102,7 @@ public final class PersistServiceTest {
private Map<String, DataSourceConfiguration>
createDataSourceConfigurations() {
return
createDataSourceMap().entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry ->
-
DataSourceConfiguration.getDataSourceConfiguration(entry.getValue()),
(oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+
DataSourceConverter.getDataSourceConfiguration(entry.getValue()), (oldValue,
currentValue) -> oldValue, LinkedHashMap::new));
}
private Map<String, DataSource> createDataSourceMap() {
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
index 212d2b1..703df0b 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.mode.persist.service.impl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.mode.persist.PersistRepository;
import org.apache.shardingsphere.test.mock.MockedDataSource;
import org.junit.Test;
@@ -54,8 +55,8 @@ public final class DataSourcePersistServiceTest {
when(repository.get("/metadata/foo_db/dataSources")).thenReturn(readDataSourceYaml("yaml/persist/data-source.yaml"));
Map<String, DataSourceConfiguration> actual = new
DataSourcePersistService(repository).load("foo_db");
assertThat(actual.size(), is(2));
- assertDataSourceConfiguration(actual.get("ds_0"),
DataSourceConfiguration.getDataSourceConfiguration(createDataSource("ds_0")));
- assertDataSourceConfiguration(actual.get("ds_1"),
DataSourceConfiguration.getDataSourceConfiguration(createDataSource("ds_1")));
+ assertDataSourceConfiguration(actual.get("ds_0"),
DataSourceConverter.getDataSourceConfiguration(createDataSource("ds_0")));
+ assertDataSourceConfiguration(actual.get("ds_1"),
DataSourceConverter.getDataSourceConfiguration(createDataSource("ds_1")));
}
@SneakyThrows({IOException.class, URISyntaxException.class})
@@ -81,7 +82,7 @@ public final class DataSourcePersistServiceTest {
@Test
public void assertAppend() {
- new DataSourcePersistService(repository).append("foo_db",
Collections.singletonMap("foo_ds",
DataSourceConfiguration.getDataSourceConfiguration(createDataSource("foo_ds"))));
+ new DataSourcePersistService(repository).append("foo_db",
Collections.singletonMap("foo_ds",
DataSourceConverter.getDataSourceConfiguration(createDataSource("foo_ds"))));
String expected =
readDataSourceYaml("yaml/persist/data-source-foo.yaml");
verify(repository).persist("/metadata/foo_db/dataSources", expected);
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
index 97c0aa1..039ecdc 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilder.java
@@ -216,14 +216,6 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
||
!dataSourceConfigurationMap.get(entry.getKey()).equals(entry.getValue())).collect(Collectors.toMap(Entry::getKey,
Entry::getValue));
}
- private Map<String, DataSource> createDataSources(final Map<String,
DataSourceConfiguration> dataSourceConfigs) {
- Map<String, DataSource> result = new
LinkedHashMap<>(dataSourceConfigs.size(), 1);
- for (Entry<String, DataSourceConfiguration> each :
dataSourceConfigs.entrySet()) {
- result.put(each.getKey(), each.getValue().createDataSource());
- }
- return result;
- }
-
private Map<String, Collection<RuleConfiguration>> loadSchemaRules(final
PersistService persistService, final Collection<String> schemaNames) {
return schemaNames.stream().collect(Collectors.toMap(
each -> each, each ->
persistService.getSchemaRuleService().load(each), (oldValue, currentValue) ->
oldValue, LinkedHashMap::new));
@@ -485,7 +477,7 @@ public final class ClusterContextManagerBuilder implements
ContextManagerBuilder
private Map<String, Map<String, DataSource>> getChangedDataSources(final
Map<String, Map<String, DataSourceConfiguration>>
changedDataSourceConfigurations) {
Map<String, Map<String, DataSource>> result = new
LinkedHashMap<>(changedDataSourceConfigurations.size(), 1);
for (Entry<String, Map<String, DataSourceConfiguration>> entry :
changedDataSourceConfigurations.entrySet()) {
- result.put(entry.getKey(), createDataSources(entry.getValue()));
+ result.put(entry.getKey(),
DataSourceConverter.getDataSourceMap(entry.getValue()));
}
return result;
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilderTest.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilderTest.java
index 490d4f7..31e1429 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilderTest.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/ClusterContextManagerBuilderTest.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import
org.apache.shardingsphere.infra.config.mode.PersistRepositoryConfiguration;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
@@ -136,9 +137,9 @@ public final class ClusterContextManagerBuilderTest {
private Map<String, DataSourceConfiguration> getDataSourceConfigurations()
{
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3,
1);
- result.put("primary_ds",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
- result.put("ds_0",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
- result.put("ds_1",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("primary_ds",
DataSourceConverter.getDataSourceConfiguration(dataSource));
+ result.put("ds_0",
DataSourceConverter.getDataSourceConfiguration(dataSource));
+ result.put("ds_1",
DataSourceConverter.getDataSourceConfiguration(dataSource));
return result;
}
@@ -197,9 +198,9 @@ public final class ClusterContextManagerBuilderTest {
private Map<String, DataSourceConfiguration>
getChangedDataSourceConfigurations() {
MockedDataSource dataSource = new MockedDataSource();
Map<String, DataSourceConfiguration> result = new LinkedHashMap<>(3,
1);
- result.put("primary_ds",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
- result.put("ds_1",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
- result.put("ds_2",
DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("primary_ds",
DataSourceConverter.getDataSourceConfiguration(dataSource));
+ result.put("ds_1",
DataSourceConverter.getDataSourceConfiguration(dataSource));
+ result.put("ds_2",
DataSourceConverter.getDataSourceConfiguration(dataSource));
return result;
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
index a276ac3..384688f 100644
---
a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
+++
b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-standalone-mode/shardingsphere-standalone-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/standalone/StandaloneContextManagerBuilder.java
@@ -156,15 +156,7 @@ public final class StandaloneContextManagerBuilder
implements ContextManagerBuil
private Map<String, Map<String, DataSource>> getChangedDataSources(final
Map<String, Map<String, DataSourceConfiguration>>
changedDataSourceConfigurations) {
Map<String, Map<String, DataSource>> result = new
LinkedHashMap<>(changedDataSourceConfigurations.size(), 1);
for (Entry<String, Map<String, DataSourceConfiguration>> entry :
changedDataSourceConfigurations.entrySet()) {
- result.put(entry.getKey(), createDataSources(entry.getValue()));
- }
- return result;
- }
-
- private Map<String, DataSource> createDataSources(final Map<String,
DataSourceConfiguration> dataSourceConfigs) {
- Map<String, DataSource> result = new
LinkedHashMap<>(dataSourceConfigs.size(), 1);
- for (Entry<String, DataSourceConfiguration> each :
dataSourceConfigs.entrySet()) {
- result.put(each.getKey(), each.getValue().createDataSource());
+ result.put(entry.getKey(),
DataSourceConverter.getDataSourceMap(entry.getValue()));
}
return result;
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index 75090e2..41a8853 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -22,7 +22,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerInfo;
import
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLServerInfo;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
@@ -80,15 +80,7 @@ public final class BootstrapInitializer {
private Map<String, Map<String, DataSource>> getDataSourcesMap(final
Map<String, Map<String, DataSourceParameter>> dataSourceParametersMap) {
Map<String, Map<String, DataSource>> result = new
LinkedHashMap<>(dataSourceParametersMap.size(), 1);
for (Entry<String, Map<String, DataSourceParameter>> entry :
dataSourceParametersMap.entrySet()) {
- result.put(entry.getKey(),
getDataSourceMap(DataSourceParameterConverter.getDataSourceConfigurationMap(entry.getValue())));
- }
- return result;
- }
-
- private Map<String, DataSource> getDataSourceMap(final Map<String,
DataSourceConfiguration> dataSourceConfigMap) {
- Map<String, DataSource> result = new
LinkedHashMap<>(dataSourceConfigMap.size(), 1);
- for (Entry<String, DataSourceConfiguration> entry :
dataSourceConfigMap.entrySet()) {
- result.put(entry.getKey(), entry.getValue().createDataSource());
+ result.put(entry.getKey(),
DataSourceConverter.getDataSourceMap(DataSourceParameterConverter.getDataSourceConfigurationMap(entry.getValue())));
}
return result;
}
diff --git
a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/util/JobConfigurationUtil.java
b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/util/JobConfigurationUtil.java
index c5c9bab..2a03616 100644
---
a/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/util/JobConfigurationUtil.java
+++
b/shardingsphere-scaling/shardingsphere-scaling-core/src/main/java/org/apache/shardingsphere/scaling/core/util/JobConfigurationUtil.java
@@ -27,6 +27,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
import
org.apache.shardingsphere.infra.yaml.config.swapper.YamlDataSourceConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
@@ -222,7 +223,7 @@ public final class JobConfigurationUtil {
ShardingSphereJDBCDataSourceConfiguration sourceConfig =
getSourceConfiguration(jobConfig);
ShardingRuleConfiguration sourceRuleConfig =
ShardingRuleConfigurationSwapper.findAndConvertShardingRuleConfiguration(sourceConfig.getRootConfig().getRules());
Map<String, DataSourceConfiguration> sourceDataSource =
getDataSourceConfigurations(sourceConfig.getRootConfig().getDataSources());
- Map<String, DataSource> dataSourceMap =
sourceDataSource.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> entry.getValue().createDataSource()));
+ Map<String, DataSource> dataSourceMap =
DataSourceConverter.getDataSourceMap(sourceDataSource);
Map<String, Map<String, String>> dataSourceTableNameMap =
toDataSourceTableNameMap(new ShardingRule(sourceRuleConfig, dataSourceMap));
Optional<ShardingRuleConfiguration> targetRuleConfig =
getTargetRuleConfiguration(jobConfig);
filterByShardingDataSourceTables(dataSourceTableNameMap,
jobConfig.getHandleConfig());