forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
PlatformPlaceholderDatabaseDriverResolver.java
140 lines (123 loc) · 4.85 KB
/
PlatformPlaceholderDatabaseDriverResolver.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
/*
* 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.init;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import javax.sql.DataSource;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Utility class that can resolve placeholder text with the actual {@link DatabaseDriver}
* platform.
* <p>
* By default, the name of the platform is the {@link DatabaseDriver#getId ID of the
* driver}. This mapping can be customized by
* {@link #withDriverPlatform(DatabaseDriver, String)} registering custom
* {@code DatabaseDriver} to platform mappings.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 2.6.0
*/
public class PlatformPlaceholderDatabaseDriverResolver {
private final String placeholder;
private final Map<DatabaseDriver, String> driverMappings;
/**
* Creates a new resolver that will use the default {@code "@@platform@@"}
* placeholder.
*/
public PlatformPlaceholderDatabaseDriverResolver() {
this("@@platform@@");
}
/**
* Creates a new resolver that will use the given {@code placeholder}.
* @param placeholder the placeholder to use
*/
public PlatformPlaceholderDatabaseDriverResolver(String placeholder) {
this(placeholder, Collections.emptyMap());
}
private PlatformPlaceholderDatabaseDriverResolver(String placeholder, Map<DatabaseDriver, String> driverMappings) {
this.placeholder = placeholder;
this.driverMappings = driverMappings;
}
/**
* Creates a new {@link PlatformPlaceholderDatabaseDriverResolver} that will map the
* given {@code driver} to the given {@code platform}.
* @param driver the driver
* @param platform the platform
* @return the new resolver
*/
public PlatformPlaceholderDatabaseDriverResolver withDriverPlatform(DatabaseDriver driver, String platform) {
Map<DatabaseDriver, String> driverMappings = new LinkedHashMap<>(this.driverMappings);
driverMappings.put(driver, platform);
return new PlatformPlaceholderDatabaseDriverResolver(this.placeholder, driverMappings);
}
/**
* Resolves the placeholders in the given {@code values}, replacing them with the
* platform derived from the {@link DatabaseDriver} of the given {@code dataSource}.
* @param dataSource the DataSource from which the {@link DatabaseDriver} is derived
* @param values the values in which placeholders are resolved
* @return the values with their placeholders resolved
* @see DatabaseDriver#fromDataSource(DataSource)
*/
public List<String> resolveAll(DataSource dataSource, String... values) {
Assert.notNull(dataSource, "DataSource must not be null");
return resolveAll(() -> determinePlatform(dataSource), values);
}
/**
* Resolves the placeholders in the given {@code values}, replacing them with the
* given platform
* @param platform the platform to use
* @param values the values in which placeholders are resolved
* @return the values with their placeholders resolved
* @since 2.6.2
*/
public List<String> resolveAll(String platform, String... values) {
Assert.notNull(platform, "Platform must not be null");
return resolveAll(() -> platform, values);
}
private List<String> resolveAll(Supplier<String> platformProvider, String... values) {
if (ObjectUtils.isEmpty(values)) {
return Collections.emptyList();
}
List<String> resolved = new ArrayList<>(values.length);
String platform = null;
for (String value : values) {
if (StringUtils.hasLength(value)) {
if (value.contains(this.placeholder)) {
platform = (platform != null) ? platform : platformProvider.get();
value = value.replace(this.placeholder, platform);
}
}
resolved.add(value);
}
return Collections.unmodifiableList(resolved);
}
private String determinePlatform(DataSource dataSource) {
DatabaseDriver databaseDriver = getDatabaseDriver(dataSource);
Assert.state(databaseDriver != DatabaseDriver.UNKNOWN, "Unable to detect database type");
return this.driverMappings.getOrDefault(databaseDriver, databaseDriver.getId());
}
DatabaseDriver getDatabaseDriver(DataSource dataSource) {
return DatabaseDriver.fromDataSource(dataSource);
}
}