diff options
author | Paul Phillips <paulp@improving.org> | 2010-02-23 00:27:39 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-02-23 00:27:39 +0000 |
commit | df94b3c5b831e78a056f7b0b5334b1fb8d3c3bd5 (patch) | |
tree | 6be1d68b93045d6a568ac165126d4ed8e11e3fd7 /src/library | |
parent | 8d74992310fe60a1da32606949c96531691754e9 (diff) | |
download | scala-df94b3c5b831e78a056f7b0b5334b1fb8d3c3bd5.tar.gz scala-df94b3c5b831e78a056f7b0b5334b1fb8d3c3bd5.tar.bz2 scala-df94b3c5b831e78a056f7b0b5334b1fb8d3c3bd5.zip |
Some much needed housecleaning regarding system...
Some much needed housecleaning regarding system properties. If you can
possibly resist the temptation, it'd be great if people could try to
go through the properties classes to get and set them, and also to set
property values somewhere fixed rather than using strings directly.
Review by community.
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Application.scala | 3 | ||||
-rw-r--r-- | src/library/scala/compat/Platform.scala | 2 | ||||
-rw-r--r-- | src/library/scala/util/Properties.scala | 99 |
3 files changed, 66 insertions, 38 deletions
diff --git a/src/library/scala/Application.scala b/src/library/scala/Application.scala index e9b97b5356..fdb122f5bf 100644 --- a/src/library/scala/Application.scala +++ b/src/library/scala/Application.scala @@ -11,7 +11,6 @@ package scala -import java.lang.System.getProperty import scala.compat.Platform.currentTime /** <p> @@ -84,7 +83,7 @@ trait Application { * @param args the arguments passed to the main method */ def main(args: Array[String]) { - if (getProperty("scala.time") ne null) { + if (util.Properties.propIsSet("scala.time")) { val total = currentTime - executionStart Console.println("[total " + total + "ms]") } diff --git a/src/library/scala/compat/Platform.scala b/src/library/scala/compat/Platform.scala index f7f5070699..7580d2cc0e 100644 --- a/src/library/scala/compat/Platform.scala +++ b/src/library/scala/compat/Platform.scala @@ -47,7 +47,7 @@ object Platform { @inline def getClassForName(name: String): Class[_] = java.lang.Class.forName(name) - val EOL = System.getProperty("line.separator", "\n") + val EOL = util.Properties.lineSeparator @inline def currentTime: Long = System.currentTimeMillis() diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index ee3e68ed76..d61a834555 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -8,12 +8,18 @@ // $Id$ - package scala.util +import java.io.{ IOException, PrintWriter } + +/** Loads library.properties from the jar. */ +object Properties extends PropertiesTrait { + protected def propCategory = "library" + protected def pickJarBasedOn = classOf[ScalaObject] +} + 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 @@ -21,7 +27,7 @@ private[scala] trait PropertiesTrait protected val propFilename = "/" + propCategory + ".properties" /** The loaded properties */ - protected lazy val props: java.util.Properties = { + protected lazy val scalaProps: java.util.Properties = { val props = new java.util.Properties val stream = pickJarBasedOn getResourceAsStream propFilename if (stream ne null) @@ -37,53 +43,76 @@ private[scala] trait PropertiesTrait catch { case _: IOException => } } - // for values based on system properties - def sysprop(name: String): String = sysprop(name, "") - def sysprop(name: String, default: String): String = System.getProperty(name, default) - def syspropset(name: String, value: String) = System.setProperty(name, value) + def propIsSet(name: String) = System.getProperty(name) != null + def propIsSetTo(name: String, value: String) = propOrNull(name) == value + def propOrElse(name: String, alt: String) = System.getProperty(name, alt) + def propOrEmpty(name: String) = propOrElse(name, "") + def propOrNull(name: String) = propOrElse(name, null) + def propOrNone(name: String) = Option(propOrNull(name)) + def setProp(name: String, value: String) = System.setProperty(name, value) + def clearProp(name: String) = System.clearProperty(name) + + def envOrElse(name: String, alt: String) = Option(System getenv name) getOrElse alt // for values based on propFilename - def prop(name: String): String = props.getProperty(name, "") - def prop(name: String, default: String): String = props.getProperty(name, default) + def scalaPropOrElse(name: String, alt: String): String = scalaProps.getProperty(name, alt) + def scalaPropOrEmpty(name: String): String = scalaPropOrElse(name, "") /** 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-2010 LAMP/EPFL") + val versionString = "version " + scalaPropOrElse("version.number", "(unknown)") + val copyrightString = scalaPropOrElse("copyright.string", "(c) 2002-2010 LAMP/EPFL") /** This is the encoding to use reading in source files, overridden with -encoding * Note that it uses "prop" i.e. looks in the scala jar, not the system properties. */ - def sourceEncoding = prop("file.encoding", "UTF-8") + def sourceEncoding = scalaPropOrElse("file.encoding", "UTF-8") /** This is the default text encoding, overridden (unreliably) with * JAVA_OPTS="-Dfile.encoding=Foo" */ - def encodingString = sysprop("file.encoding", "UTF-8") - - def isWin = sysprop("os.name") startsWith "Windows" - def isMac = sysprop("java.vendor") startsWith "Apple" - def javaClassPath = sysprop("java.class.path") - def javaHome = sysprop("java.home") - def javaVmName = sysprop("java.vm.name") - def javaVmVersion = sysprop("java.vm.version") - def javaVmInfo = sysprop("java.vm.info") - def javaVersion = sysprop("java.version") - def tmpDir = sysprop("java.io.tmpdir") - def homeDir = sysprop("user.home") - def currentDir = sysprop("user.dir") - def userName = sysprop("user.name") - def scalaHome = sysprop("scala.home", null) // XXX places do null checks... + def encodingString = propOrElse("file.encoding", "UTF-8") + + /** The default end of line character. + */ + def lineSeparator = propOrElse("line.separator", "\n") + + /** Various well-known properties. + */ + def javaClassPath = propOrEmpty("java.class.path") + def javaHome = propOrEmpty("java.home") + def javaVendor = propOrEmpty("java.vendor") + def javaVersion = propOrEmpty("java.version") + def javaVmInfo = propOrEmpty("java.vm.info") + def javaVmName = propOrEmpty("java.vm.name") + def javaVmVendor = propOrEmpty("java.vm.vendor") + def javaVmVersion = propOrEmpty("java.vm.version") + def osName = propOrEmpty("os.name") + def scalaHome = propOrEmpty("scala.home") + def tmpDir = propOrEmpty("java.io.tmpdir") + def userDir = propOrEmpty("user.dir") + def userHome = propOrEmpty("user.home") + def userName = propOrEmpty("user.name") + + /** Some derived values. + */ + def isWin = osName startsWith "Windows" + def isMac = javaVendor startsWith "Apple" - // provide a main method so version info can be obtained by running this - private val writer = new java.io.PrintWriter(Console.err, true) def versionMsg = "Scala %s %s -- %s".format(propCategory, versionString, copyrightString) - def main(args: Array[String]) { writer println versionMsg } -} + def scalaCmd = if (isWin) "scala.bat" else "scala" + def scalacCmd = if (isWin) "scalac.bat" else "scalac" -/** Loads library.properties from the jar. */ -object Properties extends PropertiesTrait { - protected def propCategory = "library" - protected def pickJarBasedOn = classOf[Application] + /** Can the java version be determined to be at least as high as the argument? + */ + def isJavaAtLeast(version: Double) = + try javaVersion.toDouble >= version + catch { case _: NumberFormatException => false } + + // provide a main method so version info can be obtained by running this + def main(args: Array[String]) { + val writer = new PrintWriter(Console.err, true) + writer println versionMsg + } } |