Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Play 2.2.x mode prior to start of Application using Java

I want to point my play application to a particular application config file based on the environment it is running in. There are three and they correspond to the standard Play states:

  • application.dev.conf
  • application.test.conf
  • application.prod.conf

A co-worker shared a method for doing this which requires setting an OS environment variable.

I'd like to eliminate the need to set an OS variable. My preference is the use whatever Play uses at startup to know what mode it is in.

For example, if you execute play run from the command line, part of the output is "[info] play - Application started (Dev)"

I want to use this information in my Global.java where I override onLoadConfig like so:

public Configuration onLoadConfig(Configuration baseConfiguration, File f, ClassLoader loader) {
    String playEnv=<some static method to get mode>;        
    Config additionalConfig = ConfigFactory.parseFile(new File(f,"conf/application."+playEnv+".conf"));
    Config baseConfig = baseConfiguration.getWrappedConfiguration().underlying();
    return new Configuration(baseConfig.withFallback(additionalConfig));
}

Everything that I find is how to do this after the application has been started i.e. using isDev(), isTest(), isProd().

Is there static method that provides the mode while I am overriding onLoadConfig in Global.java?

like image 881
Jerry Mindek Avatar asked Oct 24 '13 17:10

Jerry Mindek


2 Answers

I think play run is dev, play start is prod.

EDIT: If you're looking to see what the current mode is, it's passed in through play.api.Play.current:

Play.current.mode match {
  case Play.Mode.Dev => "dev"
  case Play.Mode.Prod => "prod"
}
like image 84
Will Sargent Avatar answered Oct 05 '22 02:10

Will Sargent


The issue appeared to be addressed in the latest Play (3.0.0). There is another onLoadConfig method added to Global witch has a mode: {Dev,Prod,Test} parameter.

public Configuration onLoadConfig(Configuration config, File path, ClassLoader classloader, Mode mode) {
    return onLoadConfig(config, path, classloader); // default implementation
}
like image 42
AlexV Avatar answered Oct 05 '22 04:10

AlexV