1

I there a way to access the isDevelopmentMode value from outside the ScalatraServlet? It seems to come from the ScalatraKernel trait, so would including that be a feasible option?

What I'm trying to achieve is to make the DI configuration know whether I'm on development mode or in production to be able to configure database settings. I'm currently using Subcut for DI.

hleinone
  • 4,470
  • 4
  • 35
  • 49

1 Answers1

3

I set the development mode in the scalatra servlet to a config/system property value

object AppEnv {
  private val cloader = getClass.getClassLoader

  val PRODUCTION = "production".intern
  val DEVELOPMENT = "development".intern
  val STAGING = "staging".intern
  val TEST = "test".intern

  lazy val environment = readEnvironmentKey(println _)

  def isProduction = isEnvironment(PRODUCTION)
  def isDevelopment = isEnvironment(DEVELOPMENT)
  def isStaging = isEnvironment(STAGING)
  def isTest = isEnvironment(TEST)
  def isEnvironment(env: String) = environment.toLowerCase(Locale.ENGLISH) == env.toLowerCase(Locale.ENGLISH)

  def readEnvironmentKey(failWith: String ⇒ Unit = _ ⇒ null) = {
    (ep("BACKCHAT_MODE") orElse sp("backchat.mode")) getOrElse {
      val inferred = "development"
      failWith("no environment found, defaulting to: " + inferred)
      inferred
    }
  }

  private def sp(key: String) = {
    val s = System.getProperty(key)
    if (s != null && s.trim.nonEmpty) Some(s) else None
  }
  private def ep(key: String) = {
    val s = System.getenv(key)
    if (s != null && s.trim.nonEmpty) Some(s) else None
  }
}

trait ApplicationModes extends ScalatraKernel {
  override def environment: String = AppEnv.environment
  override def isDevelopmentMode = environment != AppEnv.PRODUCTION && environment != AppEnv.STAGING
}

Scalatra also allows you to use System property to set that mode, you can possibly read that property out and get similar results. -Dorg.scalatra.environment=development

Casual Jim
  • 1,179
  • 7
  • 13