diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Predef.scala | 15 | ||||
-rw-r--r-- | src/library/scala/system/PropertiesMap.scala | 31 | ||||
-rw-r--r-- | src/library/scala/system/ShutdownHookThread.scala | 39 | ||||
-rw-r--r-- | src/library/scala/system/package.scala | 88 |
4 files changed, 164 insertions, 9 deletions
diff --git a/src/library/scala/Predef.scala b/src/library/scala/Predef.scala index 491d9e3901..7237c628d0 100644 --- a/src/library/scala/Predef.scala +++ b/src/library/scala/Predef.scala @@ -58,17 +58,14 @@ object Predef extends LowPriorityImplicits { // errors and asserts ------------------------------------------------- - // @deprecated("Throw your own exceptions") // deprecation waiting for 2.9 - def error(message: String): Nothing = throw new RuntimeException(message) + @deprecated("Use system.error(message) instead") + def error(message: String): Nothing = system.error(message) - // @deprecated("Use System.exit instead") // deprecation waiting for 2.9 - def exit(): Nothing = exit(0) + @deprecated("Use system.exit() instead") + def exit(): Nothing = system.exit() - // @deprecated("Use System.exit(status) instead") // deprecation waiting for 2.9 - def exit(status: Int): Nothing = { - java.lang.System.exit(status) - throw new Throwable() - } + @deprecated("Use system.exit(status) instead") + def exit(status: Int): Nothing = system.exit(status) /** Tests an expression, throwing an AssertionError if false. * Calls to this method will not be generated if -Xelide-below diff --git a/src/library/scala/system/PropertiesMap.scala b/src/library/scala/system/PropertiesMap.scala new file mode 100644 index 0000000000..a01eee6931 --- /dev/null +++ b/src/library/scala/system/PropertiesMap.scala @@ -0,0 +1,31 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.system + +import scala.collection.mutable +import scala.collection.JavaConverters._ + +/** A bidirectional map wrapping the java System properties. + * Changes to System properties will be immediately visible in the map, + * and modifications made to the map will be immediately applied to the + * System properties. + * + * @author Paul Phillips + * @version 2.9 + * @since 2.9 + */ +class PropertiesMap extends mutable.Map[String, String] { + override def empty = new PropertiesMap + override def default(key: String): String = null + def iterator: Iterator[(String, String)] = System.getProperties().asScala.iterator + def get(key: String) = Option(System.getProperty(key)) + + def -= (key: String): this.type = { System.clearProperty(key) ; this } + def += (kv: (String, String)): this.type = { System.setProperty(kv._1, kv._2) ; this } +}
\ No newline at end of file diff --git a/src/library/scala/system/ShutdownHookThread.scala b/src/library/scala/system/ShutdownHookThread.scala new file mode 100644 index 0000000000..17a5e398ef --- /dev/null +++ b/src/library/scala/system/ShutdownHookThread.scala @@ -0,0 +1,39 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.system + +/** A minimal Thread wrapper to enhance shutdown hooks. It knows + * how to unregister itself. + * + * @author Paul Phillips + * @version 2.9 + * @since 2.9 + */ +class ShutdownHookThread private (name: String) extends Thread(name) { + def remove() = runtime removeShutdownHook this +} + +object ShutdownHookThread { + private var hookNameCount: Int = 0 + private def hookName(): String = synchronized { + hookNameCount += 1 + "shutdownHook" + hookNameCount + } + /** Creates, names, and registers a shutdown hook to run the + * given code. + */ + def apply(body: => Unit): ShutdownHookThread = { + val t = new ShutdownHookThread(hookName()) { + override def run() = body + } + t setDaemon true + runtime addShutdownHook t + t + } +} diff --git a/src/library/scala/system/package.scala b/src/library/scala/system/package.scala new file mode 100644 index 0000000000..4dc754d373 --- /dev/null +++ b/src/library/scala/system/package.scala @@ -0,0 +1,88 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2010, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala + +import scala.collection.immutable +import collection.JavaConverters._ + +/** The package object `scala.system` contains methods for reading + * and altering core aspects of the virtual machine as well as the + * world outside of it. + * + * @author Paul Phillips + * @version 2.9 + * @since 2.9 + */ +package object system { + /** Throw a new RuntimeException with the supplied message. + * + * @return Nothing. + */ + def error(message: String): Nothing = throw new RuntimeException(message) + + /** Exit the JVM with the default status code. + * + * @return Nothing. + */ + def exit(): Nothing = exit(0) + + /** Exit the JVM with the given status code. + * + * @return Nothing. + */ + def exit(status: Int): Nothing = { + java.lang.System.exit(status) + throw new Throwable() + } + + /** A convenience method to get the current Runtime instance. + * + * @return the result of `java.lang.Runtime.getRuntime()` + */ + def runtime: Runtime = Runtime.getRuntime + + /** A bidirectional, mutable Map representing the current system Properties. + * + * @return a PropertiesMap. + * @see `scala.system.PropertiesMap` + */ + def props: PropertiesMap = new PropertiesMap + + /** An immutable Map representing the current system environment. + * + * @return a Map containing the system environment variables. + */ + def env: immutable.Map[String, String] = immutable.Map(System.getenv().asScala.toSeq: _*) + + /** Register a shutdown hook to be run when the VM exits. + * The newly created thread is marked as a daemon so it will not + * interfere with VM shutdown. The hook is automatically registered: + * the returned value can be ignored, but is available in case the + * Thread requires further modification. It can also be unregistered + * by calling ShutdownHookThread#remove(). + * + * Note that shutdown hooks are NOT guaranteed to be run. + * + * @param the body of code to run at shutdown + * @return the Thread which will run the shutdown hook. + */ + def addShutdownHook(body: => Unit): Thread = ShutdownHookThread(body) + + /** Returns all active thread in the current thread's thread group and subgroups. + * + * @return an IndexedSeq containing the threads. + */ + def allThreads(): IndexedSeq[Thread] = { + val num = Thread.activeCount() + val tarray = new Array[Thread](num) + val got = Thread.enumerate(tarray) + + tarray take got + } +}
\ No newline at end of file |