Sunday, April 12, 2009

Determining if your code is executing in the GAE/J environment/sandbox

When moving your apps to the GAE/J platform you might be required to do some tweaking to make your application comply with the restricted sandbox environment of the Google App Engine (no write access to disk, read access limited to "user.dir", no thread creation, etc.).

Here is a quick way to determine if your code is executing in the GAE/J environment:
boolean onGoogleAppEngine = getServletContext().getServerInfo().startsWith("Google App Engine");
getServerInfo() currently returns "Google App Engine/1.2.0" when running in the live GAE/J environment. When running in local development mode it returns "Google App Engine Development/1.2.0".

Please comment if you know of any better/cleaner way to determine if your code is executing in the GAE/J sandbox.

4 comments:

  1. Wow, I hope people use this as a temporary ugly hack or GAE/j is really breaking WORA.
    ReplyDelete
  2. Alexis: The check is meant to be used for debugging GAE/J compatibility issues. If possible using such a check should be avoided.
    ReplyDelete
  3. I think there's a small typo in your code example. You're missing a "/" at the end of "Google App Engine". As written, your function will always return true.

    It should read:

    getServletContext().getServerInfo().startsWith("Google App Engine/");
    ReplyDelete
  4. Much better..
    //GET this printed once!
    static final String YOUR_HOME_DIR ="/home/username/grails......../stage";
    String userDir = System.getProperty("user.dir");

    if(YOUR_HOME_DIR.equals(userDir)){
    //DEv Env
    }else{
    //Live Env
    }
    ReplyDelete