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.yaml.config.swapper.resource;
19  
20  import com.google.common.base.Preconditions;
21  import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
22  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
23  import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
24  
25  import javax.sql.DataSource;
26  import java.util.HashMap;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  import java.util.Map.Entry;
30  import java.util.stream.Collectors;
31  
32  /**
33   * YAML data source configuration swapper.
34   */
35  public final class YamlDataSourceConfigurationSwapper {
36      
37      private static final String DATA_SOURCE_CLASS_NAME_KEY = "dataSourceClassName";
38      
39      private static final String CUSTOM_POOL_PROPS_KEY = "customPoolProps";
40      
41      /**
42       * Swap to data sources from YAML data sources.
43       *
44       * @param yamlDataSources YAML data sources map
45       * @return data sources
46       */
47      public Map<String, DataSource> swapToDataSources(final Map<String, Map<String, Object>> yamlDataSources) {
48          return swapToDataSources(yamlDataSources, true);
49      }
50      
51      /**
52       * Swap to data sources from YAML data sources.
53       *
54       * @param yamlDataSources YAML data sources map
55       * @param cacheEnabled cache enabled
56       * @return data sources
57       */
58      public Map<String, DataSource> swapToDataSources(final Map<String, Map<String, Object>> yamlDataSources, final boolean cacheEnabled) {
59          return DataSourcePoolCreator.create(yamlDataSources.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> swapToDataSourcePoolProperties(entry.getValue()))), cacheEnabled);
60      }
61      
62      /**
63       * Get data source pool properties.
64       *
65       * @param yamlRootConfig YAML root configuration
66       * @return data source name to data source pool properties map
67       */
68      public Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(final YamlRootConfiguration yamlRootConfig) {
69          Map<String, Map<String, Object>> yamlDataSourceConfigs = yamlRootConfig.getDataSources();
70          Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(yamlDataSourceConfigs.size(), 1F);
71          yamlDataSourceConfigs.forEach((key, value) -> result.put(key, swapToDataSourcePoolProperties(value)));
72          return result;
73      }
74      
75      /**
76       * Swap to data source pool properties.
77       * 
78       * @param yamlConfig YAML configurations
79       * @return data source pool properties
80       */
81      public DataSourcePoolProperties swapToDataSourcePoolProperties(final Map<String, Object> yamlConfig) {
82          Preconditions.checkState(yamlConfig.containsKey(DATA_SOURCE_CLASS_NAME_KEY), "%s can not be null.", DATA_SOURCE_CLASS_NAME_KEY);
83          return new DataSourcePoolProperties(yamlConfig.get(DATA_SOURCE_CLASS_NAME_KEY).toString(), getProperties(yamlConfig));
84      }
85      
86      @SuppressWarnings({"rawtypes", "unchecked"})
87      private Map<String, Object> getProperties(final Map<String, Object> yamlConfig) {
88          Map<String, Object> result = new HashMap<>(yamlConfig);
89          if ("com.zaxxer.hikari.HikariDataSource".equals(result.get(DATA_SOURCE_CLASS_NAME_KEY).toString())) {
90              result.remove(DATA_SOURCE_CLASS_NAME_KEY);
91          }
92          if (null != yamlConfig.get(CUSTOM_POOL_PROPS_KEY)) {
93              result.putAll((Map) yamlConfig.get(CUSTOM_POOL_PROPS_KEY));
94          }
95          result.remove(CUSTOM_POOL_PROPS_KEY);
96          return result;
97      }
98      
99      /**
100      * Swap to map from data source properties.
101      * 
102      * @param props data source pool properties
103      * @return data source map
104      */
105     public Map<String, Object> swapToMap(final DataSourcePoolProperties props) {
106         Map<String, Object> result = new HashMap<>(props.getAllStandardProperties());
107         result.put(DATA_SOURCE_CLASS_NAME_KEY, props.getPoolClassName());
108         return result;
109     }
110 }