Lift properties improved

Lift web framework uses a sophisticated file-based strategy for application properties, based on modes/hosts and file naming conventions. While very handy, this could be unpractical on a CI environment where typically one doesn’t want to mess with the filesystem (think CloudBees), and doesn’t want to store sensitive properties (such as user credentials) into SCM.

The problem is well known, and a few strategies exists.

Cloudbees, for instance has a file system workaround and supports various types of parameters.

For me, using system environment properties (one could use environment properties as well) is simple and straightforward, and keeps a reasonably high level of security. Sure, if you have a lot of them (maybe API keys) configuring the application container can be tedious, but IMO is worth the effort.

The following is a simple Property object which chains a system variable into a Box and defaults to standard lift Props. The logic here is that, if you set a system property, that should take precedence. Another discussion of the problem here.

package code
package config

import net.liftweb._
import common._
import util._
import Helpers._

object Property {

  /**
   * Chain system property and Lift property
   * @param name key for the property to get
   * @return the value of the property if defined
   */
  def get(name: String): Box[String] = sys.props.get(name) orElse Props.get(name)

  def getInt(name: String): Box[Int] = get(name).map(toInt)
  def getInt(name: String, defVal: Int): Int = getInt(name) openOr defVal
  def getLong(name: String): Box[Long] = get(name).flatMap(asLong)
  def getLong(name: String, defVal: Long): Long = getLong(name) openOr defVal
  def getBool(name: String): Box[Boolean] = get(name).map(toBoolean)
  def getBool(name: String, defVal: Boolean): Boolean = getBool(name) openOr defVal
  def get(name: String, defVal: String): String = get(name) openOr defVal

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *