annotate src/clojure/contrib/properties.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved.
rlm@10 2 ; The use and distribution terms for this software are covered by the
rlm@10 3 ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 4 ; which can be found in the file epl-v10.html at the root of this distribution.
rlm@10 5 ; By using this software in any fashion, you are agreeing to be bound by
rlm@10 6 ; the terms of this license.
rlm@10 7 ; You must not remove this notice, or any other, from this software.
rlm@10 8
rlm@10 9 ;; DEPRECATED in 1.2. Moved to c.c.java-utils
rlm@10 10
rlm@10 11 (ns ^{:deprecated "1.2"}
rlm@10 12 clojure.contrib.properties
rlm@10 13 (:use [clojure.contrib.string :only (as-str)]
rlm@10 14 [clojure.contrib.io :only (file)])
rlm@10 15 (:import (java.util Properties)
rlm@10 16 (java.io FileInputStream FileOutputStream)))
rlm@10 17
rlm@10 18 (defn get-system-property
rlm@10 19 "Get a system property."
rlm@10 20 ([stringable]
rlm@10 21 (System/getProperty (as-str stringable)))
rlm@10 22 ([stringable default]
rlm@10 23 (System/getProperty (as-str stringable) default)))
rlm@10 24
rlm@10 25 (defn set-system-properties
rlm@10 26 "Set some system properties. Nil clears a property."
rlm@10 27 [settings]
rlm@10 28 (doseq [[name val] settings]
rlm@10 29 (if val
rlm@10 30 (System/setProperty (as-str name) (as-str val))
rlm@10 31 (System/clearProperty (as-str name)))))
rlm@10 32
rlm@10 33 (defmacro with-system-properties
rlm@10 34 "setting => property-name value
rlm@10 35
rlm@10 36 Sets the system properties to the supplied values, executes the body, and
rlm@10 37 sets the properties back to their original values. Values of nil are
rlm@10 38 translated to a clearing of the property."
rlm@10 39 [settings & body]
rlm@10 40 `(let [settings# ~settings
rlm@10 41 current# (reduce (fn [coll# k#]
rlm@10 42 (assoc coll# k# (get-system-property k#)))
rlm@10 43 {}
rlm@10 44 (keys settings#))]
rlm@10 45 (set-system-properties settings#)
rlm@10 46 (try
rlm@10 47 ~@body
rlm@10 48 (finally
rlm@10 49 (set-system-properties current#)))))
rlm@10 50
rlm@10 51
rlm@10 52 ; Not there is no corresponding props->map. Just destructure!
rlm@10 53 (defn ^Properties as-properties
rlm@10 54 "Convert any seq of pairs to a java.utils.Properties instance.
rlm@10 55 Uses as-str to convert both keys and values into strings."
rlm@10 56 {:tag Properties}
rlm@10 57 [m]
rlm@10 58 (let [p (Properties.)]
rlm@10 59 (doseq [[k v] m]
rlm@10 60 (.setProperty p (as-str k) (as-str v)))
rlm@10 61 p))
rlm@10 62
rlm@10 63 (defn read-properties
rlm@10 64 "Read properties from file-able."
rlm@10 65 [file-able]
rlm@10 66 (with-open [f (java.io.FileInputStream. (file file-able))]
rlm@10 67 (doto (Properties.)
rlm@10 68 (.load f))))
rlm@10 69
rlm@10 70 (defn write-properties
rlm@10 71 "Write properties to file-able."
rlm@10 72 {:tag Properties}
rlm@10 73 ([m file-able] (write-properties m file-able nil))
rlm@10 74 ([m file-able comments]
rlm@10 75 (with-open [^FileOutputStream f (FileOutputStream. (file file-able))]
rlm@10 76 (doto (as-properties m)
rlm@10 77 (.store f ^String comments)))))