diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java index 4abcc29d01a6a..c76ec08d82cf1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java @@ -29,7 +29,6 @@ import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeProperties; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; @@ -51,16 +50,46 @@ public final class StorageResourceCreator { */ public static StorageResource createStorageResource(final Map propsMap) { Map storageNodes = new LinkedHashMap<>(); - Map storageUnitNodeMappers = new LinkedHashMap<>(); + Map mappers = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { - StorageNodeProperties storageNodeProps = getStorageNodeProperties(entry.getKey(), entry.getValue()); - StorageNode storageNode = new StorageNode(storageNodeProps.getName()); + StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue())); if (!storageNodes.containsKey(storageNode)) { storageNodes.put(storageNode, DataSourcePoolCreator.create(entry.getKey(), entry.getValue(), true, storageNodes.values())); } - appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProps, entry.getKey(), entry.getValue()); + appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); } - return new StorageResource(storageNodes, storageUnitNodeMappers); + return new StorageResource(storageNodes, mappers); + } + + private static String getStorageNodeName(final String dataSourceName, final DataSourcePoolProperties storageNodeProps) { + Map standardProps = storageNodeProps.getConnectionPropertySynonyms().getStandardProperties(); + String url = standardProps.get("url").toString(); + String username = standardProps.get("username").toString(); + DatabaseType databaseType = DatabaseTypeFactory.get(url); + try { + JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); + DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); + return dialectDatabaseMetaData.isInstanceConnectionAvailable() ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName; + } catch (final UnrecognizedDatabaseURLException ex) { + return dataSourceName; + } + } + + private static String generateStorageNodeName(final String hostname, final int port, final String username) { + return String.format("%s_%s_%s", hostname, port, username); + } + + private static void appendStorageUnitNodeMapper(final Map storageUnitNodeMappers, final StorageNode storageNode, + final String storageUnitName, final DataSourcePoolProperties props) { + String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); + storageUnitNodeMappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, DatabaseTypeFactory.get(url), storageUnitName, url)); + } + + private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final DatabaseType databaseType, final String unitName, final String url) { + DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); + return dialectDatabaseMetaData.isInstanceConnectionAvailable() + ? new StorageUnitNodeMapper(unitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url) + : new StorageUnitNodeMapper(unitName, storageNode, url); } /** @@ -71,55 +100,18 @@ public static StorageResource createStorageResource(final Map propsMap) { Map storageNodes = new LinkedHashMap<>(); - Map storageUnitNodeMappers = new LinkedHashMap<>(); + Map mappers = new LinkedHashMap<>(); Map newPropsMap = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { - StorageNodeProperties storageNodeProps = getStorageNodeProperties(entry.getKey(), entry.getValue()); - StorageNode storageNode = new StorageNode(storageNodeProps.getName()); + StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue())); if (storageNodes.containsKey(storageNode)) { - appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProps, entry.getKey(), entry.getValue()); + appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); continue; } storageNodes.put(storageNode, null); - appendStorageUnitNodeMapper(storageUnitNodeMappers, storageNodeProps, entry.getKey(), entry.getValue()); - newPropsMap.put(storageNodeProps.getName(), entry.getValue()); - } - return new StorageResource(storageNodes, storageUnitNodeMappers, newPropsMap); - } - - private static void appendStorageUnitNodeMapper(final Map storageUnitNodeMappers, final StorageNodeProperties storageNodeProps, - final String unitName, final DataSourcePoolProperties props) { - String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString(); - storageUnitNodeMappers.put(unitName, getStorageUnitNodeMapper(storageNodeProps, unitName, url)); - } - - private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNodeProperties storageNodeProps, final String unitName, final String url) { - DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(storageNodeProps.getDatabaseType()).getDialectDatabaseMetaData(); - return dialectDatabaseMetaData.isInstanceConnectionAvailable() - ? new StorageUnitNodeMapper(unitName, new StorageNode(storageNodeProps.getName()), storageNodeProps.getCatalog(), url) - : new StorageUnitNodeMapper(unitName, new StorageNode(storageNodeProps.getName()), url); - } - - private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final DataSourcePoolProperties storageNodeProps) { - Map standardProps = storageNodeProps.getConnectionPropertySynonyms().getStandardProperties(); - String url = standardProps.get("url").toString(); - String username = standardProps.get("username").toString(); - DatabaseType databaseType = DatabaseTypeFactory.get(url); - return getStorageNodeProperties(dataSourceName, url, username, databaseType); - } - - private static StorageNodeProperties getStorageNodeProperties(final String dataSourceName, final String url, final String username, final DatabaseType databaseType) { - try { - JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); - DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData(); - String nodeName = dialectDatabaseMetaData.isInstanceConnectionAvailable() ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName; - return new StorageNodeProperties(nodeName, databaseType, jdbcUrl.getDatabase()); - } catch (final UnrecognizedDatabaseURLException ex) { - return new StorageNodeProperties(dataSourceName, databaseType, null); + appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue()); + newPropsMap.put(storageNode.getName(), entry.getValue()); } - } - - private static String generateStorageNodeName(final String hostname, final int port, final String username) { - return String.format("%s_%s_%s", hostname, port, username); + return new StorageResource(storageNodes, mappers, newPropsMap); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeProperties.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeProperties.java deleted file mode 100644 index 93aa06f6ca771..0000000000000 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeProperties.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.infra.metadata.database.resource.node; - -import com.google.common.base.Objects; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; - -/** - * Storage node properties. - */ -@RequiredArgsConstructor -@Getter -public final class StorageNodeProperties { - - private final String name; - - private final DatabaseType databaseType; - - private final String catalog; - - @Override - public boolean equals(final Object obj) { - if (obj instanceof StorageNodeProperties) { - StorageNodeProperties storageNodeProps = (StorageNodeProperties) obj; - return storageNodeProps.name.equals(name); - } - return false; - } - - @Override - public int hashCode() { - return Objects.hashCode(name.toUpperCase()); - } -}