annotate src/clojure/contrib/jmx/client.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
rev   line source
rlm@10 1 ;; JMX client APIs for Clojure
rlm@10 2 ;; docs in clojure/contrib/jmx.clj!!
rlm@10 3
rlm@10 4 ;; by Stuart Halloway
rlm@10 5
rlm@10 6 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14
rlm@10 15 (in-ns 'clojure.contrib.jmx)
rlm@10 16
rlm@10 17 (defmacro with-connection
rlm@10 18 "Execute body with JMX connection specified by opts. opts can also
rlm@10 19 include an optional :environment key which is passed as the
rlm@10 20 environment arg to JMXConnectorFactory/connect."
rlm@10 21 [opts & body]
rlm@10 22 `(let [opts# ~opts
rlm@10 23 env# (get opts# :environment {})
rlm@10 24 opts# (dissoc opts# :environment)]
rlm@10 25 (with-open [connector# (javax.management.remote.JMXConnectorFactory/connect
rlm@10 26 (JMXServiceURL. (jmx-url opts#)) env#)]
rlm@10 27 (binding [*connection* (.getMBeanServerConnection connector#)]
rlm@10 28 ~@body))))
rlm@10 29
rlm@10 30 (defn mbean-info [n]
rlm@10 31 (.getMBeanInfo *connection* (as-object-name n)))
rlm@10 32
rlm@10 33 (defn raw-read
rlm@10 34 "Read an mbean property. Returns low-level Java object model for
rlm@10 35 composites, tabulars, etc. Most callers should use read."
rlm@10 36 [n attr]
rlm@10 37 (.getAttribute *connection* (as-object-name n) (as-str attr)))
rlm@10 38
rlm@10 39 (defvar read
rlm@10 40 (comp jmx->clj raw-read)
rlm@10 41 "Read an mbean property.")
rlm@10 42
rlm@10 43 (defn read-supported
rlm@10 44 "Calls read to read an mbean property, *returning* unsupported
rlm@10 45 operation exceptions instead of throwing them. Used to keep mbean
rlm@10 46 from blowing up. Note: There is no good exception that aggregates
rlm@10 47 unsupported operations, hence the overly-general catch block."
rlm@10 48 [n attr]
rlm@10 49 (try
rlm@10 50 (read n attr)
rlm@10 51 (catch Exception e
rlm@10 52 e)))
rlm@10 53
rlm@10 54 (defn write! [n attr value]
rlm@10 55 (.setAttribute
rlm@10 56 *connection*
rlm@10 57 (as-object-name n)
rlm@10 58 (Attribute. (as-str attr) value)))
rlm@10 59
rlm@10 60 (defn attribute-info
rlm@10 61 "Get the MBeanAttributeInfo for an attribute."
rlm@10 62 [object-name attr-name]
rlm@10 63 (filter #(= (as-str attr-name) (.getName %))
rlm@10 64 (.getAttributes (mbean-info object-name))))
rlm@10 65
rlm@10 66 (defn readable?
rlm@10 67 "Is attribute readable?"
rlm@10 68 [n attr]
rlm@10 69 (.isReadable () (mbean-info n)))
rlm@10 70
rlm@10 71 (defn operations
rlm@10 72 "All oeprations available on an MBean."
rlm@10 73 [n]
rlm@10 74 (.getOperations (mbean-info n)))
rlm@10 75
rlm@10 76 (defn operation
rlm@10 77 "The MBeanOperationInfo for operation op on mbean n. Used by invoke."
rlm@10 78 [n op]
rlm@10 79 (first (filter #(= (-> % .getName keyword) op) (operations n))))
rlm@10 80
rlm@10 81 (defn op-param-types
rlm@10 82 "The parameter types (as class name strings) for operation op on n.
rlm@10 83 Used for invoke."
rlm@10 84 [n op]
rlm@10 85 (map #(-> % .getType) (.getSignature (operation n op))))
rlm@10 86
rlm@10 87