1919import static com .google .cloud .datastore .Validator .validateNamespace ;
2020
2121import com .google .api .core .BetaApi ;
22- import com .google .api .core .ObsoleteApi ;
2322import com .google .api .gax .grpc .ChannelPoolSettings ;
2423import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
2524import com .google .api .gax .rpc .TransportChannelProvider ;
3534import com .google .cloud .grpc .GrpcTransportOptions ;
3635import com .google .cloud .http .HttpTransportOptions ;
3736import com .google .common .base .MoreObjects ;
37+ import com .google .common .base .Preconditions ;
3838import com .google .common .collect .ImmutableSet ;
3939import java .io .IOException ;
4040import java .lang .reflect .Method ;
4141import java .util .Objects ;
4242import java .util .Set ;
43+ import java .util .logging .Logger ;
4344import javax .annotation .Nonnull ;
4445import javax .annotation .Nullable ;
4546
4647public class DatastoreOptions extends ServiceOptions <Datastore , DatastoreOptions > {
4748
4849 private static final long serialVersionUID = -1018382430058137336L ;
4950 private static final String API_SHORT_NAME = "Datastore" ;
51+ private static final Logger logger = Logger .getLogger (DatastoreOptions .class .getName ());
5052 private static final String DATASTORE_SCOPE = "https://www.googleapis.com/auth/datastore" ;
5153 private static final Set <String > SCOPES = ImmutableSet .of (DATASTORE_SCOPE );
5254 private static final String DEFAULT_DATABASE_ID = "" ;
@@ -72,9 +74,7 @@ public class DatastoreOptions extends ServiceOptions<Datastore, DatastoreOptions
7274 /**
7375 * @deprecated This constant is obsolete and will be removed in a future version.
7476 */
75- @ ObsoleteApi ("This constant is obsolete and will be removed in a future version." )
76- @ Deprecated
77- public static final int MAX_CHANNEL_COUNT = 10 ;
77+ @ Deprecated public static final int MAX_CHANNEL_COUNT = 10 ;
7878
7979 private transient TransportChannelProvider channelProvider = null ;
8080
@@ -129,7 +129,10 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
129129 private String databaseId ;
130130 private TransportChannelProvider channelProvider = null ;
131131 private String host ;
132- private TransportOptions transportOptions ;
132+
133+ @ Nonnull
134+ private TransportOptions transportOptions =
135+ new DatastoreDefaults ().getDefaultTransportOptions ();
133136
134137 @ Nullable private DatastoreOpenTelemetryOptions openTelemetryOptions = null ;
135138
@@ -141,25 +144,51 @@ private Builder(DatastoreOptions options) {
141144 this .databaseId = options .databaseId ;
142145 this .openTelemetryOptions = options .openTelemetryOptions ;
143146 this .channelProvider = validateChannelProvider (options .channelProvider );
147+ this .host = options .getHost ();
148+ this .transportOptions = options .getTransportOptions ();
149+ }
150+
151+ private TransportChannelProvider validateChannelProvider (
152+ TransportChannelProvider channelProvider ) {
153+ Preconditions .checkNotNull (channelProvider , "TransportChannelProvider cannot be null" );
154+ if (!(channelProvider instanceof InstantiatingGrpcChannelProvider )) {
155+ throw new IllegalArgumentException (
156+ "Only GRPC channels are allowed for " + API_SHORT_NAME + "." );
157+ }
158+ return channelProvider ;
144159 }
145160
161+ /**
162+ * Sets the transport options.
163+ *
164+ * @param transportOptions the transport options to set, must be {@link HttpTransportOptions} or
165+ * {@link GrpcTransportOptions}
166+ * @return the builder
167+ * @throws IllegalArgumentException if the transport options are not supported
168+ */
146169 @ Override
147- public Builder setTransportOptions (TransportOptions transportOptions ) {
148- if (!(transportOptions instanceof HttpTransportOptions )) {
170+ public Builder setTransportOptions (@ Nonnull TransportOptions transportOptions ) {
171+ Preconditions .checkNotNull (transportOptions , "TransportOptions cannot be null" );
172+ if (!(transportOptions instanceof HttpTransportOptions )
173+ && !(transportOptions instanceof GrpcTransportOptions )) {
149174 throw new IllegalArgumentException (
150- "Only http transport is allowed for " + API_SHORT_NAME + "." );
175+ "Only http and grpc transport are allowed for " + API_SHORT_NAME + "." );
151176 }
152177 this .transportOptions = transportOptions ;
153178 return super .setTransportOptions (transportOptions );
154179 }
155180
156181 /**
157- * Sets the transport to gRPC. Note this functionality is experimental and subject to change.
182+ * This method deprecated. Prefer to use {@link #setTransportOptions(TransportOptions)} instead.
183+ * When using the transport-neutral variant, you may need to cast to TransportOptions when using
184+ * a GrpcTransportOptions class, otherwise it will default to the deprecated method.
185+ *
186+ * <p>Sets the transport to gRPC. Note this functionality is experimental and subject to change.
158187 */
188+ @ Deprecated
159189 @ BetaApi
160190 public Builder setTransportOptions (GrpcTransportOptions transportOptions ) {
161- this .transportOptions = transportOptions ;
162- return super .setTransportOptions (transportOptions );
191+ return setTransportOptions ((TransportOptions ) transportOptions );
163192 }
164193
165194 @ Override
@@ -185,10 +214,28 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
185214 return this ;
186215 }
187216
217+ /**
218+ * Builds the {@link DatastoreOptions} instance.
219+ *
220+ * <p>If the host is not explicitly set, it defaults to the transport-specific default host:
221+ *
222+ * <ul>
223+ * <li>gRPC: {@code datastore.googleapis.com:443}
224+ * <li>HTTP: {@code https://datastore.googleapis.com}
225+ * </ul>
226+ *
227+ * @return the {@link DatastoreOptions} instance
228+ */
188229 @ Override
189230 public DatastoreOptions build () {
190- if (this .host == null && this .transportOptions instanceof GrpcTransportOptions ) {
191- this .setHost (DatastoreSettings .getDefaultEndpoint ());
231+ if (this .host == null ) {
232+ // Use whatever host value the user passes in, otherwise use the transport specific default
233+ // host values
234+ if (this .transportOptions instanceof GrpcTransportOptions ) {
235+ this .setHost (DatastoreSettings .getDefaultEndpoint ());
236+ } else if (this .transportOptions instanceof HttpTransportOptions ) {
237+ this .setHost (com .google .datastore .v1 .client .DatastoreFactory .DEFAULT_HOST );
238+ }
192239 }
193240 return new DatastoreOptions (this );
194241 }
@@ -218,15 +265,6 @@ public Builder setOpenTelemetryOptions(
218265 }
219266 }
220267
221- private static TransportChannelProvider validateChannelProvider (
222- TransportChannelProvider channelProvider ) {
223- if (channelProvider != null && !(channelProvider instanceof InstantiatingGrpcChannelProvider )) {
224- throw new IllegalArgumentException (
225- "Only GRPC channels are allowed for " + API_SHORT_NAME + "." );
226- }
227- return channelProvider ;
228- }
229-
230268 private DatastoreOptions (Builder builder ) {
231269 super (DatastoreFactory .class , DatastoreRpcFactory .class , builder , new DatastoreDefaults ());
232270
@@ -239,9 +277,9 @@ private DatastoreOptions(Builder builder) {
239277 namespace = MoreObjects .firstNonNull (builder .namespace , defaultNamespace ());
240278 databaseId = MoreObjects .firstNonNull (builder .databaseId , DEFAULT_DATABASE_ID );
241279
280+ // ChannelProvider is used by GAX but HttpJson does not use it so we safely ignore it.
242281 if (getTransportOptions () instanceof HttpTransportOptions && builder .channelProvider != null ) {
243- throw new IllegalArgumentException (
244- "Only gRPC transport allows setting of channel provider or credentials provider" );
282+ logger .warning ("Channel provider is ignored for HttpJson transport." );
245283 } else if (getTransportOptions () instanceof GrpcTransportOptions ) {
246284 if (builder .channelProvider == null ) {
247285 // Set the default gRPC connection pool to be configured with a minimum of 1 channel.
@@ -310,8 +348,8 @@ public TransportOptions getDefaultTransportOptions() {
310348 return TRANSPORT_OPTIONS ;
311349 }
312350
313- public static HttpTransportOptions .Builder getDefaultTransportOptionsBuilder () {
314- return HttpTransportOptions .newBuilder ();
351+ public static GrpcTransportOptions .Builder getDefaultTransportOptionsBuilder () {
352+ return GrpcTransportOptions .newBuilder ();
315353 }
316354 }
317355
0 commit comments