@@ -5,6 +5,10 @@ import javax.inject.Singleton
55import com .google .inject .{ImplementedBy , Inject }
66import play .api .Configuration
77
8+ import scala .collection .JavaConverters ._
9+ import scala .util .{Failure , Success , Try }
10+
11+
812@ ImplementedBy (classOf [HostsImpl ])
913trait Hosts {
1014
@@ -17,18 +21,18 @@ trait Hosts {
1721@ Singleton
1822class HostsImpl @ Inject ()(config : Configuration ) extends Hosts {
1923
20- val hosts : Map [String , ElasticServer ] = config.getConfigSeq (" hosts" ) match {
21- case Some (hostsConf) => hostsConf.map { hostConf =>
22- val host = hostConf.getString (" host" ).get
23- val name = hostConf.getString (" name" ).getOrElse(host)
24- val username = hostConf.getString (" auth.username" )
25- val password = hostConf.getString (" auth.password" )
24+ val hosts : Map [String , ElasticServer ] = Try ( config.underlying.getConfigList (" hosts" ).asScala.map( Configuration (_)) ) match {
25+ case Success (hostsConf) => hostsConf.map { hostConf =>
26+ val host = hostConf.getOptional[ String ] (" host" ).get
27+ val name = hostConf.getOptional[ String ] (" name" ).getOrElse(host)
28+ val username = hostConf.getOptional[ String ] (" auth.username" )
29+ val password = hostConf.getOptional[ String ] (" auth.password" )
2630 (username, password) match {
2731 case (Some (username), Some (password)) => (name -> ElasticServer (host, Some (ESAuth (username, password))))
2832 case _ => (name -> ElasticServer (host, None ))
2933 }
3034 }.toMap
31- case _ => Map ()
35+ case Failure (_) => Map ()
3236 }
3337
3438 def getHostNames () = hosts.keys.toSeq
0 commit comments