You can use the type safe config for this purpose.
You will have to define a file called application.conf which should be present in your classpath.
Typesafe config works by reading configurations defined in application.conf, reference.conf etc.
So first create a file called application.conf which should be in application classpath
and define the configuration values like this
com.company.application {
production {
mongodb {
servers=["10.1.1.2", "10.1.1.3"]
password=pwd
}
}
development {
mongodb {
servers=["10.1.1.2", "10.1.1.3"]
password=pwd
}
}
local {
mongodb {
servers=["127.0.0.1."]
password=pwd
}
}
}
Code to read the values will look something like this
import com.typesafe.config.ConfigFactory
import com.typesafe.config.Config
object MongoReplicaConnection {
def mongoDb = {
val configNamespace = "com.company.application"
val mergedCfg = ConfigFactory.load().getConfig(configNamespace)
val env = getEnvironment // a util function, returns development or productoin or local
// environment can be passed as jvm args and read from System properties
val envCfg = mergedCfg.getConfig(env)
val serverCfg = envCfg.getConfig("mongodb")
val servers = serverCfg.getStringList("servers")
// Servers will be a returned as List of Strings
val pwd = serverCfg.getString("password")
val addresses = servers map { new ServerAddress(_ , 27017) }
val mongoConn = MongoConnection(addresses)
val db = mongoConn("mydb")
db
}
}