@@ -71,25 +71,28 @@ public final class PostgresqlConnectionConfiguration {
71
71
72
72
private final String schema ;
73
73
74
+ private final String socket ;
75
+
74
76
private final String username ;
75
77
76
78
private final SSLConfig sslConfig ;
77
79
78
80
private PostgresqlConnectionConfiguration (String applicationName , boolean autodetectExtensions ,
79
- @ Nullable Duration connectTimeout , @ Nullable String database , List <Extension > extensions , boolean forceBinary , String host ,
80
- @ Nullable Map <String , String > options , @ Nullable CharSequence password , int port , @ Nullable String schema , String username ,
81
+ @ Nullable Duration connectTimeout , @ Nullable String database , List <Extension > extensions , boolean forceBinary , @ Nullable String host ,
82
+ @ Nullable Map <String , String > options , @ Nullable CharSequence password , int port , @ Nullable String schema , @ Nullable String socket , String username ,
81
83
SSLConfig sslConfig ) {
82
84
this .applicationName = Assert .requireNonNull (applicationName , "applicationName must not be null" );
83
85
this .autodetectExtensions = autodetectExtensions ;
84
86
this .connectTimeout = connectTimeout ;
85
87
this .extensions = Assert .requireNonNull (extensions , "extensions must not be null" );
86
88
this .database = database ;
87
89
this .forceBinary = forceBinary ;
88
- this .host = Assert . requireNonNull ( host , "host must not be null" ) ;
90
+ this .host = host ;
89
91
this .options = options ;
90
92
this .password = password ;
91
93
this .port = port ;
92
94
this .schema = schema ;
95
+ this .socket = socket ;
93
96
this .username = Assert .requireNonNull (username , "username must not be null" );
94
97
this .sslConfig = sslConfig ;
95
98
}
@@ -150,10 +153,22 @@ List<Extension> getExtensions() {
150
153
return this .extensions ;
151
154
}
152
155
156
+ @ Nullable
153
157
String getHost () {
154
158
return this .host ;
155
159
}
156
160
161
+ String getRequiredHost () {
162
+
163
+ String host = getHost ();
164
+
165
+ if (host == null || host .isEmpty ()) {
166
+ throw new IllegalStateException ("Connection is configured for socket connections and not for host usage" );
167
+ }
168
+
169
+ return host ;
170
+ }
171
+
157
172
@ Nullable
158
173
Map <String , String > getOptions () {
159
174
return this .options ;
@@ -173,6 +188,22 @@ String getSchema() {
173
188
return this .schema ;
174
189
}
175
190
191
+ @ Nullable
192
+ String getSocket () {
193
+ return this .socket ;
194
+ }
195
+
196
+ String getRequiredSocket () {
197
+
198
+ String socket = getSocket ();
199
+
200
+ if (socket == null || socket .isEmpty ()) {
201
+ throw new IllegalStateException ("Connection is configured to use host and port connections and not for socket usage" );
202
+ }
203
+
204
+ return socket ;
205
+ }
206
+
176
207
String getUsername () {
177
208
return this .username ;
178
209
}
@@ -185,6 +216,10 @@ boolean isForceBinary() {
185
216
return this .forceBinary ;
186
217
}
187
218
219
+ boolean isUseSocket () {
220
+ return getSocket () != null ;
221
+ }
222
+
188
223
SSLConfig getSslConfig () {
189
224
return this .sslConfig ;
190
225
}
@@ -223,6 +258,9 @@ public static final class Builder {
223
258
@ Nullable
224
259
private String schema ;
225
260
261
+ @ Nullable
262
+ private String socket ;
263
+
226
264
@ Nullable
227
265
private String sslCert = null ;
228
266
@@ -275,16 +313,20 @@ public Builder autodetectExtensions(boolean autodetectExtensions) {
275
313
*/
276
314
public PostgresqlConnectionConfiguration build () {
277
315
278
- if (this .host == null ) {
279
- throw new IllegalArgumentException ("host must not be null" );
316
+ if (this .host == null && this .socket == null ) {
317
+ throw new IllegalArgumentException ("host or socket must not be null" );
318
+ }
319
+
320
+ if (this .host != null && this .socket != null ) {
321
+ throw new IllegalArgumentException ("Connection must be configured for either host/port or socket usage but not both" );
280
322
}
281
323
282
324
if (this .username == null ) {
283
325
throw new IllegalArgumentException ("username must not be null" );
284
326
}
285
327
286
328
return new PostgresqlConnectionConfiguration (this .applicationName , this .autodetectExtensions , this .connectTimeout , this .database , this .extensions , this .forceBinary , this .host ,
287
- this .options , this .password , this .port , this .schema , this .username , this .createSslConfig ());
329
+ this .options , this .password , this .port , this .schema , this .socket , this . username , this .createSslConfig ());
288
330
}
289
331
290
332
/**
@@ -418,6 +460,19 @@ public Builder schema(@Nullable String schema) {
418
460
return this ;
419
461
}
420
462
463
+ /**
464
+ * Configure the unix domain socket to connect to.
465
+ *
466
+ * @param socket the socket path
467
+ * @return this {@link Builder}
468
+ * @throws IllegalArgumentException if {@code socket} is {@code null}
469
+ */
470
+ public Builder socket (String socket ) {
471
+ this .socket = Assert .requireNonNull (socket , "host must not be null" );
472
+ sslMode (SSLMode .DISABLE );
473
+ return this ;
474
+ }
475
+
421
476
/**
422
477
* Configure ssl cert for client certificate authentication.
423
478
*
@@ -488,6 +543,7 @@ public String toString() {
488
543
", port=" + this .port +
489
544
", schema='" + this .schema + '\'' +
490
545
", username='" + this .username + '\'' +
546
+ ", socket='" + this .socket + '\'' +
491
547
", sslMode='" + this .sslMode + '\'' +
492
548
", sslRootCert='" + this .sslRootCert + '\'' +
493
549
", sslCert='" + this .sslCert + '\'' +
@@ -520,7 +576,7 @@ public Builder username(String username) {
520
576
}
521
577
522
578
private SSLConfig createSslConfig () {
523
- if (this .sslMode == SSLMode .DISABLE ) {
579
+ if (this .socket != null || this . sslMode == SSLMode .DISABLE ) {
524
580
return new SSLConfig (SSLMode .DISABLE , null , (hostname , session ) -> true );
525
581
}
526
582
HostnameVerifier hostnameVerifier = this .sslHostnameVerifier ;
0 commit comments