forked from googleapis/java-datastore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatastoreOptions.java
246 lines (214 loc) · 7.45 KB
/
DatastoreOptions.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
/*
* Copyright 2015 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.datastore.v1.client;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.core.BetaApi;
import java.util.Arrays;
import java.util.List;
/**
* An immutable object containing settings for the datastore.
*
* <p>Example for connecting to a datastore:
*
* <pre>
* DatastoreOptions options = new DatastoreOptions.Builder()
* .projectId("my-project-id")
* .credential(DatastoreHelper.getComputeEngineCredential())
* .build();
* DatastoreFactory.get().create(options);
* </pre>
*
* <p>The options should be passed to {@link DatastoreFactory#create}.
*/
public class DatastoreOptions {
private final String projectId;
@Deprecated private final String databaseId;
private final String projectEndpoint;
private final String host;
private final String localHost;
private final HttpRequestInitializer initializer;
private final Credential credential;
private final HttpTransport transport;
public static final List<String> SCOPES =
Arrays.asList("https://www.googleapis.com/auth/datastore");
DatastoreOptions(Builder b) {
checkArgument(
b.projectId != null || b.projectEndpoint != null,
"Either project ID or project endpoint must be provided.");
this.projectId = b.projectId;
this.databaseId = b.databaseId;
this.projectEndpoint = b.projectEndpoint;
this.host = b.host;
this.localHost = b.localHost;
this.initializer = b.initializer;
this.credential = b.credential;
this.transport = b.transport;
}
/** Builder for {@link DatastoreOptions}. */
public static class Builder {
private static final String PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR =
"Cannot set both project endpoint and project ID.";
private static final String PROJECT_ENDPOINT_AND_HOST_ERROR =
"Can set at most one of project endpoint, host, and local host.";
private String projectId;
@Deprecated private String databaseId;
private String projectEndpoint;
private String host;
private String localHost;
private HttpRequestInitializer initializer;
private Credential credential;
private HttpTransport transport;
public Builder() {}
public Builder(DatastoreOptions options) {
this.projectId = options.projectId;
this.databaseId = options.databaseId;
this.projectEndpoint = options.projectEndpoint;
this.host = options.host;
this.localHost = options.localHost;
this.initializer = options.initializer;
this.credential = options.credential;
this.transport = options.transport;
}
public DatastoreOptions build() {
return new DatastoreOptions(this);
}
/** Sets the project ID used to access Cloud Datastore. */
public Builder projectId(String projectId) {
checkArgument(projectEndpoint == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
this.projectId = projectId;
return this;
}
/**
* This field is ignored and will be removed in a future release. Please set the database id on
* the request itself. For example:
*
* <pre>{@code
* CommitRequest.newBuilder()
* .setDatabaseId("my-database-id")
* ....
* .build();
* }</pre>
*/
@BetaApi
@Deprecated
public Builder databaseId(String databaseId) {
this.databaseId = databaseId;
return this;
}
/**
* Sets the host used to access Cloud Datastore. To connect to the Cloud Datastore Emulator, use
* {@link #localHost} instead.
*/
public Builder host(String host) {
checkArgument(projectEndpoint == null && localHost == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
if (includesScheme(host)) {
throw new IllegalArgumentException(
String.format("Host \"%s\" must not include scheme.", host));
}
this.host = host;
return this;
}
/**
* Configures the client to access Cloud Datastore on a local host (typically a Cloud Datastore
* Emulator instance). Call this method also configures the client not to attach credentials to
* requests.
*/
public Builder localHost(String localHost) {
checkArgument(projectEndpoint == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
if (includesScheme(localHost)) {
throw new IllegalArgumentException(
String.format("Local host \"%s\" must not include scheme.", localHost));
}
this.localHost = localHost;
return this;
}
/**
* Sets the project endpoint used to access Cloud Datastore. Prefer using {@link #projectId}
* and/or {@link #host}/{@link #localHost} when possible.
*
* @deprecated Use {@link #projectId} and/or {@link #host}/{@link #localHost} instead.
*/
@Deprecated
public Builder projectEndpoint(String projectEndpoint) {
checkArgument(projectId == null, PROJECT_ENDPOINT_AND_PROJECT_ID_ERROR);
checkArgument(localHost == null && host == null, PROJECT_ENDPOINT_AND_HOST_ERROR);
if (!includesScheme(projectEndpoint)) {
throw new IllegalArgumentException(
String.format("Project endpoint \"%s\" must include scheme.", projectEndpoint));
}
this.projectEndpoint = projectEndpoint;
return this;
}
/** Sets the (optional) initializer to run on HTTP requests to Cloud Datastore. */
public Builder initializer(HttpRequestInitializer initializer) {
this.initializer = initializer;
return this;
}
/** Sets the Google APIs {@link Credential} used to access Cloud Datastore. */
public Builder credential(Credential credential) {
this.credential = credential;
return this;
}
/** Sets the transport used to access Cloud Datastore. */
public Builder transport(HttpTransport transport) {
this.transport = transport;
return this;
}
private static boolean includesScheme(String url) {
return url.startsWith("http://") || url.startsWith("https://");
}
}
public String getProjectId() {
return projectId;
}
/**
* This field is ignored and will be removed in a future release. Please set the database id on
* the request itself. For example:
*
* <pre>{@code
* CommitRequest.newBuilder()
* .setDatabaseId("my-database-id")
* ....
* .build();
* }</pre>
*/
@BetaApi
@Deprecated
public String getDatabaseId() {
return databaseId;
}
public String getProjectEndpoint() {
return projectEndpoint;
}
public String getHost() {
return host;
}
public String getLocalHost() {
return localHost;
}
public HttpRequestInitializer getInitializer() {
return initializer;
}
public Credential getCredential() {
return credential;
}
public HttpTransport getTransport() {
return transport;
}
}