/
ConnectionOptions.java
855 lines (767 loc) · 34.2 KB
/
ConnectionOptions.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/*
* Copyright 2019 Google LLC
*
* 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
*
* http://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 com.google.cloud.spanner.connection;
import com.google.api.core.InternalApi;
import com.google.auth.Credentials;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.ServiceOptions;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.SessionPoolOptions;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
/**
* Internal connection API for Google Cloud Spanner. This class may introduce breaking changes
* without prior notice.
*
* <p>Options for creating a {@link Connection} to a Google Cloud Spanner database.
*
* <p>Usage:
*
* <pre>
* <!--SNIPPET {@link ConnectionOptions} usage-->
* {@code
* ConnectionOptions options = ConnectionOptions.newBuilder()
* .setUri("cloudspanner:/projects/my_project_id/instances/my_instance_id/databases/my_database_name?autocommit=false")
* .setCredentialsUrl("/home/cloudspanner-keys/my-key.json")
* .build();
* try(Connection connection = options.getConnection()) {
* try(ResultSet rs = connection.executeQuery(Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"))) {
* while(rs.next()) {
* // do something
* }
* }
* }
* }
* <!--SNIPPET {@link ConnectionOptions} usage-->
* </pre>
*/
@InternalApi
public class ConnectionOptions {
/** Supported connection properties that can be included in the connection URI. */
public static class ConnectionProperty {
private static final String[] BOOLEAN_VALUES = new String[] {"true", "false"};
private final String name;
private final String description;
private final String defaultValue;
private final String[] validValues;
private final int hashCode;
private static ConnectionProperty createStringProperty(String name, String description) {
return new ConnectionProperty(name, description, "", null);
}
private static ConnectionProperty createBooleanProperty(
String name, String description, boolean defaultValue) {
return new ConnectionProperty(
name, description, String.valueOf(defaultValue), BOOLEAN_VALUES);
}
private static ConnectionProperty createEmptyProperty(String name) {
return new ConnectionProperty(name, "", "", null);
}
private ConnectionProperty(
String name, String description, String defaultValue, String[] validValues) {
Preconditions.checkNotNull(name);
Preconditions.checkNotNull(description);
Preconditions.checkNotNull(defaultValue);
this.name = name;
this.description = description;
this.defaultValue = defaultValue;
this.validValues = validValues;
this.hashCode = name.toLowerCase().hashCode();
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof ConnectionProperty)) {
return false;
}
return ((ConnectionProperty) o).name.equalsIgnoreCase(this.name);
}
/** @return the name of this connection property. */
public String getName() {
return name;
}
/** @return the description of this connection property. */
public String getDescription() {
return description;
}
/** @return the default value of this connection property. */
public String getDefaultValue() {
return defaultValue;
}
/**
* @return the valid values for this connection property. <code>null</code> indicates no
* restriction.
*/
public String[] getValidValues() {
return validValues;
}
}
private static final boolean DEFAULT_USE_PLAIN_TEXT = false;
static final boolean DEFAULT_AUTOCOMMIT = true;
static final boolean DEFAULT_READONLY = false;
static final boolean DEFAULT_RETRY_ABORTS_INTERNALLY = true;
private static final String DEFAULT_CREDENTIALS = null;
private static final String DEFAULT_OAUTH_TOKEN = null;
private static final String DEFAULT_MIN_SESSIONS = null;
private static final String DEFAULT_MAX_SESSIONS = null;
private static final String DEFAULT_NUM_CHANNELS = null;
private static final String DEFAULT_USER_AGENT = null;
private static final String DEFAULT_OPTIMIZER_VERSION = "";
private static final boolean DEFAULT_RETURN_COMMIT_STATS = false;
private static final boolean DEFAULT_LENIENT = false;
private static final String PLAIN_TEXT_PROTOCOL = "http:";
private static final String HOST_PROTOCOL = "https:";
private static final String DEFAULT_HOST = "https://spanner.googleapis.com";
/** Use plain text is only for local testing purposes. */
private static final String USE_PLAIN_TEXT_PROPERTY_NAME = "usePlainText";
/** Name of the 'autocommit' connection property. */
public static final String AUTOCOMMIT_PROPERTY_NAME = "autocommit";
/** Name of the 'readonly' connection property. */
public static final String READONLY_PROPERTY_NAME = "readonly";
/** Name of the 'retry aborts internally' connection property. */
public static final String RETRY_ABORTS_INTERNALLY_PROPERTY_NAME = "retryAbortsInternally";
/** Name of the 'credentials' connection property. */
public static final String CREDENTIALS_PROPERTY_NAME = "credentials";
/**
* OAuth token to use for authentication. Cannot be used in combination with a credentials file.
*/
public static final String OAUTH_TOKEN_PROPERTY_NAME = "oauthToken";
/** Name of the 'minSessions' connection property. */
public static final String MIN_SESSIONS_PROPERTY_NAME = "minSessions";
/** Name of the 'numChannels' connection property. */
public static final String MAX_SESSIONS_PROPERTY_NAME = "maxSessions";
/** Name of the 'numChannels' connection property. */
public static final String NUM_CHANNELS_PROPERTY_NAME = "numChannels";
/** Custom user agent string is only for other Google libraries. */
private static final String USER_AGENT_PROPERTY_NAME = "userAgent";
/** Query optimizer version to use for a connection. */
private static final String OPTIMIZER_VERSION_PROPERTY_NAME = "optimizerVersion";
/** Return commit stats for read/write transactions. */
private static final String RETURN_COMMIT_STATS_PROPERTY_NAME = "returnCommitStats";
/** Name of the 'lenientMode' connection property. */
public static final String LENIENT_PROPERTY_NAME = "lenient";
/** All valid connection properties. */
public static final Set<ConnectionProperty> VALID_PROPERTIES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
ConnectionProperty.createBooleanProperty(
AUTOCOMMIT_PROPERTY_NAME,
"Should the connection start in autocommit (true/false)",
DEFAULT_AUTOCOMMIT),
ConnectionProperty.createBooleanProperty(
READONLY_PROPERTY_NAME,
"Should the connection start in read-only mode (true/false)",
DEFAULT_READONLY),
ConnectionProperty.createBooleanProperty(
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME,
"Should the connection automatically retry Aborted errors (true/false)",
DEFAULT_RETRY_ABORTS_INTERNALLY),
ConnectionProperty.createStringProperty(
CREDENTIALS_PROPERTY_NAME,
"The location of the credentials file to use for this connection. If this property is not set, the connection will use the default Google Cloud credentials for the runtime environment."),
ConnectionProperty.createStringProperty(
OAUTH_TOKEN_PROPERTY_NAME,
"A valid pre-existing OAuth token to use for authentication for this connection. Setting this property will take precedence over any value set for a credentials file."),
ConnectionProperty.createStringProperty(
MIN_SESSIONS_PROPERTY_NAME,
"The minimum number of sessions in the backing session pool. The default is 100."),
ConnectionProperty.createStringProperty(
MAX_SESSIONS_PROPERTY_NAME,
"The maximum number of sessions in the backing session pool. The default is 400."),
ConnectionProperty.createStringProperty(
NUM_CHANNELS_PROPERTY_NAME,
"The number of gRPC channels to use to communicate with Cloud Spanner. The default is 4."),
ConnectionProperty.createBooleanProperty(
USE_PLAIN_TEXT_PROPERTY_NAME,
"Use a plain text communication channel (i.e. non-TLS) for communicating with the server (true/false). Set this value to true for communication with the Cloud Spanner emulator.",
DEFAULT_USE_PLAIN_TEXT),
ConnectionProperty.createStringProperty(
USER_AGENT_PROPERTY_NAME,
"The custom user-agent property name to use when communicating with Cloud Spanner. This property is intended for internal library usage, and should not be set by applications."),
ConnectionProperty.createStringProperty(
OPTIMIZER_VERSION_PROPERTY_NAME,
"Sets the default query optimizer version to use for this connection."),
ConnectionProperty.createBooleanProperty(
RETURN_COMMIT_STATS_PROPERTY_NAME, "", DEFAULT_RETURN_COMMIT_STATS),
ConnectionProperty.createBooleanProperty(
LENIENT_PROPERTY_NAME,
"Silently ignore unknown properties in the connection string/properties (true/false)",
DEFAULT_LENIENT))));
private static final Set<ConnectionProperty> INTERNAL_PROPERTIES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
ConnectionProperty.createStringProperty(USER_AGENT_PROPERTY_NAME, ""))));
private static final Set<ConnectionProperty> INTERNAL_VALID_PROPERTIES =
Sets.union(VALID_PROPERTIES, INTERNAL_PROPERTIES);
/**
* Gets the default project-id for the current environment as defined by {@link
* ServiceOptions#getDefaultProjectId()}, and if none could be found, the project-id of the given
* credentials if it contains any.
*
* @param credentials The credentials to use to get the default project-id if none could be found
* in the environment.
* @return the default project-id.
*/
public static String getDefaultProjectId(Credentials credentials) {
String projectId = SpannerOptions.getDefaultProjectId();
if (projectId == null
&& credentials != null
&& credentials instanceof ServiceAccountCredentials) {
projectId = ((ServiceAccountCredentials) credentials).getProjectId();
}
return projectId;
}
/**
* Closes <strong>all</strong> {@link Spanner} instances that have been opened by connections
* during the lifetime of this JVM. Call this method at the end of your application to free up
* resources. You must close all {@link Connection}s that have been opened by your application
* before calling this method. Failing to do so, will cause this method to throw a {@link
* SpannerException}.
*
* <p>This method is also automatically called by a shutdown hook (see {@link
* Runtime#addShutdownHook(Thread)}) when the JVM is shutdown gracefully.
*/
public static void closeSpanner() {
SpannerPool.INSTANCE.checkAndCloseSpanners();
}
/**
* {@link SpannerOptionsConfigurator} can be used to add additional configuration for a {@link
* Spanner} instance. Intended for tests.
*/
@VisibleForTesting
interface SpannerOptionsConfigurator {
void configure(SpannerOptions.Builder options);
}
/** Builder for {@link ConnectionOptions} instances. */
public static class Builder {
private String uri;
private String credentialsUrl;
private String oauthToken;
private Credentials credentials;
private SessionPoolOptions sessionPoolOptions;
private List<StatementExecutionInterceptor> statementExecutionInterceptors =
Collections.emptyList();
private SpannerOptionsConfigurator configurator;
private Builder() {}
/** Spanner {@link ConnectionOptions} URI format. */
public static final String SPANNER_URI_FORMAT =
"(?:cloudspanner:)(?<HOSTGROUP>//[\\w.-]+(?:\\.[\\w\\.-]+)*[\\w\\-\\._~:/?#\\[\\]@!\\$&'\\(\\)\\*\\+,;=.]+)?/projects/(?<PROJECTGROUP>(([a-z]|[-.:]|[0-9])+|(DEFAULT_PROJECT_ID)))(/instances/(?<INSTANCEGROUP>([a-z]|[-]|[0-9])+)(/databases/(?<DATABASEGROUP>([a-z]|[-]|[_]|[0-9])+))?)?(?:[?|;].*)?";
private static final String SPANNER_URI_REGEX = "(?is)^" + SPANNER_URI_FORMAT + "$";
private static final Pattern SPANNER_URI_PATTERN = Pattern.compile(SPANNER_URI_REGEX);
private static final String HOST_GROUP = "HOSTGROUP";
private static final String PROJECT_GROUP = "PROJECTGROUP";
private static final String INSTANCE_GROUP = "INSTANCEGROUP";
private static final String DATABASE_GROUP = "DATABASEGROUP";
private static final String DEFAULT_PROJECT_ID_PLACEHOLDER = "DEFAULT_PROJECT_ID";
private boolean isValidUri(String uri) {
return SPANNER_URI_PATTERN.matcher(uri).matches();
}
/**
* Sets the URI of the Cloud Spanner database to connect to. A connection URI must be specified
* in this format:
*
* <pre>
* cloudspanner:[//host[:port]]/projects/project-id[/instances/instance-id[/databases/database-name]][\?property-name=property-value[;property-name=property-value]*]?
* </pre>
*
* The property-value strings should be url-encoded.
*
* <p>The project-id part of the URI may be filled with the placeholder DEFAULT_PROJECT_ID. This
* placeholder will be replaced by the default project id of the environment that is requesting
* a connection.
*
* <p>The supported properties are:
*
* <ul>
* <li>credentials (String): URL for the credentials file to use for the connection. This
* property is only used if no credentials have been specified using the {@link
* ConnectionOptions.Builder#setCredentialsUrl(String)} method. If you do not specify any
* credentials at all, the default credentials of the environment as returned by {@link
* GoogleCredentials#getApplicationDefault()} will be used.
* <li>autocommit (boolean): Sets the initial autocommit mode for the connection. Default is
* true.
* <li>readonly (boolean): Sets the initial readonly mode for the connection. Default is
* false.
* <li>minSessions (int): Sets the minimum number of sessions in the backing session pool.
* <li>maxSessions (int): Sets the maximum number of sessions in the backing session pool.
* <li>numChannels (int): Sets the number of gRPC channels to use for the connection.
* <li>retryAbortsInternally (boolean): Sets the initial retryAbortsInternally mode for the
* connection. Default is true.
* <li>optimizerVersion (string): Sets the query optimizer version to use for the connection.
* </ul>
*
* @param uri The URI of the Spanner database to connect to.
* @return this builder
*/
public Builder setUri(String uri) {
Preconditions.checkArgument(
isValidUri(uri),
"The specified URI is not a valid Cloud Spanner connection URI. Please specify a URI in the format \"cloudspanner:[//host[:port]]/projects/project-id[/instances/instance-id[/databases/database-name]][\\?property-name=property-value[;property-name=property-value]*]?\"");
checkValidProperties(uri);
this.uri = uri;
return this;
}
/** Sets the {@link SessionPoolOptions} to use for the connection. */
public Builder setSessionPoolOptions(SessionPoolOptions sessionPoolOptions) {
Preconditions.checkNotNull(sessionPoolOptions);
this.sessionPoolOptions = sessionPoolOptions;
return this;
}
/**
* Sets the URL of the credentials file to use for this connection. The URL may be a reference
* to a file on the local file system, or to a file on Google Cloud Storage. References to
* Google Cloud Storage files are only allowed when the application is running on Google Cloud
* and the environment has access to the specified storage location. It also requires that the
* Google Cloud Storage client library is present on the class path. The Google Cloud Storage
* library is not automatically added as a dependency by the JDBC driver.
*
* <p>If you do not specify a credentialsUrl (either by using this setter, or by specifying on
* the connection URI), the credentials returned by {@link
* GoogleCredentials#getApplicationDefault()} will be used for the connection.
*
* @param credentialsUrl A valid file or Google Cloud Storage URL for the credentials file to be
* used.
* @return this builder
*/
public Builder setCredentialsUrl(String credentialsUrl) {
this.credentialsUrl = credentialsUrl;
return this;
}
/**
* Sets the OAuth token to use with this connection. The token must be a valid token with access
* to the resources (project/instance/database) that the connection will be accessing. This
* authentication method cannot be used in combination with a credentials file. If both an OAuth
* token and a credentials file is specified, the {@link #build()} method will throw an
* exception.
*
* @param oauthToken A valid OAuth token for the Google Cloud project that is used by this
* connection.
* @return this builder
*/
public Builder setOAuthToken(String oauthToken) {
this.oauthToken = oauthToken;
return this;
}
@VisibleForTesting
Builder setStatementExecutionInterceptors(List<StatementExecutionInterceptor> interceptors) {
this.statementExecutionInterceptors = interceptors;
return this;
}
@VisibleForTesting
Builder setConfigurator(SpannerOptionsConfigurator configurator) {
this.configurator = Preconditions.checkNotNull(configurator);
return this;
}
@VisibleForTesting
Builder setCredentials(Credentials credentials) {
this.credentials = credentials;
return this;
}
/** @return the {@link ConnectionOptions} */
public ConnectionOptions build() {
Preconditions.checkState(this.uri != null, "Connection URI is required");
return new ConnectionOptions(this);
}
}
/**
* Create a {@link Builder} for {@link ConnectionOptions}. Use this method to create {@link
* ConnectionOptions} that can be used to obtain a {@link Connection}.
*
* @return a new {@link Builder}
*/
public static Builder newBuilder() {
return new Builder();
}
private final String uri;
private final String warnings;
private final String credentialsUrl;
private final String oauthToken;
private final Credentials fixedCredentials;
private final boolean usePlainText;
private final String host;
private final String projectId;
private final String instanceId;
private final String databaseName;
private final Credentials credentials;
private final SessionPoolOptions sessionPoolOptions;
private final Integer numChannels;
private final Integer minSessions;
private final Integer maxSessions;
private final String userAgent;
private final QueryOptions queryOptions;
private final boolean returnCommitStats;
private final boolean autocommit;
private final boolean readOnly;
private final boolean retryAbortsInternally;
private final List<StatementExecutionInterceptor> statementExecutionInterceptors;
private final SpannerOptionsConfigurator configurator;
private ConnectionOptions(Builder builder) {
Matcher matcher = Builder.SPANNER_URI_PATTERN.matcher(builder.uri);
Preconditions.checkArgument(
matcher.find(), String.format("Invalid connection URI specified: %s", builder.uri));
this.warnings = checkValidProperties(builder.uri);
this.uri = builder.uri;
this.credentialsUrl =
builder.credentialsUrl != null ? builder.credentialsUrl : parseCredentials(builder.uri);
this.oauthToken =
builder.oauthToken != null ? builder.oauthToken : parseOAuthToken(builder.uri);
this.fixedCredentials = builder.credentials;
// Check that not both credentials and an OAuth token have been specified.
Preconditions.checkArgument(
(builder.credentials == null && this.credentialsUrl == null) || this.oauthToken == null,
"Cannot specify both credentials and an OAuth token.");
this.usePlainText = parseUsePlainText(this.uri);
this.userAgent = parseUserAgent(this.uri);
QueryOptions.Builder queryOptionsBuilder = QueryOptions.newBuilder();
queryOptionsBuilder.setOptimizerVersion(parseOptimizerVersion(this.uri));
this.queryOptions = queryOptionsBuilder.build();
this.returnCommitStats = parseReturnCommitStats(this.uri);
this.host =
matcher.group(Builder.HOST_GROUP) == null
? DEFAULT_HOST
: (usePlainText ? PLAIN_TEXT_PROTOCOL : HOST_PROTOCOL)
+ matcher.group(Builder.HOST_GROUP);
this.instanceId = matcher.group(Builder.INSTANCE_GROUP);
this.databaseName = matcher.group(Builder.DATABASE_GROUP);
// Using credentials on a plain text connection is not allowed, so if the user has not specified
// any credentials and is using a plain text connection, we should not try to get the
// credentials from the environment, but default to NoCredentials.
if (builder.credentials == null
&& this.credentialsUrl == null
&& this.oauthToken == null
&& this.usePlainText) {
this.credentials = NoCredentials.getInstance();
} else if (this.oauthToken != null) {
this.credentials = new GoogleCredentials(new AccessToken(oauthToken, null));
} else if (this.fixedCredentials != null) {
this.credentials = fixedCredentials;
} else {
this.credentials = getCredentialsService().createCredentials(this.credentialsUrl);
}
this.minSessions =
parseIntegerProperty(MIN_SESSIONS_PROPERTY_NAME, parseMinSessions(builder.uri));
this.maxSessions =
parseIntegerProperty(MAX_SESSIONS_PROPERTY_NAME, parseMaxSessions(builder.uri));
this.numChannels =
parseIntegerProperty(NUM_CHANNELS_PROPERTY_NAME, parseNumChannels(builder.uri));
String projectId = matcher.group(Builder.PROJECT_GROUP);
if (Builder.DEFAULT_PROJECT_ID_PLACEHOLDER.equalsIgnoreCase(projectId)) {
projectId = getDefaultProjectId(this.credentials);
}
this.projectId = projectId;
this.autocommit = parseAutocommit(this.uri);
this.readOnly = parseReadOnly(this.uri);
this.retryAbortsInternally = parseRetryAbortsInternally(this.uri);
this.statementExecutionInterceptors =
Collections.unmodifiableList(builder.statementExecutionInterceptors);
this.configurator = builder.configurator;
if (this.minSessions != null || this.maxSessions != null) {
SessionPoolOptions.Builder sessionPoolOptionsBuilder =
builder.sessionPoolOptions == null
? SessionPoolOptions.newBuilder()
: builder.sessionPoolOptions.toBuilder();
if (this.minSessions != null) {
sessionPoolOptionsBuilder.setMinSessions(this.minSessions);
}
if (this.maxSessions != null) {
sessionPoolOptionsBuilder.setMaxSessions(this.maxSessions);
}
this.sessionPoolOptions = sessionPoolOptionsBuilder.build();
} else {
this.sessionPoolOptions = builder.sessionPoolOptions;
}
}
private static Integer parseIntegerProperty(String propertyName, String value) {
if (value != null) {
try {
return Integer.valueOf(value);
} catch (NumberFormatException e) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
String.format("Invalid %s value specified: %s", propertyName, value),
e);
}
}
return null;
}
SpannerOptionsConfigurator getConfigurator() {
return configurator;
}
@VisibleForTesting
CredentialsService getCredentialsService() {
return CredentialsService.INSTANCE;
}
@VisibleForTesting
static boolean parseUsePlainText(String uri) {
String value = parseUriProperty(uri, USE_PLAIN_TEXT_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_USE_PLAIN_TEXT;
}
@VisibleForTesting
static boolean parseAutocommit(String uri) {
String value = parseUriProperty(uri, AUTOCOMMIT_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_AUTOCOMMIT;
}
@VisibleForTesting
static boolean parseReadOnly(String uri) {
String value = parseUriProperty(uri, READONLY_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_READONLY;
}
@VisibleForTesting
static boolean parseRetryAbortsInternally(String uri) {
String value = parseUriProperty(uri, RETRY_ABORTS_INTERNALLY_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_RETRY_ABORTS_INTERNALLY;
}
@VisibleForTesting
static String parseCredentials(String uri) {
String value = parseUriProperty(uri, CREDENTIALS_PROPERTY_NAME);
return value != null ? value : DEFAULT_CREDENTIALS;
}
@VisibleForTesting
static String parseOAuthToken(String uri) {
String value = parseUriProperty(uri, OAUTH_TOKEN_PROPERTY_NAME);
return value != null ? value : DEFAULT_OAUTH_TOKEN;
}
@VisibleForTesting
static String parseMinSessions(String uri) {
String value = parseUriProperty(uri, MIN_SESSIONS_PROPERTY_NAME);
return value != null ? value : DEFAULT_MIN_SESSIONS;
}
@VisibleForTesting
static String parseMaxSessions(String uri) {
String value = parseUriProperty(uri, MAX_SESSIONS_PROPERTY_NAME);
return value != null ? value : DEFAULT_MAX_SESSIONS;
}
@VisibleForTesting
static String parseNumChannels(String uri) {
String value = parseUriProperty(uri, NUM_CHANNELS_PROPERTY_NAME);
return value != null ? value : DEFAULT_NUM_CHANNELS;
}
@VisibleForTesting
static String parseUserAgent(String uri) {
String value = parseUriProperty(uri, USER_AGENT_PROPERTY_NAME);
return value != null ? value : DEFAULT_USER_AGENT;
}
@VisibleForTesting
static String parseOptimizerVersion(String uri) {
String value = parseUriProperty(uri, OPTIMIZER_VERSION_PROPERTY_NAME);
return value != null ? value : DEFAULT_OPTIMIZER_VERSION;
}
@VisibleForTesting
static boolean parseReturnCommitStats(String uri) {
String value = parseUriProperty(uri, RETURN_COMMIT_STATS_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_RETURN_COMMIT_STATS;
}
@VisibleForTesting
static boolean parseLenient(String uri) {
String value = parseUriProperty(uri, LENIENT_PROPERTY_NAME);
return value != null ? Boolean.valueOf(value) : DEFAULT_LENIENT;
}
@VisibleForTesting
static String parseUriProperty(String uri, String property) {
Pattern pattern = Pattern.compile(String.format("(?is)(?:;|\\?)%s=(.*?)(?:;|$)", property));
Matcher matcher = pattern.matcher(uri);
if (matcher.find() && matcher.groupCount() == 1) {
return matcher.group(1);
}
return null;
}
/** Check that only valid properties have been specified. */
@VisibleForTesting
static String checkValidProperties(String uri) {
String invalidProperties = "";
List<String> properties = parseProperties(uri);
boolean lenient = parseLenient(uri);
for (String property : properties) {
if (!INTERNAL_VALID_PROPERTIES.contains(ConnectionProperty.createEmptyProperty(property))) {
if (invalidProperties.length() > 0) {
invalidProperties = invalidProperties + ", ";
}
invalidProperties = invalidProperties + property;
}
}
if (lenient) {
return String.format(
"Invalid properties found in connection URI: %s", invalidProperties.toString());
} else {
Preconditions.checkArgument(
invalidProperties.isEmpty(),
String.format(
"Invalid properties found in connection URI. Add lenient=true to the connection string to ignore unknown properties. Invalid properties: %s",
invalidProperties.toString()));
return null;
}
}
@VisibleForTesting
static List<String> parseProperties(String uri) {
Pattern pattern = Pattern.compile("(?is)(?:\\?|;)(?<PROPERTY>.*?)=(?:.*?)");
Matcher matcher = pattern.matcher(uri);
List<String> res = new ArrayList<>();
while (matcher.find() && matcher.group("PROPERTY") != null) {
res.add(matcher.group("PROPERTY"));
}
return res;
}
/**
* Create a new {@link Connection} from this {@link ConnectionOptions}. Calling this method
* multiple times for the same {@link ConnectionOptions} will return multiple instances of {@link
* Connection}s to the same database.
*
* @return a new {@link Connection} to the database referenced by this {@link ConnectionOptions}
*/
public Connection getConnection() {
return new ConnectionImpl(this);
}
/** The URI of this {@link ConnectionOptions} */
public String getUri() {
return uri;
}
/** The credentials URL of this {@link ConnectionOptions} */
public String getCredentialsUrl() {
return credentialsUrl;
}
String getOAuthToken() {
return this.oauthToken;
}
Credentials getFixedCredentials() {
return this.fixedCredentials;
}
/** The {@link SessionPoolOptions} of this {@link ConnectionOptions}. */
public SessionPoolOptions getSessionPoolOptions() {
return sessionPoolOptions;
}
/**
* The minimum number of sessions in the backing session pool of this connection. The session pool
* is shared between all connections in the same JVM that connect to the same Cloud Spanner
* database using the same connection settings.
*/
public Integer getMinSessions() {
return minSessions;
}
/**
* The maximum number of sessions in the backing session pool of this connection. The session pool
* is shared between all connections in the same JVM that connect to the same Cloud Spanner
* database using the same connection settings.
*/
public Integer getMaxSessions() {
return maxSessions;
}
/** The number of channels to use for the connection. */
public Integer getNumChannels() {
return numChannels;
}
/** The host and port number that this {@link ConnectionOptions} will connect to */
public String getHost() {
return host;
}
/** The Google Project ID that this {@link ConnectionOptions} will connect to */
public String getProjectId() {
return projectId;
}
/** The Spanner Instance ID that this {@link ConnectionOptions} will connect to */
public String getInstanceId() {
return instanceId;
}
/** The Spanner database name that this {@link ConnectionOptions} will connect to */
public String getDatabaseName() {
return databaseName;
}
/** The Spanner {@link DatabaseId} that this {@link ConnectionOptions} will connect to */
public DatabaseId getDatabaseId() {
Preconditions.checkState(projectId != null, "Project ID is not specified");
Preconditions.checkState(instanceId != null, "Instance ID is not specified");
Preconditions.checkState(databaseName != null, "Database name is not specified");
return DatabaseId.of(projectId, instanceId, databaseName);
}
/**
* The {@link Credentials} of this {@link ConnectionOptions}. This is either the credentials
* specified in the credentialsUrl or the default Google application credentials
*/
public Credentials getCredentials() {
return credentials;
}
/** The initial autocommit value for connections created by this {@link ConnectionOptions} */
public boolean isAutocommit() {
return autocommit;
}
/** The initial readonly value for connections created by this {@link ConnectionOptions} */
public boolean isReadOnly() {
return readOnly;
}
/**
* The initial retryAbortsInternally value for connections created by this {@link
* ConnectionOptions}
*/
public boolean isRetryAbortsInternally() {
return retryAbortsInternally;
}
/** Any warnings that were generated while creating the {@link ConnectionOptions} instance. */
@Nullable
public String getWarnings() {
return warnings;
}
/** Use http instead of https. Only valid for (local) test servers. */
boolean isUsePlainText() {
return usePlainText;
}
/**
* The (custom) user agent string to use for this connection. If <code>null</code>, then the
* default JDBC user agent string will be used.
*/
String getUserAgent() {
return userAgent;
}
/** The {@link QueryOptions} to use for the connection. */
QueryOptions getQueryOptions() {
return queryOptions;
}
/**
* The initial returnCommitStats value for connections created by this {@link ConnectionOptions}
*/
public boolean isReturnCommitStats() {
return returnCommitStats;
}
/** Interceptors that should be executed after each statement */
List<StatementExecutionInterceptor> getStatementExecutionInterceptors() {
return statementExecutionInterceptors;
}
@Override
public String toString() {
return getUri();
}
}