18
18
19
19
import static com .google .cloud .datastore .Validator .validateNamespace ;
20
20
21
+ import com .google .api .gax .core .CredentialsProvider ;
22
+ import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
23
+ import com .google .api .gax .rpc .TransportChannelProvider ;
21
24
import com .google .cloud .ServiceDefaults ;
22
25
import com .google .cloud .ServiceOptions ;
23
26
import com .google .cloud .ServiceRpc ;
@@ -46,6 +49,9 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
46
49
public static final String PROJECT_ID_ENV_VAR = "DATASTORE_PROJECT_ID" ;
47
50
public static final String LOCAL_HOST_ENV_VAR = "DATASTORE_EMULATOR_HOST" ;
48
51
52
+ private transient TransportChannelProvider channelProvider = null ;
53
+ private transient CredentialsProvider credentialsProvider = null ;
54
+
49
55
private final String namespace ;
50
56
private final String databaseId ;
51
57
@@ -69,6 +75,10 @@ public ServiceRpc create(DatastoreOptions options) {
69
75
if (options .getTransportOptions () instanceof GrpcTransportOptions ) {
70
76
return new GrpcDatastoreRpc (options );
71
77
} else if (options .getTransportOptions () instanceof HttpTransportOptions ) {
78
+ // todo see if we can remove this check
79
+ if (DatastoreUtils .isEmulator (options )) {
80
+ throw new IllegalArgumentException ("Only GRPC channels are allowed for emulator." );
81
+ }
72
82
return new HttpDatastoreRpc (options );
73
83
} else {
74
84
throw new IllegalArgumentException (
@@ -84,20 +94,46 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
84
94
85
95
private String namespace ;
86
96
private String databaseId ;
97
+ private TransportChannelProvider channelProvider = null ;
98
+ private CredentialsProvider credentialsProvider = null ;
87
99
88
100
private Builder () {}
89
101
90
102
private Builder (DatastoreOptions options ) {
91
103
super (options );
92
- namespace = options .namespace ;
93
- databaseId = options .databaseId ;
104
+ this .namespace = options .namespace ;
105
+ this .databaseId = options .databaseId ;
106
+ this .channelProvider = validateChannelProvider (options .channelProvider );
107
+ this .credentialsProvider = options .credentialsProvider ;
94
108
}
95
109
96
110
@ Override
97
111
public Builder setTransportOptions (TransportOptions transportOptions ) {
98
112
return super .setTransportOptions (transportOptions );
99
113
}
100
114
115
+ /**
116
+ * Sets the {@link TransportChannelProvider} to use with this Datastore client.
117
+ *
118
+ * @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport
119
+ * provider for this client.
120
+ */
121
+ public Builder setChannelProvider (TransportChannelProvider channelProvider ) {
122
+ this .channelProvider = validateChannelProvider (channelProvider );
123
+ return this ;
124
+ }
125
+
126
+ /**
127
+ * Sets the {@link CredentialsProvider} to use with this Datastore client.
128
+ *
129
+ * @param credentialsProvider A CredentialsProvider object that defines the credential provider
130
+ * for this client.
131
+ */
132
+ public Builder setCredentialsProvider (CredentialsProvider credentialsProvider ) {
133
+ this .credentialsProvider = credentialsProvider ;
134
+ return this ;
135
+ }
136
+
101
137
@ Override
102
138
public DatastoreOptions build () {
103
139
return new DatastoreOptions (this );
@@ -115,10 +151,45 @@ public Builder setDatabaseId(String databaseId) {
115
151
}
116
152
}
117
153
154
+ private static TransportChannelProvider validateChannelProvider (
155
+ TransportChannelProvider channelProvider ) {
156
+ if (!(channelProvider instanceof InstantiatingGrpcChannelProvider )) {
157
+ throw new IllegalArgumentException (
158
+ "Only GRPC channels are allowed for " + API_SHORT_NAME + "." );
159
+ }
160
+ return channelProvider ;
161
+ }
162
+
118
163
private DatastoreOptions (Builder builder ) {
119
164
super (DatastoreFactory .class , DatastoreRpcFactory .class , builder , new DatastoreDefaults ());
120
165
namespace = MoreObjects .firstNonNull (builder .namespace , defaultNamespace ());
121
166
databaseId = MoreObjects .firstNonNull (builder .databaseId , DEFAULT_DATABASE_ID );
167
+
168
+ // todo see if we can update this
169
+ if (getTransportOptions () instanceof HttpTransportOptions
170
+ && (builder .channelProvider != null || builder .credentialsProvider != null )) {
171
+ throw new IllegalArgumentException (
172
+ "Only gRPC transport allows setting of channel provider or credentials provider" );
173
+ } else if (getTransportOptions () instanceof GrpcTransportOptions ) {
174
+ this .channelProvider =
175
+ builder .channelProvider != null
176
+ ? builder .channelProvider
177
+ : GrpcTransportOptions .setUpChannelProvider (
178
+ DatastoreSettings .defaultGrpcTransportProviderBuilder (), this );
179
+
180
+ this .credentialsProvider =
181
+ builder .credentialsProvider != null
182
+ ? builder .credentialsProvider
183
+ : GrpcTransportOptions .setUpCredentialsProvider (this );
184
+ }
185
+ }
186
+
187
+ public CredentialsProvider getCredentialsProvider () {
188
+ return credentialsProvider ;
189
+ }
190
+
191
+ public TransportChannelProvider getTransportChannelProvider () {
192
+ return channelProvider ;
122
193
}
123
194
124
195
@ Override
@@ -134,6 +205,7 @@ protected String getDefaultProject() {
134
205
}
135
206
136
207
private static class DatastoreDefaults implements ServiceDefaults <Datastore , DatastoreOptions > {
208
+ private final TransportOptions TRANSPORT_OPTIONS = getDefaultTransportOptionsBuilder ().build ();
137
209
138
210
@ Override
139
211
public DatastoreFactory getDefaultServiceFactory () {
@@ -147,7 +219,11 @@ public DatastoreRpcFactory getDefaultRpcFactory() {
147
219
148
220
@ Override
149
221
public TransportOptions getDefaultTransportOptions () {
150
- return getDefaultGrpcTransportOptions ();
222
+ return TRANSPORT_OPTIONS ;
223
+ }
224
+
225
+ public static GrpcTransportOptions .Builder getDefaultTransportOptionsBuilder () {
226
+ return GrpcTransportOptions .newBuilder ();
151
227
}
152
228
}
153
229
0 commit comments