Permalink
Browse files

init commit

  • Loading branch information...
0 parents commit 8082300ad7bd611a63ecb909cd9b800f0ed4d3f6 @xumingming committed Nov 28, 2012
Showing with 196 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +13 −0 README.md
  3. +6 −0 project.clj
  4. +3 −0 src/jmx/#client.clj#
  5. +1 −0 src/jmx/.#client.clj
  6. +9 −0 src/jmx/client.clj
  7. +149 −0 src/jmx/repl.clj
  8. +6 −0 test/jmx_repl/test/core.clj
@@ -0,0 +1,9 @@
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
+target
@@ -0,0 +1,13 @@
+# jmx-repl
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2012 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,6 @@
+(defproject jmx-repl "1.0.0-SNAPSHOT"
+ :description "FIXME: write description"
+ :main jmx.repl
+ :dependencies [[org.clojure/clojure "1.3.0"]
+ [org.clojure/java.jmx "0.2.0"]
+ [colorize "0.1.1"]])
@@ -0,0 +1,3 @@
+(ns jmx.client)
+
+
@@ -0,0 +1,9 @@
+(ns jmx.client)
+
+(loop [input (read-line)]
+ (if (= "help" input)
+ (println "haha")
+ (print "invalid command"))
+ (println "=>")
+ (recur (read-line)))
+
@@ -0,0 +1,149 @@
+(ns jmx.repl
+ (require [clojure.java.jmx :as jmx]
+ [clojure.string :as string]
+ [colorize.core :as color]))
+(def wd (atom {}))
+
+(declare ls0 cat0 wd->bean-name wd->path)
+(defn get-current-depth []
+ (cond
+ (nil? (:namespace @wd)) :root
+ (nil? (:type @wd)) :namespace
+ (nil? (:name @wd)) :type
+ true :name))
+
+(defn extract-namespaces [names]
+ (sort (set (map (fn [name]
+ (let [real-name (string/split name #":")
+ real-name (first real-name)]
+ real-name)) names))))
+
+(defn extract-types [names]
+ (sort (set (map (fn [name]
+ (let [real-name (string/split name #":")
+ real-name (second real-name)
+ real-name (string/split real-name #",")
+ real-name (filter (fn [name]
+ (.startsWith name "type=")) real-name)
+
+ real-name (first real-name)
+ real-name (.substring real-name 5)]
+ real-name)) names))))
+
+(defn extract-names [names]
+ (sort (set (map (fn [name]
+ (let [real-name (string/split name #":")
+ real-name (second real-name)
+ real-name (string/split real-name #",")
+
+ real-name (if (= 2 (count real-name))
+ (filter (fn [name]
+ (.startsWith name "name=")) real-name)
+ (filter (fn [name]
+ (.startsWith name "type=")) real-name))
+
+ real-name (first real-name)
+ real-name (.substring real-name 5)]
+ real-name)) names))))
+(defn extract-attributes [bean-prefix]
+ (sort (set (map name (jmx/attribute-names bean-prefix)))))
+
+(defn ls []
+ (let [depth (get-current-depth)
+ bean-prefix (wd->bean-name @wd)
+ names (ls0 bean-prefix)
+ names (condp = depth
+ ;; if current depth is root, show all the namespace
+ :root (extract-namespaces names)
+ ;; if current depth is namespace, show all the type
+ :namespace (extract-types names)
+ ;; if current depth is type, show all the names
+ :type (extract-names names)
+ :name (extract-attributes bean-prefix)
+ )]
+ (doseq [name names]
+ (println (color/cyan "\t" name)))))
+
+(defn cat [attr-name]
+ (let [attr-value (cat0 attr-name)]
+ (println (color/blue attr-value))))
+
+(defn pwd []
+ (wd->path @wd))
+;; java.lang:name=CMS Old Gen,type=MemoryPoll ->
+;; {:namespace java.lang
+;; :name "CMS Old Gen"
+;; :type "MemoryPool"}
+(defn cd [name]
+ (let [cd-type (cond
+ (= ".." name) :parent
+ (nil? (:namespace @wd)) :namespace
+ (nil? (:type @wd)) :type
+ (nil? (:name @wd)) :name
+ true :error)]
+ (if (= :parent cd-type)
+ (cond
+ (not (nil? (:name @wd))) (swap! wd #(dissoc % :name))
+ (not (nil? (:type @wd))) (swap! wd #(dissoc % :type))
+ (not (nil? (:namespace @wd))) (swap! wd #(dissoc % :namespace)))
+ (if (= :error cd-type)
+ (println "No such folder!!!")
+ (swap! wd #(assoc % cd-type name)) ))))
+
+(defn wd->bean-name [wd]
+ (cond
+ (nil? (:namespace wd)) "*:*"
+ (nil? (:type wd)) (str (:namespace wd) ":*")
+ (nil? (:name wd)) (str (:namespace wd) ":type=" (:type wd) ",*")
+ (= (:name wd) (:type wd)) (str (:namespace wd) ":type=" (:type wd))
+ true (str (:namespace wd) ":name=" (:name wd) ",type=" (:type wd))))
+
+(defn wd->path [wd]
+ (cond
+ (nil? (:namespace wd)) "/"
+ (nil? (:type wd)) (str "/" (:namespace wd))
+ (nil? (:name wd)) (str "/" (:namespace wd) "/" (:type wd))
+ (= (:name wd) (:type wd)) (str "/" (:namespace wd) "/" (:type wd))
+ true (str "/" (:namespace wd) "/" (:type wd) "/" (:name wd) )))
+
+
+
+
+(defn cat0 [attr-name]
+ (let [bean-fullname (wd->bean-name @wd)
+ attr-value (jmx/read bean-fullname (keyword attr-name))]
+ attr-value))
+
+(defn ls0 [bean-prefix]
+ (let [mbeans (->> (jmx/mbean-names bean-prefix) (map #(.getCanonicalName %)))]
+ mbeans))
+
+(defn help []
+ (let [help-text ["\t help -- print this help"
+ "\t ls -- list the items in current directory"
+ "\t cd -- enter a folder"
+ "\t cat -- print the value of an item"
+ "\t pwd -- show the current path"]]
+ (doseq [ht help-text]
+ (println (color/cyan ht)))))
+
+(loop [input "help"]
+ (try
+ (let [argv (string/split input #" ")
+ command (first argv)
+ argv (rest argv)]
+ (condp = command
+ "help" (help)
+ "ls" (ls)
+ "pwd" (pwd)
+ "cd" (apply cd argv)
+ "cat" (apply cat argv)
+ (help)))
+ (catch Throwable e
+ (println "ERROR: " e)))
+
+ (print (str "[" (color/green (pwd)) "] => "))
+ (flush)
+ (recur (read-line)))
+
+
@@ -0,0 +1,6 @@
+(ns jmx-repl.test.core
+ (:use [jmx-repl.core])
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit 8082300

Please sign in to comment.