Detecting Code Execution on Google App Engine (GAE/J)

Post on the Stringbuffer.com gives an example for determining GAE/J environment using ServletContext:

1
boolean onGoogleAppEngine = getServletContext().getServerInfo().startsWith("Google App Engine");

This is fine if you can access servlet context but what if you need to detect GAE/J in a part of application that has no [easy way to] access to it? It can be done simply checking package name of the current Java SecurityManager:

1
2
3
4
SecurityManager sm = System.getSecurityManager();
boolean onGoogleAppEngine = sm != null && (
                sm.getClass().getName().startsWith("com.google.appengine.") ||
                sm.getClass().getName().startsWith("com.google.apphosting."));

I’ve found that SecurityManager implementation under GAE/J development server is **com.google.appengine.**tools.development.DevAppServerFactory$CustomSecurityManager while when it’s deployed on App Engine it’s **com.google.apphosting.**utils.security.SecurityManagerInstaller$CustomSecurityManager

Comments