Advertisement
Guest User

Untitled

a guest
Jul 18th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.81 KB | None | 0 0
  1.   lazy val props: Map[String, String] = {
  2.     import java.io.{ByteArrayInputStream}
  3.     import java.util.InvalidPropertiesFormatException
  4.     import java.util.{Map => JMap}
  5.  
  6.     var tried: List[String] = Nil
  7.  
  8.     trace("Loading properties. Active run.mode is %s".format(if (modeName=="") "(Development)" else modeName))
  9.  
  10.     def vendStreams: List[(String, () => Box[InputStream])] = whereToLook() :::
  11.     toTry.map{
  12.       f => {
  13.         val name = f() + "props"
  14.         name -> {() =>
  15.           val res = tryo{getClass.getResourceAsStream(name)}.filter(_ ne null)
  16.           trace("Trying to open resource %s. Result=%s".format(name, res))
  17.           res
  18.         }
  19.       }
  20.     }
  21.  
  22.     // find the first property file that is available
  23.     first(vendStreams){
  24.       case (str, streamBox) =>
  25.         tried ::= str
  26.         for {
  27.           stream <- streamBox()
  28.         } yield {
  29.           val ret = new Properties
  30.           val ba = Helpers.readWholeStream(stream)
  31.           try {
  32.             ret.loadFromXML(new ByteArrayInputStream(ba))
  33.             debug("Loaded XML properties from resource %s".format(str))
  34.           } catch {
  35.             case _: InvalidPropertiesFormatException =>
  36.               ret.load(new ByteArrayInputStream(ba))
  37.               debug("Loaded key/value properties from resource %s".format(str))
  38.           }
  39.           ret
  40.         }
  41.     } match {
  42.       // if we've got a propety file, create name/value pairs and turn them into a Map
  43.       case Full(prop) =>
  44.         Map(prop.entrySet.toArray.flatMap{
  45.           case s: JMap.Entry[_, _] => List((s.getKey.toString, s.getValue.toString))
  46.           case _ => Nil
  47.         } :_*)
  48.  
  49.       case _ =>
  50.         error("Failed to find a properties file (but properties were accessed).  Searched: "+tried.reverse.mkString(", "))
  51.         Map()
  52.     }
  53.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement