Skip to content

Commit ed1d437

Browse files
committed
Merge pull request #346 from ajkannan/project-id-optional
Allow services to avoid setting project ID
2 parents 98e9c03 + 6c40825 commit ed1d437

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.google.gcloud;
1818

1919
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;
2121
import static java.nio.charset.StandardCharsets.UTF_8;
2222

2323
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
@@ -306,7 +306,13 @@ public B readTimeout(int readTimeout) {
306306
protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> serviceFactoryClass,
307307
Class<? extends ServiceRpcFactory<ServiceRpcT, OptionsT>> rpcFactoryClass,
308308
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+
}
310316
host = firstNonNull(builder.host, defaultHost());
311317
httpTransportFactory = firstNonNull(builder.httpTransportFactory,
312318
getFromServiceLoader(HttpTransportFactory.class, DefaultHttpTransportFactory.INSTANCE));
@@ -325,6 +331,16 @@ protected ServiceOptions(Class<? extends ServiceFactory<ServiceT, OptionsT>> ser
325331
clock = firstNonNull(builder.clock, Clock.defaultClock());
326332
}
327333

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+
328344
private static AuthCredentials defaultAuthCredentials() {
329345
// Consider App Engine. This will not be needed once issue #21 is fixed.
330346
if (appEngineAppId() != null) {
@@ -462,6 +478,8 @@ public ServiceRpcT rpc() {
462478

463479
/**
464480
* Returns the project id.
481+
*
482+
* Return value can be null (for services that don't require a project id).
465483
*/
466484
public String projectId() {
467485
return projectId;

0 commit comments

Comments
 (0)