26
26
27
27
import javax .net .ssl .HostnameVerifier ;
28
28
import java .util .LinkedHashMap ;
29
+ import java .util .Locale ;
29
30
import java .util .Map ;
30
31
import java .util .function .Function ;
31
32
@@ -136,37 +137,128 @@ public final class PostgresqlConnectionFactoryProvider implements ConnectionFact
136
137
*/
137
138
public static final Option <Map <String , String >> OPTIONS = Option .valueOf ("options" );
138
139
140
+ /**
141
+ * Returns a new {@link PostgresqlConnectionConfiguration.Builder} configured with the given {@link ConnectionFactoryOptions}.
142
+ *
143
+ * @param connectionFactoryOptions {@link ConnectionFactoryOptions} used to initialize the {@link PostgresqlConnectionConfiguration.Builder}.
144
+ * @return a {@link PostgresqlConnectionConfiguration.Builder}
145
+ * @since 0.8.3
146
+ */
147
+ public static PostgresqlConnectionConfiguration .Builder builder (ConnectionFactoryOptions connectionFactoryOptions ) {
148
+ return fromConnectionFactoryOptions (connectionFactoryOptions );
149
+ }
150
+
139
151
@ Override
140
152
public PostgresqlConnectionFactory create (ConnectionFactoryOptions connectionFactoryOptions ) {
141
- return new PostgresqlConnectionFactory (createConfiguration (connectionFactoryOptions ));
153
+ return new PostgresqlConnectionFactory (builder (connectionFactoryOptions ).build ());
154
+ }
155
+
156
+ @ Override
157
+ public String getDriver () {
158
+ return POSTGRESQL_DRIVER ;
142
159
}
143
160
144
- private static PostgresqlConnectionConfiguration createConfiguration (ConnectionFactoryOptions connectionFactoryOptions ) {
161
+ @ Override
162
+ public boolean supports (ConnectionFactoryOptions connectionFactoryOptions ) {
145
163
Assert .requireNonNull (connectionFactoryOptions , "connectionFactoryOptions must not be null" );
146
164
147
- boolean tcp ;
148
- PostgresqlConnectionConfiguration .Builder builder = PostgresqlConnectionConfiguration .builder ();
165
+ String driver = connectionFactoryOptions .getValue (DRIVER );
166
+ return driver != null && (driver .equals (POSTGRESQL_DRIVER ) || driver .equals (LEGACY_POSTGRESQL_DRIVER ));
167
+ }
149
168
150
- String applicationName = connectionFactoryOptions .getValue (APPLICATION_NAME );
151
- if (applicationName != null ) {
152
- builder .applicationName (applicationName );
169
+ private static void setupSsl (PostgresqlConnectionConfiguration .Builder builder , ConnectionFactoryOptions connectionFactoryOptions ) {
170
+ Boolean ssl = connectionFactoryOptions .getValue (SSL );
171
+ if (ssl != null && ssl ) {
172
+ builder .enableSsl ();
153
173
}
154
174
155
- Object autodetectExtensions = connectionFactoryOptions .getValue (AUTODETECT_EXTENSIONS );
156
- if (autodetectExtensions != null ) {
157
- builder .autodetectExtensions (convertToBoolean (autodetectExtensions ));
175
+ Object sslMode = connectionFactoryOptions .getValue (SSL_MODE );
176
+ if (sslMode != null ) {
177
+ if (sslMode instanceof String ) {
178
+ builder .sslMode (SSLMode .fromValue (sslMode .toString ()));
179
+ } else {
180
+ builder .sslMode ((SSLMode ) sslMode );
181
+ }
158
182
}
159
183
160
- builder .connectTimeout (connectionFactoryOptions .getValue (CONNECT_TIMEOUT ));
161
- builder .database (connectionFactoryOptions .getValue (DATABASE ));
184
+ String sslRootCert = connectionFactoryOptions .getValue (SSL_ROOT_CERT );
185
+ if (sslRootCert != null ) {
186
+ builder .sslRootCert (sslRootCert );
187
+ }
162
188
163
- if (connectionFactoryOptions .hasOption (SOCKET )) {
164
- tcp = false ;
165
- builder .socket (connectionFactoryOptions .getRequiredValue (SOCKET ));
166
- } else {
167
- tcp = true ;
168
- builder .host (connectionFactoryOptions .getRequiredValue (HOST ));
189
+ String sslCert = connectionFactoryOptions .getValue (SSL_CERT );
190
+ if (sslCert != null ) {
191
+ builder .sslCert (sslCert );
192
+ }
193
+
194
+ String sslKey = connectionFactoryOptions .getValue (SSL_KEY );
195
+ if (sslKey != null ) {
196
+ builder .sslKey (sslKey );
169
197
}
198
+
199
+ String sslPassword = connectionFactoryOptions .getValue (SSL_PASSWORD );
200
+ if (sslPassword != null ) {
201
+ builder .sslPassword (sslPassword );
202
+ }
203
+
204
+ if (connectionFactoryOptions .hasOption (SSL_CONTEXT_BUILDER_CUSTOMIZER )) {
205
+ builder .sslContextBuilderCustomizer (connectionFactoryOptions .getRequiredValue (SSL_CONTEXT_BUILDER_CUSTOMIZER ));
206
+ }
207
+
208
+ setSslHostnameVerifier (builder , connectionFactoryOptions );
209
+ }
210
+
211
+ private static void setSslHostnameVerifier (PostgresqlConnectionConfiguration .Builder builder , ConnectionFactoryOptions connectionFactoryOptions ) {
212
+ Object sslHostnameVerifier = connectionFactoryOptions .getValue (SSL_HOSTNAME_VERIFIER );
213
+ if (sslHostnameVerifier != null ) {
214
+
215
+ if (sslHostnameVerifier instanceof String ) {
216
+
217
+ try {
218
+ Class <?> verifierClass = Class .forName ((String ) sslHostnameVerifier );
219
+ Object verifier = verifierClass .getConstructor ().newInstance ();
220
+
221
+ builder .sslHostnameVerifier ((HostnameVerifier ) verifier );
222
+ } catch (ReflectiveOperationException e ) {
223
+ throw new IllegalStateException ("Cannot instantiate " + sslHostnameVerifier , e );
224
+ }
225
+ } else {
226
+ builder .sslHostnameVerifier ((HostnameVerifier ) sslHostnameVerifier );
227
+ }
228
+ }
229
+ }
230
+
231
+ private static boolean isUsingTcp (ConnectionFactoryOptions connectionFactoryOptions ) {
232
+ return !connectionFactoryOptions .hasOption (SOCKET );
233
+ }
234
+
235
+ private static boolean convertToBoolean (Object value ) {
236
+ return value instanceof Boolean ? (boolean ) value : Boolean .parseBoolean (value .toString ());
237
+ }
238
+
239
+ private static <T extends Enum <T >> T convertToEnum (Object value , Class <T > enumType ) {
240
+ return enumType .isInstance (value ) ? enumType .cast (value ) : Enum .valueOf (enumType , value .toString ().toUpperCase (Locale .US ));
241
+ }
242
+
243
+ private static int convertToInt (Object value ) {
244
+ return value instanceof Integer ? (int ) value : Integer .parseInt (value .toString ());
245
+ }
246
+
247
+ /**
248
+ * Configure the builder with the given {@link ConnectionFactoryOptions}.
249
+ *
250
+ * @param connectionFactoryOptions {@link ConnectionFactoryOptions}
251
+ * @return this {@link PostgresqlConnectionConfiguration.Builder}
252
+ * @throws IllegalArgumentException if {@code connectionFactoryOptions} is {@code null}
253
+ */
254
+ private static PostgresqlConnectionConfiguration .Builder fromConnectionFactoryOptions (ConnectionFactoryOptions connectionFactoryOptions ) {
255
+
256
+ Assert .requireNonNull (connectionFactoryOptions , "connectionFactoryOptions must not be null" );
257
+
258
+ PostgresqlConnectionConfiguration .Builder builder = PostgresqlConnectionConfiguration .builder ();
259
+
260
+ builder .connectTimeout (connectionFactoryOptions .getValue (CONNECT_TIMEOUT ));
261
+ builder .database (connectionFactoryOptions .getValue (DATABASE ));
170
262
builder .password (connectionFactoryOptions .getValue (PASSWORD ));
171
263
172
264
if (connectionFactoryOptions .getValue (CURRENT_SCHEMA ) != null ) {
@@ -177,6 +269,16 @@ private static PostgresqlConnectionConfiguration createConfiguration(ConnectionF
177
269
178
270
builder .username (connectionFactoryOptions .getRequiredValue (USER ));
179
271
272
+ String applicationName = connectionFactoryOptions .getValue (APPLICATION_NAME );
273
+ if (applicationName != null ) {
274
+ builder .applicationName (applicationName );
275
+ }
276
+
277
+ Object autodetectExtensions = connectionFactoryOptions .getValue (AUTODETECT_EXTENSIONS );
278
+ if (autodetectExtensions != null ) {
279
+ builder .autodetectExtensions (convertToBoolean (autodetectExtensions ));
280
+ }
281
+
180
282
Integer port = connectionFactoryOptions .getValue (PORT );
181
283
if (port != null ) {
182
284
builder .port (port );
@@ -187,9 +289,7 @@ private static PostgresqlConnectionConfiguration createConfiguration(ConnectionF
187
289
builder .fetchSize (convertToInt (fetchSize ));
188
290
}
189
291
190
-
191
292
Object forceBinary = connectionFactoryOptions .getValue (FORCE_BINARY );
192
-
193
293
if (forceBinary != null ) {
194
294
builder .forceBinary (convertToBoolean (forceBinary ));
195
295
}
@@ -204,86 +304,14 @@ private static PostgresqlConnectionConfiguration createConfiguration(ConnectionF
204
304
builder .options (convertToMap (options ));
205
305
}
206
306
207
- if (tcp ) {
208
- Boolean ssl = connectionFactoryOptions .getValue (SSL );
209
- if (ssl != null && ssl ) {
210
- builder .enableSsl ();
211
- }
212
-
213
- Object sslMode = connectionFactoryOptions .getValue (SSL_MODE );
214
- if (sslMode != null ) {
215
- if (sslMode instanceof String ) {
216
- builder .sslMode (SSLMode .fromValue (sslMode .toString ()));
217
- } else {
218
- builder .sslMode ((SSLMode ) sslMode );
219
- }
220
- }
221
-
222
- String sslRootCert = connectionFactoryOptions .getValue (SSL_ROOT_CERT );
223
- if (sslRootCert != null ) {
224
- builder .sslRootCert (sslRootCert );
225
- }
226
-
227
- String sslCert = connectionFactoryOptions .getValue (SSL_CERT );
228
- if (sslCert != null ) {
229
- builder .sslCert (sslCert );
230
- }
231
-
232
- String sslKey = connectionFactoryOptions .getValue (SSL_KEY );
233
- if (sslKey != null ) {
234
- builder .sslKey (sslKey );
235
- }
236
-
237
- String sslPassword = connectionFactoryOptions .getValue (SSL_PASSWORD );
238
- if (sslPassword != null ) {
239
- builder .sslPassword (sslPassword );
240
- }
241
-
242
- Object sslHostnameVerifier = connectionFactoryOptions .getValue (SSL_HOSTNAME_VERIFIER );
243
- if (sslHostnameVerifier != null ) {
244
-
245
- if (sslHostnameVerifier instanceof String ) {
246
-
247
- try {
248
- Class <?> verifierClass = Class .forName ((String ) sslHostnameVerifier );
249
- Object verifier = verifierClass .getConstructor ().newInstance ();
250
-
251
- builder .sslHostnameVerifier ((HostnameVerifier ) verifier );
252
- } catch (ReflectiveOperationException e ) {
253
- throw new IllegalStateException ("Cannot instantiate " + sslHostnameVerifier , e );
254
- }
255
- } else {
256
- builder .sslHostnameVerifier ((HostnameVerifier ) sslHostnameVerifier );
257
- }
258
- }
259
-
260
- if (connectionFactoryOptions .hasOption (SSL_CONTEXT_BUILDER_CUSTOMIZER )) {
261
- builder .sslContextBuilderCustomizer (connectionFactoryOptions .getRequiredValue (SSL_CONTEXT_BUILDER_CUSTOMIZER ));
262
- }
307
+ if (isUsingTcp (connectionFactoryOptions )) {
308
+ builder .host (connectionFactoryOptions .getRequiredValue (HOST ));
309
+ setupSsl (builder , connectionFactoryOptions );
310
+ } else {
311
+ builder .socket (connectionFactoryOptions .getRequiredValue (SOCKET ));
263
312
}
264
313
265
- return builder .build ();
266
- }
267
-
268
- @ Override
269
- public String getDriver () {
270
- return POSTGRESQL_DRIVER ;
271
- }
272
-
273
- @ Override
274
- public boolean supports (ConnectionFactoryOptions connectionFactoryOptions ) {
275
- Assert .requireNonNull (connectionFactoryOptions , "connectionFactoryOptions must not be null" );
276
-
277
- String driver = connectionFactoryOptions .getValue (DRIVER );
278
- return driver != null && (driver .equals (POSTGRESQL_DRIVER ) || driver .equals (LEGACY_POSTGRESQL_DRIVER ));
279
- }
280
-
281
- private static boolean convertToBoolean (Object value ) {
282
- return value instanceof Boolean ? (boolean ) value : Boolean .parseBoolean (value .toString ());
283
- }
284
-
285
- private static int convertToInt (Object value ) {
286
- return value instanceof Integer ? (int ) value : Integer .parseInt (value .toString ());
314
+ return builder ;
287
315
}
288
316
289
317
@ SuppressWarnings ("unchecked" )
0 commit comments