Chciałbym to zrobić bardziej elegancko. Jest to główna klasa, którą używam jako jar zapodając mu argumenty z lini poleceń
np. java -jar myapp.jar --aps.props="./aps.properties"
object Main {
val logger = Logger(LoggerFactory.getLogger(this.getClass))
val AppPropsFile = "--app.props"
val ConnPropsFile = "--conn.props"
val DaoPropsFile = "--dao.props"
val DefaultPropsFile = "resources/app.properties"
val DefaultConnPropsFile = "resources/connconfigs.properties"
val DefaultDaoPropsFile = "resources/dao.properties"
val HelloMsg: String =
s"""
|Welcome in my app.
|
|You can set properties files to use with argKey=argValue. Allowed key args are:
|${AppPropsFile}
|${ConnPropsFile}
|${DaoPropsFile}
|""".stripMargin
private def getPath(argsMap: Map[String, String], argKey: String, defaultArgValue: String): String = {
val path = argsMap.getOrElse(argKey, defaultArgValue)
if ( !Files.exists(Paths.get(path)) ) {
throw new FileNotFoundException(s"Set ${argKey} to existing file.")
}
path
}
def main(args: Array[String]): Unit = {
logger.info(HelloMsg)
val argsMap: Map[String, String] = args
.map(_.split("="))
.map(x => x(0) -> x(1))
.toMap
val propsFile = getPath(argsMap, AppPropsFile, DefaultPropsFile)
val connPropsFile = getPath(argsMap, ConnPropsFile, DefaultConnPropsFile)
val daoPropsFile = getPath(argsMap, DaoPropsFile, DefaultDaoPropsFile)
val props = new AdsPropertiesFile(propsFile)
val connectionConfigs = new ConnConfigProperties(connPropsFile)
val propsDao = new DaoProperties(daoPropsFile)
val daoContainer = new DaoContainer(propsDao)
val serviceContainer = new ServiceContainer(daoContainer)
val app = new App(props, connectionConfigs, serviceContainer)
app.start()
}
}