Skip to content

Commit cc651a7

Browse files
mziccardaozarov
authored andcommitted
---
yaml --- r: 4759 b: refs/heads/logging-alpha c: d17e325 h: refs/heads/master i: 4757: 033a20f 4755: bae75a3 4751: b392773
1 parent 9b18c13 commit cc651a7

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ refs/heads/compute-alpha: 969cba2627f1d53d352cc4a5ffe0879dacf65e6c
1212
refs/heads/dns-alpha: 2f90e7e338349287ace33375896907af0f032ca1
1313
refs/heads/dns-alpha-batch: 17442b07867021b85d0452f5f3eda29a3413288f
1414
refs/heads/gcs-nio: 283aeaf15efdcf3621eb6859f05e55ad7764375d
15-
refs/heads/logging-alpha: 59cdf37fed705df06669611a79487faf8d1af8f7
15+
refs/heads/logging-alpha: d17e3253afaafaa9441df140d95fbca86a4051ef
1616
refs/tags/v0.1.0: a615317f7424ed58621b1f65d5c4d8cbbe8a6ed8
1717
refs/tags/v0.1.1: 7a7f6985fe465e9dd6a075af55493f42b4933be0
1818
refs/tags/v0.1.2: 3eb3fe866ba22487686048f45d927b8c8638ea3f

branches/logging-alpha/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ Most `gcloud-java` libraries require a project ID. There are multiple ways to s
8484
1. Project ID supplied when building the service options
8585
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
8686
3. App Engine project ID
87-
4. Google Cloud SDK project ID
88-
5. Compute Engine project ID
87+
4. Project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable
88+
5. Google Cloud SDK project ID
89+
6. Compute Engine project ID
8990

9091
Authentication
9192
--------------

branches/logging-alpha/gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@
3030
import com.google.common.io.Files;
3131
import com.google.gcloud.spi.ServiceRpcFactory;
3232

33+
import org.json.JSONException;
34+
import org.json.JSONObject;
35+
import org.json.JSONTokener;
36+
3337
import java.io.BufferedReader;
3438
import java.io.File;
39+
import java.io.FileInputStream;
3540
import java.io.FileNotFoundException;
3641
import java.io.FileReader;
3742
import java.io.IOException;
@@ -378,7 +383,10 @@ protected String defaultHost() {
378383
protected String defaultProject() {
379384
String projectId = System.getProperty(PROJECT_ENV_NAME, System.getenv(PROJECT_ENV_NAME));
380385
if (projectId == null) {
381-
projectId = getAppEngineProjectId();
386+
projectId = appEngineProjectId();
387+
}
388+
if (projectId == null) {
389+
projectId = serviceAccountProjectId();
382390
}
383391
return projectId != null ? projectId : googleCloudProjectId();
384392
}
@@ -461,7 +469,7 @@ private static boolean isWindows() {
461469
return System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows");
462470
}
463471

464-
protected static String getAppEngineProjectId() {
472+
protected static String appEngineProjectId() {
465473
try {
466474
Class<?> factoryClass =
467475
Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory");
@@ -479,6 +487,20 @@ protected static String getAppEngineProjectId() {
479487
}
480488
}
481489

490+
protected static String serviceAccountProjectId() {
491+
String project = null;
492+
String credentialsPath = System.getenv("GOOGLE_APPLICATION_CREDENTIALS");
493+
if(credentialsPath != null) {
494+
try (InputStream credentialsStream = new FileInputStream(credentialsPath)) {
495+
JSONObject json = new JSONObject(new JSONTokener(credentialsStream));
496+
project = json.getString("project_id");
497+
} catch (IOException | JSONException ex) {
498+
// ignore
499+
}
500+
}
501+
return project;
502+
}
503+
482504
@SuppressWarnings("unchecked")
483505
public ServiceT service() {
484506
if (service == null) {

0 commit comments

Comments
 (0)