diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2009-04-05 16:02:37 +0000 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2009-04-05 16:02:37 +0000 |
commit | 47024875854f8bed0301f9927d7faa6e0e83b595 (patch) | |
tree | c7daeb1071a26f59ceed2416a34f7914536f0a9e /src/clojure/contrib/java_utils.clj | |
parent | 57571874b35cd8df77ccdc96a4e158c74478280e (diff) |
java-util: the-str and with-system-properties
Diffstat (limited to 'src/clojure/contrib/java_utils.clj')
-rw-r--r-- | src/clojure/contrib/java_utils.clj | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/src/clojure/contrib/java_utils.clj b/src/clojure/contrib/java_utils.clj index 1ea420ea..a60f68c8 100644 --- a/src/clojure/contrib/java_utils.clj +++ b/src/clojure/contrib/java_utils.clj @@ -1,4 +1,4 @@ -; Copyright (c) Stuart Halloway, April 2009. All rights reserved. +; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved. ; The use and distribution terms for this software are covered by the ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) ; which can be found in the file epl-v10.html at the root of this distribution. @@ -23,6 +23,12 @@ ; ; If something in this module violates the principle of least surprise, please ; let me (Stu) and the Clojure community know via the mailing list. +; +; Contributors: +; +; Stuart Halloway +; Stephen C. Gilardi +; Shawn Hoover (ns clojure.contrib.java-utils (:import [java.io File])) @@ -56,5 +62,44 @@ (File. (as-file parent) (relative-path-string child))) ([parent child & more] (reduce file (file parent child) more))) + +(defn the-str + "Returns the name or string representation of x" + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) + +(defn get-system-property [stringable] + (System/getProperty (the-str stringable))) + +(defn set-system-properties + [settings] + "Set some system properties. Nil clears a property." + (doseq [[name val] settings] + (if val + (System/setProperty (the-str name) val) + (System/clearProperty (the-str name))))) + +(defmacro with-system-properties + "setting => property-name value + + Sets the system properties to the supplied values, executes the body, and + sets the properties back to their original values. Values of nil are + translated to a clearing of the property." + [settings & body] + `(let [settings# ~settings + current# (reduce (fn [coll# k#] + (assoc coll# k# (get-system-property k#))) + {} + (keys settings#))] + (set-system-properties settings#) + (try + ~@body + (finally + (set-system-properties current#))))) + + +
\ No newline at end of file |