|
20 | 20 | import static com.google.common.base.Preconditions.checkArgument; |
21 | 21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | 22 |
|
| 23 | +import com.google.api.client.http.GenericUrl; |
| 24 | +import com.google.api.client.http.HttpHeaders; |
| 25 | +import com.google.api.client.http.HttpRequest; |
| 26 | +import com.google.api.client.http.HttpRequestFactory; |
| 27 | +import com.google.api.client.http.HttpResponse; |
| 28 | +import com.google.api.client.http.HttpTransport; |
| 29 | +import com.google.api.client.http.javanet.NetHttpTransport; |
23 | 30 | import com.google.api.core.ApiClock; |
24 | 31 | import com.google.api.core.CurrentMillisClock; |
25 | 32 | import com.google.api.core.InternalApi; |
|
39 | 46 | import java.io.InputStream; |
40 | 47 | import java.io.ObjectInputStream; |
41 | 48 | import java.io.Serializable; |
42 | | -import java.lang.reflect.Method; |
43 | 49 | import java.nio.charset.Charset; |
44 | 50 | import java.util.Locale; |
45 | 51 | import java.util.Objects; |
@@ -381,30 +387,51 @@ private static boolean isWindows() { |
381 | 387 | } |
382 | 388 |
|
383 | 389 | protected static String getAppEngineProjectId() { |
384 | | - try { |
385 | | - Class<?> factoryClass = |
386 | | - Class.forName("com.google.appengine.api.appidentity.AppIdentityServiceFactory"); |
387 | | - Class<?> serviceClass = |
388 | | - Class.forName("com.google.appengine.api.appidentity.AppIdentityService"); |
389 | | - Method method = factoryClass.getMethod("getAppIdentityService"); |
390 | | - Object appIdentityService = method.invoke(null); |
391 | | - method = serviceClass.getMethod("getServiceAccountName"); |
392 | | - String serviceAccountName = (String) method.invoke(appIdentityService); |
393 | | - int indexOfAtSign = serviceAccountName.indexOf('@'); |
394 | | - return serviceAccountName.substring(0, indexOfAtSign); |
395 | | - } catch (ClassNotFoundException exception) { |
396 | | - if (System.getProperty("com.google.appengine.runtime.version") != null) { |
397 | | - // Could not resolve appengine classes under GAE environment. |
398 | | - throw new RuntimeException("Google App Engine runtime detected " |
399 | | - + "(the environment variable \"com.google.appengine.runtime.version\" is set), " |
400 | | - + "but unable to resolve appengine-sdk classes. " |
401 | | - + "For more details see " |
402 | | - + "https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/APPENGINE.md"); |
| 390 | + String projectId = null; |
| 391 | + if (PlatformInformation.isOnGAEStandard7()) { |
| 392 | + projectId = getAppEngineProjectIdFromAppId(); |
| 393 | + } else { |
| 394 | + //for GAE flex and standard Java 8 environment |
| 395 | + projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 396 | + if (projectId == null) { |
| 397 | + projectId = System.getenv("GCLOUD_PROJECT"); |
| 398 | + } |
| 399 | + if (projectId == null) { |
| 400 | + projectId = getAppEngineProjectIdFromAppId(); |
| 401 | + } |
| 402 | + if (projectId == null) { |
| 403 | + try { |
| 404 | + projectId = getAppEngineProjectIdFromMetadataServer(); |
| 405 | + } catch (IOException ignore) { |
| 406 | + projectId = null; |
| 407 | + } |
403 | 408 | } |
404 | | - return null; |
405 | | - } catch (Exception ignore) { |
406 | | - return null; |
407 | 409 | } |
| 410 | + return projectId; |
| 411 | + } |
| 412 | + |
| 413 | + protected static String getAppEngineProjectIdFromAppId() { |
| 414 | + String projectId = getAppEngineAppId(); |
| 415 | + if (projectId != null && projectId.contains(":")) { |
| 416 | + int colonIndex = projectId.indexOf(":"); |
| 417 | + projectId = projectId.substring(colonIndex + 1); |
| 418 | + } |
| 419 | + return projectId; |
| 420 | + } |
| 421 | + |
| 422 | + private static String getAppEngineProjectIdFromMetadataServer() throws IOException { |
| 423 | + String metadata = "http://metadata.google.internal"; |
| 424 | + String projectIdURL = "/computeMetadata/v1/project/project-id"; |
| 425 | + GenericUrl url = new GenericUrl(metadata + projectIdURL); |
| 426 | + |
| 427 | + HttpTransport netHttpTransport = new NetHttpTransport(); |
| 428 | + HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory(); |
| 429 | + HttpRequest request = requestFactory.buildGetRequest(url) |
| 430 | + .setConnectTimeout(500) |
| 431 | + .setReadTimeout(500) |
| 432 | + .setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google")); |
| 433 | + HttpResponse response = request.execute(); |
| 434 | + return response.parseAsString(); |
408 | 435 | } |
409 | 436 |
|
410 | 437 | protected static String getServiceAccountProjectId() { |
|
0 commit comments