view src/clojure/contrib/jmx/data.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 source
1 ;; Conversions between JMX data structures and idiomatic Clojure
2 ;; docs in clojure/contrib/jmx.clj!!
4 ;; by Stuart Halloway
6 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use
7 ;; and distribution terms for this software are covered by the Eclipse
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 ;; which can be found in the file epl-v10.html at the root of this
10 ;; distribution. By using this software in any fashion, you are
11 ;; agreeing to be bound by the terms of this license. You must not
12 ;; remove this notice, or any other, from this software.
15 (in-ns 'clojure.contrib.jmx)
17 (declare jmx->clj)
19 (defn jmx-url
20 "Build a JMX URL from options."
21 ([] (jmx-url {}))
22 ([overrides]
23 (let [opts (merge {:host "localhost", :port "3000", :jndi-path "jmxrmi"} overrides)]
24 (format "service:jmx:rmi:///jndi/rmi://%s:%s/%s" (opts :host) (opts :port) (opts :jndi-path)))))
26 (defmulti as-object-name
27 "Interpret an object as a JMX ObjectName."
28 { :arglists '([string-or-name]) }
29 class)
30 (defmethod as-object-name String [n] (ObjectName. n))
31 (defmethod as-object-name ObjectName [n] n)
33 (defn composite-data->map [cd]
34 (into {}
35 (map (fn [attr] [(keyword attr) (jmx->clj (.get cd attr))])
36 (.. cd getCompositeType keySet))))
38 (defn maybe-keywordize
39 "Convert a string key to a keyword, leaving other types alone. Used to
40 simplify keys in the tabular data API."
41 [s]
42 (if (string? s) (keyword s) s))
44 (defn maybe-atomize
45 "Convert a list of length 1 into its contents, leaving other things alone.
46 Used to simplify keys in the tabular data API."
47 [k]
48 (if (and (instance? java.util.List k)
49 (= 1 (count k)))
50 (first k)
51 k))
53 (defvar simplify-tabular-data-key
54 (comp maybe-keywordize maybe-atomize))
56 (defn tabular-data->map [td]
57 (into {}
58 ; the need for into-array here was a surprise, and may not
59 ; work for all examples. Are keys always arrays?
60 (map (fn [k]
61 [(simplify-tabular-data-key k) (jmx->clj (.get td (into-array k)))])
62 (.keySet td))))
64 (defmulti jmx->clj
65 "Coerce JMX data structures into Clojure data.
66 Handles CompositeData, TabularData, maps, and atoms."
67 { :argslists '([jmx-data-structure]) }
68 (fn [x]
69 (cond
70 (instance? javax.management.openmbean.CompositeData x) :composite
71 (instance? javax.management.openmbean.TabularData x) :tabular
72 (instance? clojure.lang.Associative x) :map
73 :default :default)))
74 (defmethod jmx->clj :composite [c] (composite-data->map c))
75 (defmethod jmx->clj :tabular [t] (tabular-data->map t))
76 (defmethod jmx->clj :map [m] (into {} (zipmap (keys m) (map jmx->clj (vals m)))))
77 (defmethod jmx->clj :default [obj] obj)
79 (def guess-attribute-map
80 {"java.lang.Integer" "int"
81 "java.lang.Boolean" "boolean"
82 "java.lang.Long" "long"
83 })
85 (defn guess-attribute-typename
86 "Guess the attribute typename for MBeanAttributeInfo based on the attribute value."
87 [value]
88 (let [classname (.getName (class value))]
89 (get guess-attribute-map classname classname)))
91 (defn build-attribute-info
92 "Construct an MBeanAttributeInfo. Normally called with a key/value pair from a Clojure map."
93 ([attr-name attr-value]
94 (build-attribute-info
95 (as-str attr-name)
96 (guess-attribute-typename attr-value)
97 (as-str attr-name) true false false))
98 ([name type desc readable? writable? is?] (MBeanAttributeInfo. name type desc readable? writable? is? )))
100 (defn map->attribute-infos
101 "Construct an MBeanAttributeInfo[] from a Clojure associative."
102 [attr-map]
103 (into-array (map (fn [[attr-name value]] (build-attribute-info attr-name value))
104 attr-map)))