View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.config.database.impl;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
22  import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
23  import org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfiguration;
24  import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
25  import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
26  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
27  import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
28  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
29  import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator;
30  import org.apache.shardingsphere.infra.util.close.DataSourcesCloser;
31  
32  import javax.sql.DataSource;
33  import java.util.Collection;
34  import java.util.LinkedHashMap;
35  import java.util.Map;
36  import java.util.Map.Entry;
37  import java.util.stream.Collectors;
38  
39  /**
40   * Data source generated database configuration.
41   */
42  @Getter
43  public final class DataSourceGeneratedDatabaseConfiguration implements DatabaseConfiguration {
44      
45      private final Collection<RuleConfiguration> ruleConfigurations;
46      
47      private final Map<String, StorageUnit> storageUnits;
48      
49      private final Map<StorageNode, DataSource> dataSources;
50      
51      public DataSourceGeneratedDatabaseConfiguration(final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigs) {
52          ruleConfigurations = ruleConfigs;
53          Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = dataSourceConfigs.entrySet().stream()
54                  .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
55          Map<String, StorageNode> storageUnitNodeMap = StorageUnitNodeMapCreator.create(dataSourcePoolPropertiesMap);
56          Map<StorageNode, DataSource> storageNodeDataSources = getStorageNodeDataSourceMap(dataSourcePoolPropertiesMap, storageUnitNodeMap);
57          storageUnits = new LinkedHashMap<>(dataSourceConfigs.size(), 1F);
58          for (Entry<String, DataSourceConfiguration> entry : dataSourceConfigs.entrySet()) {
59              String storageUnitName = entry.getKey();
60              StorageNode storageNode = storageUnitNodeMap.get(storageUnitName);
61              DataSource dataSource = storageNodeDataSources.get(storageNode);
62              StorageUnit storageUnit = new StorageUnit(storageNode, dataSourcePoolPropertiesMap.get(storageUnitName), dataSource);
63              storageUnits.put(storageUnitName, storageUnit);
64          }
65          dataSources = storageNodeDataSources;
66      }
67      
68      private Map<StorageNode, DataSource> getStorageNodeDataSourceMap(final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageUnitNodeMap) {
69          Map<StorageNode, DataSource> result = new LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
70          try {
71              for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) {
72                  result.computeIfAbsent(entry.getValue(), key -> DataSourcePoolCreator.create(entry.getKey(), dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values()));
73              }
74              // CHECKSTYLE:OFF
75          } catch (final Exception ex) {
76              // CHECKSTYLE:ON
77              DataSourcesCloser.close(result.values());
78              throw ex;
79          }
80          return result;
81      }
82  }