diff options
author | Paul Phillips <paulp@improving.org> | 2009-03-10 14:01:44 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-03-10 14:01:44 +0000 |
commit | 807daab252714f28d0d7a0e172af682520a8cf16 (patch) | |
tree | 30c5d38d62370113007f27509e434460775f8ba2 /src/library | |
parent | a1c3d51a90aed4daf097ed7e3aa1cf3c344c0d34 (diff) | |
download | scala-807daab252714f28d0d7a0e172af682520a8cf16.tar.gz scala-807daab252714f28d0d7a0e172af682520a8cf16.tar.bz2 scala-807daab252714f28d0d7a0e172af682520a8cf16.zip |
Refactored a pile of duplicated Properties code...
Refactored a pile of duplicated Properties code into a trait which is
used by the library, compiler, and partest Properties objects.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/util/Properties.scala | 87 |
1 files changed, 45 insertions, 42 deletions
diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 358e635742..77a2a10c4c 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -11,59 +11,62 @@ package scala.util -/** A utility to load the library properties from a Java properties file - * included in the jar. - * - * @author Stephane Micheloud - */ -object Properties { +private[scala] trait PropertiesTrait +{ + import java.io.{ IOException, PrintWriter } + protected def propCategory: String // specializes the remainder of the values + protected def pickJarBasedOn: Class[_] // props file comes from jar containing this /** The name of the properties file */ - private val propFilename = "/library.properties" + protected val propFilename = "/" + propCategory + ".properties" /** The loaded properties */ - private val props = { + protected lazy val props: java.util.Properties = { val props = new java.util.Properties - val stream = classOf[Application].getResourceAsStream(propFilename) - try { - if (stream != null) - props.load(stream) - } finally { - if (stream != null) { - // close quietly - try { - stream.close() - } catch { - case _ => - } - } - } + val stream = pickJarBasedOn getResourceAsStream propFilename + if (stream ne null) + quietlyDispose(props load stream, stream.close) + props } - /** The version number of the jar this was loaded from plus "version " prefix, - * or "version (unknown)" if it cannot be determined. - */ - val versionString: String = { - val defaultString = "(unknown)" - "version " + props.getProperty("version.number", defaultString) - } + private def quietlyDispose(action: => Unit, disposal: => Unit) = + try { action } + finally { + try { disposal } + catch { case _: IOException => } + } - val copyrightString: String = { - val defaultString = "(c) 2002-2009 LAMP/EPFL" - props.getProperty("copyright.string", defaultString) - } + // for values based on system properties + protected def sysprop(name: String, default: String) = + System.getProperty(name, default) - val encodingString: String = { - val defaultString = "UTF8" //"ISO-8859-1" - props.getProperty("file.encoding", defaultString) - } + // for values based on propFilename + protected def prop(name: String, default: String): String = + props.getProperty(name, default) - private val writer = new java.io.PrintWriter(Console.err, true) + // XXX file.encoding should not be here, as it causes system setting to + // be ignored. See https://lampsvn.epfl.ch/trac/scala/ticket/1581 - val versionMsg = "Scala library " + versionString + " -- " + copyrightString + /** The version number of the jar this was loaded from plus "version " prefix, + * or "version (unknown)" if it cannot be determined. + */ + val versionString = "version " + prop("version.number", "(unknown)") + val copyrightString = prop("copyright.string", "(c) 2002-2009 LAMP/EPFL") + val encodingString = prop("file.encoding", "UTF8") - def main(args: Array[String]) { - writer.println(versionMsg) - } + // provide a main method so version info can be obtained by running this + private val writer = new java.io.PrintWriter(Console.err, true) + private val versionMsg = "Scala %s %s -- %s".format(propCategory, versionString, copyrightString) + def main(args: Array[String]) { writer println versionMsg } +} + +/** A utility to load the library properties from a Java properties file + * included in the jar. + * + * @author Stephane Micheloud + */ +object Properties extends PropertiesTrait { + protected def propCategory = "library" + protected def pickJarBasedOn = classOf[Application] } |