1
0

Initial verison of reading config from files

This commit is contained in:
Henry Cook 2012-10-15 16:04:25 -07:00
parent 5d2a470215
commit a7a4e65690
2 changed files with 57 additions and 0 deletions

View File

@ -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)
}
}

View File

@ -0,0 +1 @@
NWAYS 4