From a7a4e6569040683e2b0d9703bb13953ed91280b3 Mon Sep 17 00:00:00 2001 From: Henry Cook Date: Mon, 15 Oct 2012 16:04:25 -0700 Subject: [PATCH] Initial verison of reading config from files --- rocket/src/main/scala/config.scala | 56 +++++++++++++++++++++++++++++ rocket/src/main/scala/rocket.config | 1 + 2 files changed, 57 insertions(+) create mode 100644 rocket/src/main/scala/config.scala create mode 100644 rocket/src/main/scala/rocket.config diff --git a/rocket/src/main/scala/config.scala b/rocket/src/main/scala/config.scala new file mode 100644 index 00000000..13d7344a --- /dev/null +++ b/rocket/src/main/scala/config.scala @@ -0,0 +1,56 @@ +package rocket +package config + +import java.io.File +import java.io.FileInputStream +import java.util.Properties +import scala.util.{Properties => SProperties} + +class Config(props: Properties) { + private val msg = "Configuration is missing requested parameter " + def getInt(name: String): Int = Option(props.getProperty(name).toInt).getOrElse(sys.error(msg+name)) + def getString(name: String): String = Option(props.getProperty(name)).getOrElse(sys.error(msg+name)) + def getBoolean(name: String): Boolean = Option(props.getProperty(name).toBoolean).getOrElse(sys.error(msg+name)) + def apply(name: String): Int = getInt(name) +} + +object Config { + + lazy val internal_config = getConfig() + + def apply(name: String) = internal_config(name) + + private def getConfig(): Config = { + + val filePath0 = + SProperties + .envOrNone("ROCKET_CONFIG") + .orElse(SProperties.propOrNone("rocket.config")) + if (filePath0.isEmpty) + Console.err.println(""" + | WARNING: Could not find configuration file to load. + | Options are: + | (1) Set environmental variable ROCKET_CONFIG to the config file path + | (2) Set system property rocket.config to the config file path + | Using default values for config. + """.stripMargin) + + val filePath = + filePath0.flatMap(fp => { + val f = new File(fp) + if (!f.isFile) { + Console.err.println(""" + | WARNING: File '%s' is not a valid file path + | Using default values for config + """.format(fp).stripMargin) + None + } else Some(fp) + }) + + val props = new Properties() + filePath.map(fp => props.load(new FileInputStream(fp))) + new Config(props) + } + +} + diff --git a/rocket/src/main/scala/rocket.config b/rocket/src/main/scala/rocket.config new file mode 100644 index 00000000..65b60ff0 --- /dev/null +++ b/rocket/src/main/scala/rocket.config @@ -0,0 +1 @@ +NWAYS 4