1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
43
44
45
46
47 public Map<String, DataSource> swapToDataSources(final Map<String, Map<String, Object>> yamlDataSources) {
48 return swapToDataSources(yamlDataSources, true);
49 }
50
51
52
53
54
55
56
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
64
65
66
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
77
78
79
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
101
102
103
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 }