diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/properties.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,77 @@
     1.4 +;   Copyright (c) Stuart Halloway & Contributors, April 2009. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +;; DEPRECATED in 1.2.  Moved to c.c.java-utils
    1.13 +
    1.14 +(ns ^{:deprecated "1.2"}
    1.15 +  clojure.contrib.properties
    1.16 +  (:use [clojure.contrib.string :only (as-str)]
    1.17 +        [clojure.contrib.io :only (file)])
    1.18 +  (:import (java.util Properties)
    1.19 +           (java.io FileInputStream FileOutputStream)))
    1.20 +
    1.21 +(defn get-system-property 
    1.22 +  "Get a system property."
    1.23 +  ([stringable]
    1.24 +   (System/getProperty (as-str stringable)))
    1.25 +  ([stringable default]
    1.26 +   (System/getProperty (as-str stringable) default)))
    1.27 +
    1.28 +(defn set-system-properties
    1.29 +  "Set some system properties. Nil clears a property."
    1.30 +  [settings]
    1.31 +  (doseq [[name val] settings]
    1.32 +    (if val
    1.33 +      (System/setProperty (as-str name) (as-str val))
    1.34 +      (System/clearProperty (as-str name)))))
    1.35 +
    1.36 +(defmacro with-system-properties
    1.37 +  "setting => property-name value
    1.38 +
    1.39 +  Sets the system properties to the supplied values, executes the body, and
    1.40 +  sets the properties back to their original values. Values of nil are
    1.41 +  translated to a clearing of the property."
    1.42 +  [settings & body]
    1.43 +  `(let [settings# ~settings
    1.44 +         current# (reduce (fn [coll# k#]
    1.45 +			    (assoc coll# k# (get-system-property k#)))
    1.46 +			  {}
    1.47 +			  (keys settings#))]
    1.48 +     (set-system-properties settings#)       
    1.49 +     (try
    1.50 +      ~@body
    1.51 +      (finally
    1.52 +       (set-system-properties current#)))))
    1.53 +
    1.54 +
    1.55 +; Not there is no corresponding props->map. Just destructure!
    1.56 +(defn ^Properties as-properties
    1.57 +  "Convert any seq of pairs to a java.utils.Properties instance.
    1.58 +   Uses as-str to convert both keys and values into strings."
    1.59 +  {:tag Properties}
    1.60 +  [m]
    1.61 +  (let [p (Properties.)]
    1.62 +    (doseq [[k v] m]
    1.63 +      (.setProperty p (as-str k) (as-str v)))
    1.64 +    p))
    1.65 +
    1.66 +(defn read-properties
    1.67 +  "Read properties from file-able."
    1.68 +  [file-able]
    1.69 +  (with-open [f (java.io.FileInputStream. (file file-able))]
    1.70 +    (doto (Properties.)
    1.71 +      (.load f))))
    1.72 +
    1.73 +(defn write-properties
    1.74 +  "Write properties to file-able."
    1.75 +  {:tag Properties}
    1.76 +  ([m file-able] (write-properties m file-able nil))
    1.77 +  ([m file-able comments]
    1.78 +    (with-open [^FileOutputStream f (FileOutputStream. (file file-able))]
    1.79 +      (doto (as-properties m)
    1.80 +        (.store f ^String comments)))))