rlm@10: ; Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved. rlm@10: ; The use and distribution terms for this software are covered by the rlm@10: ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ; which can be found in the file epl-v10.html at the root of this distribution. rlm@10: ; By using this software in any fashion, you are agreeing to be bound by rlm@10: ; the terms of this license. rlm@10: ; You must not remove this notice, or any other, from this software. rlm@10: rlm@10: ;; DEPRECATED in 1.2. Moved to c.c.java-utils rlm@10: rlm@10: (ns ^{:deprecated "1.2"} rlm@10: clojure.contrib.properties rlm@10: (:use [clojure.contrib.string :only (as-str)] rlm@10: [clojure.contrib.io :only (file)]) rlm@10: (:import (java.util Properties) rlm@10: (java.io FileInputStream FileOutputStream))) rlm@10: rlm@10: (defn get-system-property rlm@10: "Get a system property." rlm@10: ([stringable] rlm@10: (System/getProperty (as-str stringable))) rlm@10: ([stringable default] rlm@10: (System/getProperty (as-str stringable) default))) rlm@10: rlm@10: (defn set-system-properties rlm@10: "Set some system properties. Nil clears a property." rlm@10: [settings] rlm@10: (doseq [[name val] settings] rlm@10: (if val rlm@10: (System/setProperty (as-str name) (as-str val)) rlm@10: (System/clearProperty (as-str name))))) rlm@10: rlm@10: (defmacro with-system-properties rlm@10: "setting => property-name value rlm@10: rlm@10: Sets the system properties to the supplied values, executes the body, and rlm@10: sets the properties back to their original values. Values of nil are rlm@10: translated to a clearing of the property." rlm@10: [settings & body] rlm@10: `(let [settings# ~settings rlm@10: current# (reduce (fn [coll# k#] rlm@10: (assoc coll# k# (get-system-property k#))) rlm@10: {} rlm@10: (keys settings#))] rlm@10: (set-system-properties settings#) rlm@10: (try rlm@10: ~@body rlm@10: (finally rlm@10: (set-system-properties current#))))) rlm@10: rlm@10: rlm@10: ; Not there is no corresponding props->map. Just destructure! rlm@10: (defn ^Properties as-properties rlm@10: "Convert any seq of pairs to a java.utils.Properties instance. rlm@10: Uses as-str to convert both keys and values into strings." rlm@10: {:tag Properties} rlm@10: [m] rlm@10: (let [p (Properties.)] rlm@10: (doseq [[k v] m] rlm@10: (.setProperty p (as-str k) (as-str v))) rlm@10: p)) rlm@10: rlm@10: (defn read-properties rlm@10: "Read properties from file-able." rlm@10: [file-able] rlm@10: (with-open [f (java.io.FileInputStream. (file file-able))] rlm@10: (doto (Properties.) rlm@10: (.load f)))) rlm@10: rlm@10: (defn write-properties rlm@10: "Write properties to file-able." rlm@10: {:tag Properties} rlm@10: ([m file-able] (write-properties m file-able nil)) rlm@10: ([m file-able comments] rlm@10: (with-open [^FileOutputStream f (FileOutputStream. (file file-able))] rlm@10: (doto (as-properties m) rlm@10: (.store f ^String comments)))))