summaryrefslogtreecommitdiff
path: root/src/library/scala/sys/SystemProperties.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-03-12 18:13:12 +0000
committerPaul Phillips <paulp@improving.org>2011-03-12 18:13:12 +0000
commitee4cc17eb7f75361c2395e8f38716de14e223b87 (patch)
tree9842ea8854fa46113e3798ea2869bd3f5a536790 /src/library/scala/sys/SystemProperties.scala
parente86934018bf3078a1a9751c16b95621a04575707 (diff)
downloadscala-ee4cc17eb7f75361c2395e8f38716de14e223b87.tar.gz
scala-ee4cc17eb7f75361c2395e8f38716de14e223b87.tar.bz2
scala-ee4cc17eb7f75361c2395e8f38716de14e223b87.zip
A small addition to the library to address some...
A small addition to the library to address something bugging me forever. It's a light interface to system properties. It's not intended to solve all property issues for all time, only to greatly improve on the overly ad-hoc ways things are presently done. Feedback welcome. Sorry it's coming in this late but it arises from writing the tools to fix the bugs to allow that release to happen. That's nature's circle of bugs. Review by community.
Diffstat (limited to 'src/library/scala/sys/SystemProperties.scala')
-rw-r--r--src/library/scala/sys/SystemProperties.scala53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/library/scala/sys/SystemProperties.scala b/src/library/scala/sys/SystemProperties.scala
new file mode 100644
index 0000000000..cc8131220f
--- /dev/null
+++ b/src/library/scala/sys/SystemProperties.scala
@@ -0,0 +1,53 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2003-2011, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+package scala.sys
+
+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 SystemProperties extends mutable.Map[String, String] {
+ override def empty = new SystemProperties
+ 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 }
+}
+
+/** The values in SystemProperties can be used to access and manipulate
+ * designated system properties. See `scala.sys.Prop` for particulars.
+ * @example {{{
+ * if (!headless.isSet) headless.enable()
+ * }}}
+ */
+object SystemProperties {
+ implicit def systemPropertiesToCompanion(p: SystemProperties): SystemProperties.type = this
+ private lazy val propertyHelp = mutable.Map[String, String]()
+ private def bool(key: String, helpText: String) = {
+ try Prop.bool(key)
+ finally propertyHelp(key) = helpText
+ }
+ def help(key: String) = propertyHelp.getOrElse(key, "")
+
+ // Todo: bring some sanity to the intersection of system properties aka "mutable
+ // state shared by everyone and everything" and the reality that there is no other
+ // mechanism for accomplishing some things on the jvm.
+ lazy val headless = bool("java.awt.headless", "system should not utilize a display device")
+ lazy val preferIPv4 = bool("java.net.preferIPv4Stack", "system should prefer IPv4 sockets")
+}