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 | |
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.
-rw-r--r-- | src/compiler/scala/tools/nsc/Properties.scala | 76 | ||||
-rw-r--r-- | src/library/scala/util/Properties.scala | 87 | ||||
-rw-r--r-- | src/partest/scala/tools/partest/utils/Properties.scala | 44 |
3 files changed, 67 insertions, 140 deletions
diff --git a/src/compiler/scala/tools/nsc/Properties.scala b/src/compiler/scala/tools/nsc/Properties.scala index fcc522df93..4ec98a1d0c 100644 --- a/src/compiler/scala/tools/nsc/Properties.scala +++ b/src/compiler/scala/tools/nsc/Properties.scala @@ -6,68 +6,24 @@ // $Id$ package scala.tools.nsc +import scala.util.PropertiesTrait /** A utility to load the compiler properties from a Java properties file * included in the jar. */ -object Properties { - - /** The name of the properties file */ - private val propFilename = "/compiler.properties" - - /** The loaded properties */ - private val props = { - val props = new java.util.Properties - val stream = classOf[Global].getResourceAsStream(propFilename) - if (stream != null) - props.load(stream) - props - } - - val isWin = System.getProperty("os.name") startsWith "Windows" - - /** The version number of the jar this was loaded from, or - * "(unknown)" if it cannot be determined. - */ - val versionString: String = { - val defaultString = "(unknown)" - "version " + props.getProperty("version.number", defaultString) - } - - val copyrightString: String = { - val defaultString = "(c) 2002-2009 LAMP/EPFL" - props.getProperty("copyright.string", defaultString) - } - - val encodingString: String = { - val defaultString = "UTF8" //"ISO-8859-1" - props.getProperty("file.encoding", defaultString) - } - - val fileEndingString: String = { - val defaultString = ".scala|.java" - props.getProperty("file.ending", defaultString) - } - - val residentPromptString: String = { - val defaultString = "\nnsc> " - props.getProperty("resident.prompt", defaultString) - } - - val shellPromptString: String = { - val defaultString = "\nscala> " - props.getProperty("shell.prompt", defaultString) - } - - val scalaHome: String = - System.getProperty("scala.home") - - val envClasspath: String = - System.getProperty("env.classpath") - - val cmdName: String = - if (isWin) "scala.bat" else "scala" - - val msilILasm: String = - System.getProperty("msil.ilasm", "") +object Properties extends PropertiesTrait { + protected def propCategory = "compiler" + protected def pickJarBasedOn = classOf[Global] + + // settings based on jar properties + val fileEndingString = prop("file.ending", ".scala|.java") + val residentPromptString = prop("resident.prompt", "\nnsc> ") + val shellPromptString = prop("shell.prompt", "\nscala> ") + + // settings based on System properties + val isWin = sysprop("os.name", "") startsWith "Windows" + val scalaHome = sysprop("scala.home", null) + val envClasspath = sysprop("env.classpath", null) + val msilILasm = sysprop("msil.ilasm", "") + val cmdName = if (isWin) "scala.bat" else "scala" } 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] } diff --git a/src/partest/scala/tools/partest/utils/Properties.scala b/src/partest/scala/tools/partest/utils/Properties.scala index 76d333eccb..1807c26cf2 100644 --- a/src/partest/scala/tools/partest/utils/Properties.scala +++ b/src/partest/scala/tools/partest/utils/Properties.scala @@ -9,49 +9,17 @@ // $Id$ package scala.tools.partest.utils +import scala.util.PropertiesTrait /** A utility to load the library properties from a Java properties file * included in the jar. * * @author Stephane Micheloud */ -object Properties { +object Properties extends PropertiesTrait { + protected def propCategory = "partest" + protected def pickJarBasedOn = classOf[Application] - /** The name of the properties file */ - private val propFilename = "/partest.properties" - - /** The loaded properties */ - private val props = { - val props = new java.util.Properties - val stream = classOf[Application].getResourceAsStream(propFilename) - if (stream != null) - props.load(stream) - props - } - - /** The version number of the jar this was loaded from, or - * "(unknown)" if it cannot be determined. - */ - val versionString: String = { - val defaultString = "(unknown)" - "version " + props.getProperty("version.number") - } - - val copyrightString: String = { - val defaultString = "(c) 2002-2009 LAMP/EPFL" - props.getProperty("copyright.string", defaultString) - } - - val encodingString: String = { - val defaultString = "ISO-8859-1" - props.getProperty("file.encoding", defaultString) - } - - private val writer = new java.io.PrintWriter(Console.err, true) - - val versionMsg = "Scala partest " + versionString + " -- " + copyrightString - - def main(args: Array[String]) { - writer.println(versionMsg) - } + // XXX unlikely it's intentional that only partest uses ISO-8859-1 + override val encodingString = prop("file.encoding", "ISO-8859-1") } |