@@ -171,7 +171,8 @@ public B setClock(Clock clock) {
171171 }
172172
173173 /**
174- * Sets project id.
174+ * Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
175+ * attempt getting the project ID from the environment.
175176 *
176177 * @return the builder
177178 */
@@ -181,7 +182,8 @@ public B projectId(String projectId) {
181182 }
182183
183184 /**
184- * Sets project id.
185+ * Sets the project ID. If no project ID is set, {@link #getDefaultProjectId()} will be used to
186+ * attempt getting the project ID from the environment.
185187 *
186188 * @return the builder
187189 */
@@ -311,10 +313,6 @@ private static GoogleCredentials defaultCredentials() {
311313 }
312314 }
313315
314- protected static String appEngineAppId () {
315- return System .getProperty ("com.google.appengine.application.id" );
316- }
317-
318316 @ Deprecated
319317 protected String defaultHost () {
320318 return getDefaultHost ();
@@ -330,21 +328,41 @@ protected String defaultProject() {
330328 }
331329
332330 protected String getDefaultProject () {
331+ return getDefaultProjectId ();
332+ }
333+
334+ /**
335+ * Returns the default project ID, or {@code null} if no default project ID could be found. This
336+ * method returns the first available project ID among the following sources:
337+ * <ol>
338+ * <li>The project ID specified by the GOOGLE_CLOUD_PROJECT environment variable
339+ * <li>The App Engine project ID
340+ * <li>The project ID specified in the JSON credentials file pointed by the
341+ * {@code GOOGLE_APPLICATION_CREDENTIALS} environment variable
342+ * <li>The Google Cloud SDK project ID
343+ * <li>The Compute Engine project ID
344+ * </ol>
345+ */
346+ public static String getDefaultProjectId () {
333347 String projectId = System .getProperty (PROJECT_ENV_NAME , System .getenv (PROJECT_ENV_NAME ));
334348 if (projectId == null ) {
335349 projectId =
336350 System .getProperty (LEGACY_PROJECT_ENV_NAME , System .getenv (LEGACY_PROJECT_ENV_NAME ));
337351 }
338352 if (projectId == null ) {
339- projectId = appEngineProjectId ();
353+ projectId = getAppEngineProjectId ();
340354 }
341355 if (projectId == null ) {
342- projectId = serviceAccountProjectId ();
356+ projectId = getServiceAccountProjectId ();
343357 }
344- return projectId != null ? projectId : googleCloudProjectId ();
358+ return projectId != null ? projectId : getGoogleCloudProjectId ();
359+ }
360+
361+ protected static String getAppEngineAppId () {
362+ return System .getProperty ("com.google.appengine.application.id" );
345363 }
346364
347- private static String activeGoogleCloudConfig (File configDir ) {
365+ private static String getActiveGoogleCloudConfig (File configDir ) {
348366 String activeGoogleCloudConfig = null ;
349367 try {
350368 activeGoogleCloudConfig =
@@ -356,7 +374,7 @@ private static String activeGoogleCloudConfig(File configDir) {
356374 return firstNonNull (activeGoogleCloudConfig , "default" );
357375 }
358376
359- protected static String googleCloudProjectId () {
377+ protected static String getGoogleCloudProjectId () {
360378 File configDir ;
361379 if (System .getenv ().containsKey ("CLOUDSDK_CONFIG" )) {
362380 configDir = new File (System .getenv ("CLOUDSDK_CONFIG" ));
@@ -365,7 +383,7 @@ protected static String googleCloudProjectId() {
365383 } else {
366384 configDir = new File (System .getProperty ("user.home" ), ".config/gcloud" );
367385 }
368- String activeConfig = activeGoogleCloudConfig (configDir );
386+ String activeConfig = getActiveGoogleCloudConfig (configDir );
369387 FileReader fileReader = null ;
370388 try {
371389 fileReader = new FileReader (new File (configDir , "configurations/config_" + activeConfig ));
@@ -422,7 +440,7 @@ private static boolean isWindows() {
422440 return System .getProperty ("os.name" ).toLowerCase (Locale .ENGLISH ).contains ("windows" );
423441 }
424442
425- protected static String appEngineProjectId () {
443+ protected static String getAppEngineProjectId () {
426444 try {
427445 Class <?> factoryClass =
428446 Class .forName ("com.google.appengine.api.appidentity.AppIdentityServiceFactory" );
@@ -440,7 +458,7 @@ protected static String appEngineProjectId() {
440458 }
441459 }
442460
443- protected static String serviceAccountProjectId () {
461+ protected static String getServiceAccountProjectId () {
444462 String project = null ;
445463 String credentialsPath = System .getenv ("GOOGLE_APPLICATION_CREDENTIALS" );
446464 if (credentialsPath != null ) {
@@ -481,17 +499,17 @@ public ServiceRpcT getRpc() {
481499 }
482500
483501 /**
484- * Returns the project id . Return value can be null (for services that don't require a project
485- * id ).
502+ * Returns the project ID . Return value can be null (for services that don't require a project
503+ * ID ).
486504 */
487505 @ Deprecated
488506 public String projectId () {
489507 return getProjectId ();
490508 }
491509
492510 /**
493- * Returns the project id . Return value can be null (for services that don't require a project
494- * id ).
511+ * Returns the project ID . Return value can be null (for services that don't require a project
512+ * ID ).
495513 */
496514 public String getProjectId () {
497515 return projectId ;
0 commit comments