Play2.0 / Scala で動的に設定値を追加


いろいろ方法があるとは思いますが、紹介するのは文字列をパースして追加する方法です。

com.typesafe.config.ConfigFactory は内部的に利用されているので何もしなくても import 出来ます。

 play console                                                                            [info] Loading project definition from /private/tmp/todolist/project
[info] Set current project to todolist (in build file:/private/tmp/todolist/)
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.

scala> new play.core.StaticApplication(new java.io.File("."))
[info] play - database [default] connected at jdbc:postgresql://localhost:5432/todolist
[info] play - Application started (Prod)
res0: play.core.StaticApplication = play.core.StaticApplication@757d5b2a

scala> import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigFactory

scala> ConfigFactory.parseString(
     | """
     | hoge.fuga="moge"
     | """
     | )
res1: com.typesafe.config.Config = Config(SimpleConfigObject({"hoge" : {"fuga" : "moge"}}))

scala> val config = play.api.Play.current.configuration ++ play.api.Configuration(res1)
config: play.api.Configuration = Configuration(Config(SimpleConfigObject({"db" : {"default" : {"driver" : "org.postgresql.Driver","user" : "postgres","password" : "","url" : "jdbc:postgresql://localhost:5432/todolist"}},"os" : {"arch" : "x86_64","name" : "Mac OS X","version" : "10.7.4"},"application" : {"langs" : "en","secret" : "ytfSWO;?II/emj]W:xbig/rXH1[1VSVo>J?5GSmgFVem_KOHT?r6k9l?et71S@?A"},"mrj" : {"build" : "11M3811","version" : "1070.1.6.0_35-428"},"sbt" : {"boot" : {"properties" : "/usr/local/bin/../Cellar/play/2.0.3/bin/../libexec/framework/sbt/sbt.boot.properties"},"ivy" : {"home" : "/usr/local/bin/../Cellar/play/2.0.3/bin/../libexec/framework/../repository"}},"play" : {"home" : "/usr/local/bin/../Cellar/play/2.0.3/bin/../libexec/framework","version" : "2.0.3","akka" : {"loglevel"...
scala> config.getString("hoge.fuga").get
res2: String = moge

scala>