 (ns backtype.storm.config
   (:import [java.io FileReader File])
-  (:import [backtype.storm Config])
+  (:import [backtype.storm Config Config$FieldValidator])
   (:import [backtype.storm.utils Utils LocalState])
   (:import [org.apache.commons.io FileUtils])
   (:require [clojure [string :as str]])

  (dofor [f (seq (.getFields Config))]
         (.get f nil)
         ))

(defn cluster-mode [conf & args]
  (keyword (conf STORM-CLUSTER-MODE)))

(defn read-default-config []
  (clojurify-structure (Utils/readDefaultConfig)))

(defn- validate-configs-with-schemas [conf]
  (doseq [[k v] conf
         :let [_ (println (str "trying to validate" [k v]))
               _ (println (str "schema is (" (get CONFIG-SCHEMA-MAP k) ")"))
               schema (get CONFIG-SCHEMA-MAP k)]]
    (if (instance? Config$FieldValidator schema)
      (if (and (not (nil? v))
               (not (.validateField schema v)))
        (throw (IllegalArgumentException.
                 (str "'" k "' " (.getCriteriaPredicate schema))))
        (println (str "OK: " k " " (.getCriteriaPredicate schema)))
      )
      (do 
        (println "schema is (" schema ")")
        (println "v is " v)
        (if (and (not (instance? schema v))
                 (not (nil? v)))
          (throw (IllegalArgumentException.
                   (str "'" k "' must be a '" (.getName schema) "'")))
          (println (str "OK: " k " was a " (.getName schema)))
        )
      )
    )))


(defn read-storm-config []
  (let [conf (clojurify-structure (Utils/readStormConfig))]
    (validate-configs-with-schemas conf)
    conf))

(defn read-yaml-config [name]
  (let [conf (clojurify-structure (Utils/findAndReadConfigFile name true))]
    (validate-configs-with-schemas conf)
    conf))

(defn master-local-dir [conf]
  (let [ret (str (conf STORM-LOCAL-DIR) "/nimbus")]
