-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
DataSourceBuilder.java
258 lines (214 loc) · 9.32 KB
/
DataSourceBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.boot.jdbc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.sql.DataSource;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertyNameAliases;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.util.ClassUtils;
/**
* Convenience class for building a {@link DataSource} with common implementations and
* properties. If HikariCP, Tomcat, Commons DBCP or Oracle UCP are on the classpath one of
* them will be selected (in that order with Hikari first). In the interest of a uniform
* interface, and so that there can be a fallback to an embedded database if one can be
* detected on the classpath, only a small set of common configuration properties are
* supported. To inject additional properties into the result you can downcast it, or use
* {@code @ConfigurationProperties}.
*
* @param <T> type of DataSource produced by the builder
* @author Dave Syer
* @author Madhura Bhave
* @author Fabio Grassi
* @since 2.0.0
*/
public final class DataSourceBuilder<T extends DataSource> {
private Class<? extends DataSource> type;
private final DataSourceSettingsResolver settingsResolver;
private final Map<String, String> properties = new HashMap<>();
public static DataSourceBuilder<?> create() {
return new DataSourceBuilder<>(null);
}
public static DataSourceBuilder<?> create(ClassLoader classLoader) {
return new DataSourceBuilder<>(classLoader);
}
private DataSourceBuilder(ClassLoader classLoader) {
this.settingsResolver = new DataSourceSettingsResolver(classLoader);
}
@SuppressWarnings("unchecked")
public T build() {
Class<? extends DataSource> type = getType();
DataSource result = BeanUtils.instantiateClass(type);
maybeGetDriverClassName();
bind(result);
return (T) result;
}
private void maybeGetDriverClassName() {
if (!this.properties.containsKey("driverClassName") && this.properties.containsKey("url")) {
String url = this.properties.get("url");
String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
this.properties.put("driverClassName", driverClass);
}
}
private void bind(DataSource result) {
ConfigurationPropertySource source = new MapConfigurationPropertySource(this.properties);
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
this.settingsResolver.registerAliases(result, aliases);
Binder binder = new Binder(source.withAliases(aliases));
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
}
@SuppressWarnings("unchecked")
public <D extends DataSource> DataSourceBuilder<D> type(Class<D> type) {
this.type = type;
return (DataSourceBuilder<D>) this;
}
public DataSourceBuilder<T> url(String url) {
this.properties.put("url", url);
return this;
}
public DataSourceBuilder<T> driverClassName(String driverClassName) {
this.properties.put("driverClassName", driverClassName);
return this;
}
public DataSourceBuilder<T> username(String username) {
this.properties.put("username", username);
return this;
}
public DataSourceBuilder<T> password(String password) {
this.properties.put("password", password);
return this;
}
public static Class<? extends DataSource> findType(ClassLoader classLoader) {
DataSourceSettings preferredDataSourceSettings = new DataSourceSettingsResolver(classLoader)
.getPreferredDataSourceSettings();
return (preferredDataSourceSettings != null) ? preferredDataSourceSettings.getType() : null;
}
private Class<? extends DataSource> getType() {
if (this.type != null) {
return this.type;
}
DataSourceSettings preferredDataSourceSettings = this.settingsResolver.getPreferredDataSourceSettings();
if (preferredDataSourceSettings != null) {
return preferredDataSourceSettings.getType();
}
throw new IllegalStateException("No supported DataSource type found");
}
private static class DataSourceSettings {
private final Class<? extends DataSource> type;
private final Consumer<ConfigurationPropertyNameAliases> aliasesCustomizer;
DataSourceSettings(Class<? extends DataSource> type,
Consumer<ConfigurationPropertyNameAliases> aliasesCustomizer) {
this.type = type;
this.aliasesCustomizer = aliasesCustomizer;
}
DataSourceSettings(Class<? extends DataSource> type) {
this(type, (aliases) -> {
});
}
Class<? extends DataSource> getType() {
return this.type;
}
void registerAliases(DataSource candidate, ConfigurationPropertyNameAliases aliases) {
if (this.type != null && this.type.isInstance(candidate)) {
this.aliasesCustomizer.accept(aliases);
}
}
}
private static class OracleDataSourceSettings extends DataSourceSettings {
OracleDataSourceSettings(Class<? extends DataSource> type) {
super(type, (aliases) -> aliases.addAliases("username", "user"));
}
@Override
public Class<? extends DataSource> getType() {
return null; // Base interface
}
}
private static class DataSourceSettingsResolver {
private final DataSourceSettings preferredDataSourceSettings;
private final List<DataSourceSettings> allDataSourceSettings;
DataSourceSettingsResolver(ClassLoader classLoader) {
List<DataSourceSettings> supportedProviders = resolveAvailableDataSourceSettings(classLoader);
this.preferredDataSourceSettings = (!supportedProviders.isEmpty()) ? supportedProviders.get(0) : null;
this.allDataSourceSettings = new ArrayList<>(supportedProviders);
addIfAvailable(this.allDataSourceSettings,
create(classLoader, "org.springframework.jdbc.datasource.SimpleDriverDataSource",
(type) -> new DataSourceSettings(type,
(aliases) -> aliases.addAliases("driver-class-name", "driver-class"))));
addIfAvailable(this.allDataSourceSettings,
create(classLoader, "oracle.jdbc.datasource.OracleDataSource", OracleDataSourceSettings::new));
addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.h2.jdbcx.JdbcDataSource",
(type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user"))));
addIfAvailable(this.allDataSourceSettings, create(classLoader, "org.postgresql.ds.PGSimpleDataSource",
(type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("username", "user"))));
}
private static List<DataSourceSettings> resolveAvailableDataSourceSettings(ClassLoader classLoader) {
List<DataSourceSettings> providers = new ArrayList<>();
addIfAvailable(providers, create(classLoader, "com.zaxxer.hikari.HikariDataSource",
(type) -> new DataSourceSettings(type, (aliases) -> aliases.addAliases("url", "jdbc-url"))));
addIfAvailable(providers,
create(classLoader, "org.apache.tomcat.jdbc.pool.DataSource", DataSourceSettings::new));
addIfAvailable(providers,
create(classLoader, "org.apache.commons.dbcp2.BasicDataSource", DataSourceSettings::new));
addIfAvailable(providers, create(classLoader, "oracle.ucp.jdbc.PoolDataSourceImpl", (type) -> {
// Unfortunately Oracle UCP has an import on the Oracle driver itself
if (ClassUtils.isPresent("oracle.jdbc.OracleConnection", classLoader)) {
return new DataSourceSettings(type, (aliases) -> {
aliases.addAliases("username", "user");
aliases.addAliases("driver-class-name", "connection-factory-class-name");
});
}
return null;
}));
return providers;
}
@SuppressWarnings("unchecked")
private static DataSourceSettings create(ClassLoader classLoader, String target,
Function<Class<? extends DataSource>, DataSourceSettings> factory) {
if (ClassUtils.isPresent(target, classLoader)) {
try {
Class<? extends DataSource> type = (Class<? extends DataSource>) ClassUtils.forName(target,
classLoader);
return factory.apply(type);
}
catch (Exception ex) {
// Ignore
}
}
return null;
}
private static void addIfAvailable(Collection<DataSourceSettings> list, DataSourceSettings dataSourceSettings) {
if (dataSourceSettings != null) {
list.add(dataSourceSettings);
}
}
DataSourceSettings getPreferredDataSourceSettings() {
return this.preferredDataSourceSettings;
}
void registerAliases(DataSource result, ConfigurationPropertyNameAliases aliases) {
this.allDataSourceSettings.forEach((settings) -> settings.registerAliases(result, aliases));
}
}
}