-
Notifications
You must be signed in to change notification settings - Fork 177
/
PostgresqlConnectionFactoryProvider.java
348 lines (288 loc) · 13.2 KB
/
PostgresqlConnectionFactoryProvider.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright 2017 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 io.r2dbc.postgresql;
import io.netty.handler.ssl.SslContextBuilder;
import io.r2dbc.postgresql.client.DefaultHostnameVerifier;
import io.r2dbc.postgresql.client.SSLMode;
import io.r2dbc.postgresql.codec.Codecs;
import io.r2dbc.postgresql.codec.Json;
import io.r2dbc.postgresql.extension.Extension;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.postgresql.util.LogLevel;
import io.r2dbc.spi.ConnectionFactoryOptions;
import io.r2dbc.spi.ConnectionFactoryProvider;
import io.r2dbc.spi.Option;
import reactor.netty.resources.LoopResources;
import javax.net.ssl.HostnameVerifier;
import java.time.Duration;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import static io.r2dbc.spi.ConnectionFactoryOptions.CONNECT_TIMEOUT;
import static io.r2dbc.spi.ConnectionFactoryOptions.DATABASE;
import static io.r2dbc.spi.ConnectionFactoryOptions.DRIVER;
import static io.r2dbc.spi.ConnectionFactoryOptions.HOST;
import static io.r2dbc.spi.ConnectionFactoryOptions.PASSWORD;
import static io.r2dbc.spi.ConnectionFactoryOptions.PORT;
import static io.r2dbc.spi.ConnectionFactoryOptions.SSL;
import static io.r2dbc.spi.ConnectionFactoryOptions.USER;
/**
* An implementation of {@link ConnectionFactoryProvider} for creating {@link PostgresqlConnectionFactory}s.
*/
public final class PostgresqlConnectionFactoryProvider implements ConnectionFactoryProvider {
/**
* Application name.
*/
public static final Option<String> APPLICATION_NAME = Option.valueOf("applicationName");
/**
* Auto-detect extensions.
*/
public static final Option<Boolean> AUTODETECT_EXTENSIONS = Option.valueOf("autodetectExtensions");
/**
* Compatibility query mode for cursored query execution.
*
* @since 0.8.7
*/
public static final Option<Boolean> COMPATIBILITY_MODE = Option.valueOf("compatibilityMode");
/**
* Error Response Log Level.
*/
public static final Option<LogLevel> ERROR_RESPONSE_LOG_LEVEL = Option.valueOf("errorResponseLogLevel");
/**
* Extensions to register.
*
* @since 0.8.9
*/
public static final Option<Collection<Extension>> EXTENSIONS = Option.valueOf("extensions");
/**
* Fetch Size.
*/
public static final Option<Integer> FETCH_SIZE = Option.valueOf("fetchSize");
/**
* Force binary transfer.
*/
public static final Option<Boolean> FORCE_BINARY = Option.valueOf("forceBinary");
/**
* Lock timeout.
*
* @since 0.8.9
*/
public static final Option<Duration> LOCK_WAIT_TIMEOUT = ConnectionFactoryOptions.LOCK_WAIT_TIMEOUT;
/**
* Event {@link LoopResources}.
*
* @since 0.8.5
*/
public static final Option<LoopResources> LOOP_RESOURCES = Option.valueOf("loopResources");
/**
* Notice Response Log Level.
*/
public static final Option<LogLevel> NOTICE_LOG_LEVEL = Option.valueOf("noticeLogLevel");
/**
* Connection options which are applied once after the connection has been created.
*/
public static final Option<Map<String, String>> OPTIONS = Option.valueOf("options");
/**
* Driver option value.
*/
public static final String POSTGRESQL_DRIVER = "postgresql";
/**
* Legacy driver option value.
*/
public static final String LEGACY_POSTGRESQL_DRIVER = "postgres";
/**
* Configure whether {@link Codecs codecs} should prefer attached data buffers. The default is {@code false}, meaning that codecs will copy data from the input buffer into a {@code byte[]}
* or similar data structure that is enabled for garbage collection. Using attached buffers is more efficient but comes with the requirement that decoded values (such as {@link Json}) must
* be consumed to release attached buffers to avoid memory leaks.
*
* @since 0.8.5
*/
public static final Option<Boolean> PREFER_ATTACHED_BUFFERS = Option.valueOf("preferAttachedBuffers");
/**
* Determine the number of queries that are cached in each connection.
* The default is {@code -1}, meaning there's no limit. The value of {@code 0} disables the cache. Any other value specifies the cache size.
*/
public static final Option<Integer> PREPARED_STATEMENT_CACHE_QUERIES = Option.valueOf("preparedStatementCacheQueries");
/**
* Schema search path (alias for "currentSchema").
*/
public static final Option<String> SCHEMA = Option.valueOf("schema");
/**
* Schema search path.
*
* @since 0.9
*/
public static final Option<String> CURRENT_SCHEMA = Option.valueOf("currentSchema");
/**
* Unix domain socket.
*/
public static final Option<String> SOCKET = Option.valueOf("socket");
/**
* Customizer {@link Function} for {@link SslContextBuilder}.
*/
public static final Option<Function<SslContextBuilder, SslContextBuilder>> SSL_CONTEXT_BUILDER_CUSTOMIZER = Option.valueOf("sslContextBuilderCustomizer");
/**
* Path for the certificate file. Can point to either a resource within the classpath or a file.
*/
public static final Option<String> SSL_CERT = Option.valueOf("sslCert");
/**
* Class name of hostname verifier. Defaults to {@link DefaultHostnameVerifier}.
*/
public static final Option<HostnameVerifier> SSL_HOSTNAME_VERIFIER = Option.valueOf("sslHostnameVerifier");
/**
* File path for the key file. Can point to either a resource within the classpath or a file.
*/
public static final Option<String> SSL_KEY = Option.valueOf("sslKey");
/**
* Ssl mode. Default: disabled
*/
public static final Option<SSLMode> SSL_MODE = Option.valueOf("sslMode");
/**
* SSL key password
*/
public static final Option<String> SSL_PASSWORD = Option.valueOf("sslPassword");
/**
* File path of the SSL root certificate. Can point to either a resource within the classpath or a file.
*/
public static final Option<String> SSL_ROOT_CERT = Option.valueOf("sslRootCert");
/**
* Statement timeout.
*
* @since 0.8.9
*/
public static final Option<Duration> STATEMENT_TIMEOUT = ConnectionFactoryOptions.STATEMENT_TIMEOUT;
/**
* Enable TCP KeepAlive.
*
* @since 0.8.4
*/
public static final Option<Boolean> TCP_KEEPALIVE = Option.valueOf("tcpKeepAlive");
/**
* Enable TCP NoDelay.
*
* @since 0.8.4
*/
public static final Option<Boolean> TCP_NODELAY = Option.valueOf("tcpNoDelay");
/**
* Returns a new {@link PostgresqlConnectionConfiguration.Builder} configured with the given {@link ConnectionFactoryOptions}.
*
* @param connectionFactoryOptions {@link ConnectionFactoryOptions} used to initialize the {@link PostgresqlConnectionConfiguration.Builder}.
* @return a {@link PostgresqlConnectionConfiguration.Builder}
* @since 0.9
*/
public static PostgresqlConnectionConfiguration.Builder builder(ConnectionFactoryOptions connectionFactoryOptions) {
return fromConnectionFactoryOptions(connectionFactoryOptions);
}
@Override
public PostgresqlConnectionFactory create(ConnectionFactoryOptions connectionFactoryOptions) {
return new PostgresqlConnectionFactory(builder(connectionFactoryOptions).build());
}
@Override
public String getDriver() {
return POSTGRESQL_DRIVER;
}
@Override
public boolean supports(ConnectionFactoryOptions connectionFactoryOptions) {
Assert.requireNonNull(connectionFactoryOptions, "connectionFactoryOptions must not be null");
String driver = "" + connectionFactoryOptions.getValue(DRIVER);
return driver != null && (driver.equals(POSTGRESQL_DRIVER) || driver.equals(LEGACY_POSTGRESQL_DRIVER));
}
/**
* Configure the builder with the given {@link ConnectionFactoryOptions}.
*
* @param options {@link ConnectionFactoryOptions}
* @return this {@link PostgresqlConnectionConfiguration.Builder}
* @throws IllegalArgumentException if {@code options} is {@code null}
*/
private static PostgresqlConnectionConfiguration.Builder fromConnectionFactoryOptions(ConnectionFactoryOptions options) {
Assert.requireNonNull(options, "connectionFactoryOptions must not be null");
PostgresqlConnectionConfiguration.Builder builder = PostgresqlConnectionConfiguration.builder();
OptionMapper mapper = OptionMapper.create(options);
mapper.fromTyped(APPLICATION_NAME).to(builder::applicationName);
mapper.from(AUTODETECT_EXTENSIONS).map(OptionMapper::toBoolean).to(builder::autodetectExtensions);
mapper.from(COMPATIBILITY_MODE).map(OptionMapper::toBoolean).to(builder::compatibilityMode);
mapper.from(CONNECT_TIMEOUT).map(OptionMapper::toDuration).to(builder::connectTimeout);
mapper.fromTyped(CURRENT_SCHEMA).to(builder::schema).otherwise(() -> mapper.fromTyped(SCHEMA).to(builder::schema));
mapper.fromTyped(DATABASE).to(builder::database);
mapper.from(ERROR_RESPONSE_LOG_LEVEL).map(it -> OptionMapper.toEnum(it, LogLevel.class)).to(builder::errorResponseLogLevel);
mapper.fromTyped(EXTENSIONS).to(extensions -> extensions.forEach(builder::extendWith));
mapper.from(FETCH_SIZE).map(OptionMapper::toInteger).to(builder::fetchSize);
mapper.from(FORCE_BINARY).map(OptionMapper::toBoolean).to(builder::forceBinary);
mapper.from(LOCK_WAIT_TIMEOUT).map(OptionMapper::toDuration).to(builder::lockWaitTimeout);
mapper.fromTyped(LOOP_RESOURCES).to(builder::loopResources);
mapper.from(NOTICE_LOG_LEVEL).map(it -> OptionMapper.toEnum(it, LogLevel.class)).to(builder::noticeLogLevel);
mapper.from(OPTIONS).map(PostgresqlConnectionFactoryProvider::convertToMap).to(builder::options);
mapper.fromTyped(PASSWORD).to(builder::password);
mapper.from(PORT).map(OptionMapper::toInteger).to(builder::port);
mapper.from(PREFER_ATTACHED_BUFFERS).map(OptionMapper::toBoolean).to(builder::preferAttachedBuffers);
mapper.from(PREPARED_STATEMENT_CACHE_QUERIES).map(OptionMapper::toInteger).to(builder::preparedStatementCacheQueries);
mapper.fromTyped(SOCKET).to(builder::socket).otherwise(() -> {
builder.host("" + options.getRequiredValue(HOST));
setupSsl(builder, mapper);
});
mapper.from(STATEMENT_TIMEOUT).map(OptionMapper::toDuration).to(builder::statementTimeout);
mapper.from(TCP_KEEPALIVE).map(OptionMapper::toBoolean).to(builder::tcpKeepAlive);
mapper.from(TCP_NODELAY).map(OptionMapper::toBoolean).to(builder::tcpNoDelay);
builder.username("" + options.getRequiredValue(USER));
return builder;
}
private static void setupSsl(PostgresqlConnectionConfiguration.Builder builder, OptionMapper mapper) {
mapper.from(SSL).map(OptionMapper::toBoolean).to(enableSsl -> {
if (enableSsl) {
builder.enableSsl();
}
});
mapper.from(SSL_MODE).map(it -> {
if (it instanceof String) {
return SSLMode.fromValue(it.toString());
}
return (SSLMode) it;
}).to(builder::sslMode);
mapper.fromTyped(SSL_CERT).to(builder::sslCert);
mapper.fromTyped(SSL_CONTEXT_BUILDER_CUSTOMIZER).to(builder::sslContextBuilderCustomizer);
mapper.fromTyped(SSL_KEY).to(builder::sslKey);
mapper.fromTyped(SSL_ROOT_CERT).to(builder::sslRootCert);
mapper.fromTyped(SSL_PASSWORD).to(builder::sslPassword);
mapper.from(SSL_HOSTNAME_VERIFIER).map(it -> {
if (it instanceof String) {
try {
Class<?> verifierClass = Class.forName((String) it);
Object verifier = verifierClass.getConstructor().newInstance();
return (HostnameVerifier) verifier;
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Cannot instantiate " + it, e);
}
}
return (HostnameVerifier) it;
}).to(builder::sslHostnameVerifier);
}
@SuppressWarnings("unchecked")
private static Map<String, String> convertToMap(Object options) {
if (options instanceof Map) {
return Map.class.cast(options);
}
Map<String, String> result = new LinkedHashMap<>();
for (String pair : options.toString().split(";")) {
String[] items = pair.split("=");
if (items.length != 2) {
throw new IllegalArgumentException(String.format("Provided options pair is not a valid name=value pair: %s", pair));
}
result.put(items[0], items[1]);
}
return result;
}
}