Search code examples
google-app-engineclassloaderblobstore

detect AppEngine vs basic server


I am developing a web app to run on either Google's AppEngine or a basic server with file storage (it may not stay that way but that's the current status).

How do I detect whether the AppEngine services (most importantly blobstore) are available at runtime?

I have tried using code like the following:

    try{
        Class.forName( "com.google.appengine.api.blobstore.BlobstoreServiceFactory" );
        logger.info( "Using GAE blobstore backend" );
        return new GAEBlobService();
    }catch( ClassNotFoundException e ){
        logger.info( "Using filesystem-based backend" );
        return new FileBlobService();
    }

but it doesn't work because BlobstoreServiceFactory is available at compile time. What fails if trying to use GAE's blobstore without a GAE server is the following:

com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'blobstore' or call 'CreateUploadURL()' was not found.

Solution

  • There's a few things you can use.

    You can check the runtime environment to check the running version of App Engine. Check the section about "The Environment" in the runtime docs: https://developers.google.com/appengine/docs/java/runtime

    You could also do what you were doing, and attempt to make an call that uses the SDK API functions (instead of just checking for the existence of a class) and catch the exception. This may negatively impact performance since you're making an extra RPC.

    You could check request headers for GAE specific request headers too.