This is an automated email from the ASF dual-hosted git repository.

zhangyonglun 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 b77a611  Remove ProxyDataSourceContext (#7337)
b77a611 is described below

commit b77a6110e88f2fe1c0143db11d17a9f8f94bb84b
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Sep 9 01:02:42 2020 +0800

    Remove ProxyDataSourceContext (#7337)
    
    * Refactor ProxyDataSourceContext
    
    * Remove ProxyDataSourceContext
---
 .../backend/schema/ProxyDataSourceContext.java     |  75 --------------
 .../backend/schema/ProxyDataSourceContextTest.java | 109 ---------------------
 .../init/impl/AbstractBootstrapInitializer.java    |  36 ++++++-
 3 files changed, 32 insertions(+), 188 deletions(-)

diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java
deleted file mode 100644
index 1c26bc6..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContext.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.proxy.backend.schema;
-
-import lombok.Getter;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
-import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
-import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;
-
-import javax.sql.DataSource;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.stream.Collectors;
-
-/**
- * Proxy data source context.
- */
-@Getter
-public final class ProxyDataSourceContext {
-    
-    private final DatabaseType databaseType;
-    
-    private final Map<String, Map<String, DataSource>> dataSourcesMap;
-    
-    public ProxyDataSourceContext(final Map<String, Map<String, 
DataSourceParameter>> schemaDataSources) {
-        databaseType = isToInitDatabaseType(schemaDataSources) ? 
DatabaseTypes.getActualDatabaseType(getDatabaseTypeName(schemaDataSources)) : 
new MySQLDatabaseType();
-        dataSourcesMap = createDataSourcesMap(schemaDataSources);
-    }
-    
-    private boolean isToInitDatabaseType(final Map<String, Map<String, 
DataSourceParameter>> schemaDataSources) {
-        return !schemaDataSources.isEmpty() && 
!schemaDataSources.values().iterator().next().isEmpty();
-    }
-    
-    private static String getDatabaseTypeName(final Map<String, Map<String, 
DataSourceParameter>> schemaDataSources) {
-        return 
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType();
-    }
-    
-    private static Map<String, Map<String, DataSource>> 
createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> 
schemaDataSources) {
-        return 
schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, 
entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> 
oldValue, LinkedHashMap::new));
-    }
-    
-    private static Map<String, DataSource> createDataSources(final Map<String, 
DataSourceParameter> dataSourceParameters) {
-        Map<String, DataSource> result = new 
LinkedHashMap<>(dataSourceParameters.size(), 1);
-        for (Entry<String, DataSourceParameter> entry : 
dataSourceParameters.entrySet()) {
-            try {
-                result.put(entry.getKey(), 
JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), 
entry.getValue()));
-                // CHECKSTYLE:OFF
-            } catch (final Exception ex) {
-                // CHECKSTYLE:ON
-                throw new ShardingSphereException(String.format("Can not build 
data source, name is `%s`.", entry.getKey()), ex);
-            }
-        }
-        return result;
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContextTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContextTest.java
deleted file mode 100644
index 281968f..0000000
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/schema/ProxyDataSourceContextTest.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.proxy.backend.schema;
-
-import com.zaxxer.hikari.HikariDataSource;
-import lombok.SneakyThrows;
-import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
-import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
-import org.junit.After;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.any;
-import static org.mockito.Mockito.anyString;
-import static org.mockito.Mockito.reset;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class ProxyDataSourceContextTest {
-    
-    @Mock
-    private JDBCRawBackendDataSourceFactory factory;
-    
-    @After
-    public void tearDown() {
-        reset(factory);
-    }
-    
-    @Test
-    public void assertEmptySchemaDataSources() {
-        ProxyDataSourceContext proxyDataSourceContext = new 
ProxyDataSourceContext(Collections.emptyMap());
-        assertThat(proxyDataSourceContext.getDatabaseType(), 
instanceOf(MySQLDatabaseType.class));
-        assertTrue(proxyDataSourceContext.getDataSourcesMap().isEmpty());
-    }
-    
-    @Test(expected = ShardingSphereException.class)
-    public void assertWrongSchemaDataSources() {
-        Map<String, Map<String, DataSourceParameter>> schemaDataSources = 
createDataSourceParametersMap("jdbc11:mysql11:xxx");
-        new ProxyDataSourceContext(schemaDataSources);
-    }
-    
-    @Test(expected = ShardingSphereException.class)
-    public void assertThrowByBuild() {
-        when(factory.build(anyString(), any())).thenThrow(new 
ShardingSphereException(""));
-        setFactoryInstance(factory);
-        new 
ProxyDataSourceContext(createDataSourceParametersMap("jdbc:mysql:xxx"));
-    }
-    
-    @Test
-    public void assertRightMysqlSchemaDataSources() {
-        when(factory.build(anyString(), any())).thenReturn(new 
HikariDataSource());
-        setFactoryInstance(factory);
-        ProxyDataSourceContext proxyDataSourceContext = new 
ProxyDataSourceContext(createDataSourceParametersMap("jdbc:mysql:xxx"));
-        assertThat(proxyDataSourceContext.getDatabaseType(), 
instanceOf(MySQLDatabaseType.class));
-        assertThat(proxyDataSourceContext.getDataSourcesMap().size(), is(1));
-    }
-    
-    private Map<String, Map<String, DataSourceParameter>> 
createDataSourceParametersMap(final String url) {
-        DataSourceParameter dataSourceParameter = new DataSourceParameter();
-        dataSourceParameter.setUrl(url);
-        Map<String, DataSourceParameter> dataSourceParameterMap = new 
LinkedHashMap<>(1, 1);
-        dataSourceParameterMap.put("order1", dataSourceParameter);
-        Map<String, Map<String, DataSourceParameter>> result = new 
HashMap<>(1, 1);
-        result.put("order", dataSourceParameterMap);
-        return result;
-    }
-    
-    @SneakyThrows(ReflectiveOperationException.class)
-    private void setFactoryInstance(final JDBCRawBackendDataSourceFactory 
factory) {
-        JDBCRawBackendDataSourceFactory jdbcBackendDataSourceFactory = 
(JDBCRawBackendDataSourceFactory) JDBCRawBackendDataSourceFactory.getInstance();
-        Class<?> factoryClass = jdbcBackendDataSourceFactory.getClass();
-        Field field = factoryClass.getDeclaredField("INSTANCE");
-        Field modifiers = field.getClass().getDeclaredField("modifiers");
-        modifiers.setAccessible(true);
-        modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL);
-        field.setAccessible(true);
-        field.set(field, factory);
-    }
-}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/init/impl/AbstractBootstrapInitializer.java
 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/init/impl/AbstractBootstrapInitializer.java
index 1a25381..9594d66 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/init/impl/AbstractBootstrapInitializer.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/init/impl/AbstractBootstrapInitializer.java
@@ -23,7 +23,12 @@ import 
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKe
 import org.apache.shardingsphere.infra.context.SchemaContext;
 import org.apache.shardingsphere.infra.context.SchemaContexts;
 import org.apache.shardingsphere.infra.context.SchemaContextsBuilder;
-import org.apache.shardingsphere.proxy.backend.schema.ProxyDataSourceContext;
+import org.apache.shardingsphere.infra.context.schema.DataSourceParameter;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypes;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.datasource.factory.JDBCRawBackendDataSourceFactory;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.recognizer.JDBCDriverURLRecognizerEngine;
 import org.apache.shardingsphere.proxy.backend.schema.ProxyContext;
 import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
 import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
@@ -38,9 +43,11 @@ import 
org.apache.shardingsphere.transaction.context.impl.StandardTransactionCon
 import javax.sql.DataSource;
 import java.sql.SQLException;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * Abstract bootstrap initializer.
@@ -60,12 +67,33 @@ public abstract class AbstractBootstrapInitializer 
implements BootstrapInitializ
     }
     
     private SchemaContexts createSchemaContexts(final ProxyConfiguration 
proxyConfig) throws SQLException {
-        ProxyDataSourceContext dataSourceContext = new 
ProxyDataSourceContext(proxyConfig.getSchemaDataSources());
-        SchemaContextsBuilder schemaContextsBuilder = new 
SchemaContextsBuilder(
-                dataSourceContext.getDatabaseType(), 
dataSourceContext.getDataSourcesMap(), proxyConfig.getSchemaRules(), 
proxyConfig.getAuthentication(), proxyConfig.getProps());
+        DatabaseType databaseType = 
containsDataSources(proxyConfig.getSchemaDataSources()) ? 
getDatabaseType(proxyConfig.getSchemaDataSources()) : new MySQLDatabaseType();
+        Map<String, Map<String, DataSource>> dataSourcesMap = 
createDataSourcesMap(proxyConfig.getSchemaDataSources());
+        SchemaContextsBuilder schemaContextsBuilder = new 
SchemaContextsBuilder(databaseType, dataSourcesMap, 
proxyConfig.getSchemaRules(), proxyConfig.getAuthentication(), 
proxyConfig.getProps());
         return schemaContextsBuilder.build();
     }
     
+    private boolean containsDataSources(final Map<String, Map<String, 
DataSourceParameter>> schemaDataSources) {
+        return !schemaDataSources.isEmpty() && 
!schemaDataSources.values().iterator().next().isEmpty();
+    }
+    
+    private static DatabaseType getDatabaseType(final Map<String, Map<String, 
DataSourceParameter>> schemaDataSources) {
+        String databaseTypeName = 
JDBCDriverURLRecognizerEngine.getJDBCDriverURLRecognizer(schemaDataSources.values().iterator().next().values().iterator().next().getUrl()).getDatabaseType();
+        return DatabaseTypes.getActualDatabaseType(databaseTypeName);
+    }
+    
+    private static Map<String, Map<String, DataSource>> 
createDataSourcesMap(final Map<String, Map<String, DataSourceParameter>> 
schemaDataSources) {
+        return 
schemaDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, 
entry -> createDataSources(entry.getValue()), (oldValue, currentValue) -> 
oldValue, LinkedHashMap::new));
+    }
+    
+    private static Map<String, DataSource> createDataSources(final Map<String, 
DataSourceParameter> dataSourceParameters) {
+        Map<String, DataSource> result = new 
LinkedHashMap<>(dataSourceParameters.size(), 1);
+        for (Entry<String, DataSourceParameter> entry : 
dataSourceParameters.entrySet()) {
+            result.put(entry.getKey(), 
JDBCRawBackendDataSourceFactory.getInstance().build(entry.getKey(), 
entry.getValue()));
+        }
+        return result;
+    }
+    
     private TransactionContexts createTransactionContexts(final SchemaContexts 
schemaContexts) {
         Map<String, ShardingTransactionManagerEngine> 
transactionManagerEngines = new 
HashMap<>(schemaContexts.getSchemaContexts().size(), 1);
         for (Entry<String, SchemaContext> entry : 
schemaContexts.getSchemaContexts().entrySet()) {

Reply via email to