views:

201

answers:

4

Hello there,

I'd like to do this in Java Google App Engine

if(developmentMode)
  foo();
else
  bar();

Does anyone know a good way to do this?

Daniel

+3  A: 

In Python, check the SERVER_SOFTWARE environment variable. It'll be "Development/X.Y" in development mode. In Java, ServletContext.getServerInfo().

Bob Aman
A: 

Take a look at this thread on the GAE/J Group.

Several techniques are listed there. You might also look at this blog entry

It suggests doing: ServletContext.getServerInfo()

"In development this will be 'Google App Engine Development/x.x.x' and in production it will be 'Google App Engine/x.x.x'"

This blog suggests writing a ServletContextListener to sniff this value so you can expose it to classes that don't have access to the ServletContext.

James Cooper
A: 

You can have a init-parameter in web.xml. e.g devmod. Read this parameter on a startup servlet and put in a boolean static variable. e.g. CONSTANTS.DEVMOD Whenever you deploy your app to GAE, make sure you have change the value of devmod to false. In your code use your own

if(CONSTANTS.DEVMOD)
  foo();
else
  bar();

This is the technique which I am going to use for my GAE application.

Hope it helps.

Tahir Akram
+2  A: 

http://code.google.com/appengine/docs/java/runtime.html#The_Environment

In Java just test

SystemProperty.environment.value() == SystemProperty.Environment.Value.Production

Marc Hacker
Awesome, and it was documented :)
supercobra