17
17
package com .google .gcloud ;
18
18
19
19
import static com .google .common .base .MoreObjects .firstNonNull ;
20
- import static com .google .common .base .Preconditions .checkNotNull ;
20
+ import static com .google .common .base .Preconditions .checkArgument ;
21
21
import static java .nio .charset .StandardCharsets .UTF_8 ;
22
22
23
23
import com .google .api .client .extensions .appengine .http .UrlFetchTransport ;
@@ -306,7 +306,13 @@ public B readTimeout(int readTimeout) {
306
306
protected ServiceOptions (Class <? extends ServiceFactory <ServiceT , OptionsT >> serviceFactoryClass ,
307
307
Class <? extends ServiceRpcFactory <ServiceRpcT , OptionsT >> rpcFactoryClass ,
308
308
Builder <ServiceT , ServiceRpcT , OptionsT , ?> builder ) {
309
- projectId = checkNotNull (builder .projectId != null ? builder .projectId : defaultProject ());
309
+ projectId = builder .projectId != null ? builder .projectId : defaultProject ();
310
+ if (projectIdRequired ()) {
311
+ checkArgument (
312
+ projectId != null ,
313
+ "A project ID is required for this service but could not be determined from the builder or "
314
+ + "the environment. Please set a project ID using the builder." );
315
+ }
310
316
host = firstNonNull (builder .host , defaultHost ());
311
317
httpTransportFactory = firstNonNull (builder .httpTransportFactory ,
312
318
getFromServiceLoader (HttpTransportFactory .class , DefaultHttpTransportFactory .INSTANCE ));
@@ -325,6 +331,16 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
325
331
clock = firstNonNull (builder .clock , Clock .defaultClock ());
326
332
}
327
333
334
+ /**
335
+ * Returns whether a service requires a project ID. This method may be overridden in
336
+ * service-specific Options objects.
337
+ *
338
+ * @return true if a project ID is required to use the service, false if not.
339
+ */
340
+ protected boolean projectIdRequired () {
341
+ return true ;
342
+ }
343
+
328
344
private static AuthCredentials defaultAuthCredentials () {
329
345
// Consider App Engine. This will not be needed once issue #21 is fixed.
330
346
if (appEngineAppId () != null ) {
@@ -462,6 +478,8 @@ public ServiceRpcT rpc() {
462
478
463
479
/**
464
480
* Returns the project id.
481
+ *
482
+ * Return value can be null (for services that don't require a project id).
465
483
*/
466
484
public String projectId () {
467
485
return projectId ;
0 commit comments