1
1
/*
2
- * Copyright 2012-2020 the original author or authors.
2
+ * Copyright 2012-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .boot .autoconfigure .r2dbc ;
18
18
19
19
import java .util .function .Consumer ;
20
- import java .util .function .Predicate ;
21
20
import java .util .function .Supplier ;
22
21
23
22
import io .r2dbc .spi .ConnectionFactories ;
24
23
import io .r2dbc .spi .ConnectionFactory ;
25
24
import io .r2dbc .spi .ConnectionFactoryOptions ;
26
25
import io .r2dbc .spi .ConnectionFactoryOptions .Builder ;
27
- import io .r2dbc .spi .Option ;
28
-
29
- import org .springframework .beans .factory .BeanCreationException ;
30
- import org .springframework .util .StringUtils ;
31
26
32
27
/**
33
28
* Builder for {@link ConnectionFactory}.
36
31
* @author Tadaya Tsuyukubo
37
32
* @author Stephane Nicoll
38
33
* @since 2.3.0
34
+ * @deprecated since 2.5.0 in favor of
35
+ * {@link org.springframework.boot.r2dbc.ConnectionFactoryBuilder}
39
36
*/
37
+ @ Deprecated
40
38
public final class ConnectionFactoryBuilder {
41
39
42
40
private final ConnectionFactoryOptions .Builder optionsBuilder ;
@@ -59,7 +57,7 @@ private ConnectionFactoryBuilder(ConnectionFactoryOptions.Builder optionsBuilder
59
57
public static ConnectionFactoryBuilder of (R2dbcProperties properties ,
60
58
Supplier <EmbeddedDatabaseConnection > embeddedDatabaseConnection ) {
61
59
return new ConnectionFactoryBuilder (
62
- new ConnectionFactoryOptionsInitializer ().initializeOptions (properties , embeddedDatabaseConnection ));
60
+ new ConnectionFactoryOptionsInitializer ().initialize (properties , adapt ( embeddedDatabaseConnection ) ));
63
61
}
64
62
65
63
/**
@@ -133,127 +131,13 @@ public ConnectionFactoryOptions buildOptions() {
133
131
return this .optionsBuilder .build ();
134
132
}
135
133
136
- static class ConnectionFactoryOptionsInitializer {
137
-
138
- /**
139
- * Initialize a {@link io.r2dbc.spi.ConnectionFactoryOptions.Builder
140
- * ConnectionFactoryOptions.Builder} using the specified properties.
141
- * @param properties the properties to use to initialize the builder
142
- * @param embeddedDatabaseConnection the embedded connection to use as a fallback
143
- * @return an initialized builder
144
- * @throws ConnectionFactoryBeanCreationException if no suitable connection could
145
- * be determined
146
- */
147
- ConnectionFactoryOptions .Builder initializeOptions (R2dbcProperties properties ,
148
- Supplier <EmbeddedDatabaseConnection > embeddedDatabaseConnection ) {
149
- if (StringUtils .hasText (properties .getUrl ())) {
150
- return initializeRegularOptions (properties );
151
- }
152
- EmbeddedDatabaseConnection embeddedConnection = embeddedDatabaseConnection .get ();
153
- if (embeddedConnection != EmbeddedDatabaseConnection .NONE ) {
154
- return initializeEmbeddedOptions (properties , embeddedConnection );
155
- }
156
- throw connectionFactoryBeanCreationException ("Failed to determine a suitable R2DBC Connection URL" ,
157
- properties , embeddedConnection );
158
- }
159
-
160
- private ConnectionFactoryOptions .Builder initializeRegularOptions (R2dbcProperties properties ) {
161
- ConnectionFactoryOptions urlOptions = ConnectionFactoryOptions .parse (properties .getUrl ());
162
- Builder optionsBuilder = urlOptions .mutate ();
163
- configureIf (optionsBuilder , urlOptions , ConnectionFactoryOptions .USER , properties ::getUsername ,
164
- StringUtils ::hasText );
165
- configureIf (optionsBuilder , urlOptions , ConnectionFactoryOptions .PASSWORD , properties ::getPassword ,
166
- StringUtils ::hasText );
167
- configureIf (optionsBuilder , urlOptions , ConnectionFactoryOptions .DATABASE ,
168
- () -> determineDatabaseName (properties ), StringUtils ::hasText );
169
- if (properties .getProperties () != null ) {
170
- properties .getProperties ().forEach ((key , value ) -> optionsBuilder .option (Option .valueOf (key ), value ));
171
- }
172
- return optionsBuilder ;
173
- }
174
-
175
- private ConnectionFactoryOptions .Builder initializeEmbeddedOptions (R2dbcProperties properties ,
176
- EmbeddedDatabaseConnection embeddedDatabaseConnection ) {
177
- String url = embeddedDatabaseConnection .getUrl (determineEmbeddedDatabaseName (properties ));
178
- if (url == null ) {
179
- throw connectionFactoryBeanCreationException ("Failed to determine a suitable R2DBC Connection URL" ,
180
- properties , embeddedDatabaseConnection );
181
- }
182
- Builder builder = ConnectionFactoryOptions .parse (url ).mutate ();
183
- String username = determineEmbeddedUsername (properties );
184
- if (StringUtils .hasText (username )) {
185
- builder .option (ConnectionFactoryOptions .USER , username );
186
- }
187
- if (StringUtils .hasText (properties .getPassword ())) {
188
- builder .option (ConnectionFactoryOptions .PASSWORD , properties .getPassword ());
189
- }
190
- return builder ;
191
- }
192
-
193
- private String determineDatabaseName (R2dbcProperties properties ) {
194
- if (properties .isGenerateUniqueName ()) {
195
- return properties .determineUniqueName ();
196
- }
197
- if (StringUtils .hasLength (properties .getName ())) {
198
- return properties .getName ();
199
- }
200
- return null ;
201
- }
202
-
203
- private String determineEmbeddedDatabaseName (R2dbcProperties properties ) {
204
- String databaseName = determineDatabaseName (properties );
205
- return (databaseName != null ) ? databaseName : "testdb" ;
206
- }
207
-
208
- private String determineEmbeddedUsername (R2dbcProperties properties ) {
209
- String username = ifHasText (properties .getUsername ());
210
- return (username != null ) ? username : "sa" ;
211
- }
212
-
213
- private <T extends CharSequence > void configureIf (Builder optionsBuilder ,
214
- ConnectionFactoryOptions originalOptions , Option <T > option , Supplier <T > valueSupplier ,
215
- Predicate <T > setIf ) {
216
- if (originalOptions .hasOption (option )) {
217
- return ;
218
- }
219
- T value = valueSupplier .get ();
220
- if (setIf .test (value )) {
221
- optionsBuilder .option (option , value );
222
- }
223
- }
224
-
225
- private ConnectionFactoryBeanCreationException connectionFactoryBeanCreationException (String message ,
226
- R2dbcProperties properties , EmbeddedDatabaseConnection embeddedDatabaseConnection ) {
227
- return new ConnectionFactoryBeanCreationException (message , properties , embeddedDatabaseConnection );
228
- }
229
-
230
- private String ifHasText (String candidate ) {
231
- return (StringUtils .hasText (candidate )) ? candidate : null ;
232
- }
233
-
234
- }
235
-
236
- static class ConnectionFactoryBeanCreationException extends BeanCreationException {
237
-
238
- private final R2dbcProperties properties ;
239
-
240
- private final EmbeddedDatabaseConnection embeddedDatabaseConnection ;
241
-
242
- ConnectionFactoryBeanCreationException (String message , R2dbcProperties properties ,
243
- EmbeddedDatabaseConnection embeddedDatabaseConnection ) {
244
- super (message );
245
- this .properties = properties ;
246
- this .embeddedDatabaseConnection = embeddedDatabaseConnection ;
247
- }
248
-
249
- EmbeddedDatabaseConnection getEmbeddedDatabaseConnection () {
250
- return this .embeddedDatabaseConnection ;
251
- }
252
-
253
- R2dbcProperties getProperties () {
254
- return this .properties ;
255
- }
256
-
134
+ private static Supplier <org .springframework .boot .r2dbc .EmbeddedDatabaseConnection > adapt (
135
+ Supplier <EmbeddedDatabaseConnection > embeddedDatabaseConnection ) {
136
+ return () -> {
137
+ EmbeddedDatabaseConnection connection = embeddedDatabaseConnection .get ();
138
+ return (connection != null )
139
+ ? org .springframework .boot .r2dbc .EmbeddedDatabaseConnection .valueOf (connection .name ()) : null ;
140
+ };
257
141
}
258
142
259
143
}
0 commit comments