Skip to content

Commit

Permalink
Re-add PlatformSpecific.isAppEngine() for backwards-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Jacobs committed Sep 26, 2016
1 parent 04f33ad commit ddbf1bb
Showing 1 changed file with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,57 @@
import java.util.concurrent.ThreadFactory;

public class PlatformSpecific {
private final boolean isAppEngineStandardEnvironment;
private final Platform platform;

private enum Platform {
STANDARD, APPENGINE_STANDARD, APPENGINE_FLEXIBLE
}

private static PlatformSpecific INSTANCE = new PlatformSpecific();

private PlatformSpecific() {
isAppEngineStandardEnvironment = determineAppEngineReflectively();
platform = determinePlatformReflectively();
}

public static boolean isAppEngineStandardEnvironment() {
return INSTANCE.isAppEngineStandardEnvironment;
return INSTANCE.platform == Platform.APPENGINE_STANDARD;
}

public static boolean isAppEngine() {
return INSTANCE.platform == Platform.APPENGINE_FLEXIBLE || INSTANCE.platform == Platform.APPENGINE_STANDARD;
}

/*
* This detection mechanism is from Guava - specifically
* http://docs.guava-libraries.googlecode.com/git/javadoc/src-html/com/google/common/util/concurrent/MoreExecutors.html#line.766
* Added GAE_LONG_APP_ID check to detect only AppEngine Standard Environment
*/
private static boolean determineAppEngineReflectively() {
private static Platform determinePlatformReflectively() {
if (System.getProperty("com.google.appengine.runtime.environment") == null) {
return false;
return Platform.STANDARD;
}
// GAE_LONG_APP_ID is only set in the GAE Flexible Environment, where we want standard threading
if (System.getenv("GAE_LONG_APP_ID") != null) {
return false;
return Platform.APPENGINE_FLEXIBLE;
}
try {
// If the current environment is null, we're not inside AppEngine.
return Class.forName("com.google.apphosting.api.ApiProxy")
boolean isInsideAppengine = Class.forName("com.google.apphosting.api.ApiProxy")
.getMethod("getCurrentEnvironment")
.invoke(null) != null;
return isInsideAppengine ? Platform.APPENGINE_STANDARD : Platform.STANDARD;
} catch (ClassNotFoundException e) {
// If ApiProxy doesn't exist, we're not on AppEngine at all.
return false;
return Platform.STANDARD;
} catch (InvocationTargetException e) {
// If ApiProxy throws an exception, we're not in a proper AppEngine environment.
return false;
return Platform.STANDARD;
} catch (IllegalAccessException e) {
// If the method isn't accessible, we're not on a supported version of AppEngine;
return false;
return Platform.STANDARD;
} catch (NoSuchMethodException e) {
// If the method doesn't exist, we're not on a supported version of AppEngine;
return false;
return Platform.STANDARD;
}
}

Expand Down

0 comments on commit ddbf1bb

Please sign in to comment.