diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/jmx/client.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,87 @@
     1.4 +;; JMX client APIs for Clojure
     1.5 +;; docs in clojure/contrib/jmx.clj!!
     1.6 +
     1.7 +;; by Stuart Halloway
     1.8 +
     1.9 +;; Copyright (c) Stuart Halloway, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +
    1.18 +(in-ns 'clojure.contrib.jmx)
    1.19 +
    1.20 +(defmacro with-connection
    1.21 +  "Execute body with JMX connection specified by opts. opts can also
    1.22 +   include an optional :environment key which is passed as the
    1.23 +   environment arg to JMXConnectorFactory/connect."
    1.24 +  [opts & body]
    1.25 +  `(let [opts# ~opts
    1.26 +         env# (get opts# :environment {})
    1.27 +         opts# (dissoc opts# :environment)]
    1.28 +     (with-open [connector# (javax.management.remote.JMXConnectorFactory/connect
    1.29 +                             (JMXServiceURL. (jmx-url opts#)) env#)]
    1.30 +       (binding [*connection* (.getMBeanServerConnection connector#)]
    1.31 +         ~@body))))
    1.32 +
    1.33 +(defn mbean-info [n]
    1.34 +  (.getMBeanInfo *connection* (as-object-name n)))
    1.35 +
    1.36 +(defn raw-read
    1.37 +  "Read an mbean property. Returns low-level Java object model for
    1.38 +   composites, tabulars, etc. Most callers should use read."
    1.39 +  [n attr]
    1.40 +  (.getAttribute *connection* (as-object-name n) (as-str attr)))
    1.41 +
    1.42 +(defvar read
    1.43 +  (comp jmx->clj raw-read)
    1.44 +  "Read an mbean property.")
    1.45 +
    1.46 +(defn read-supported
    1.47 +  "Calls read to read an mbean property, *returning* unsupported
    1.48 +   operation exceptions instead of throwing them. Used to keep mbean
    1.49 +   from blowing up. Note: There is no good exception that aggregates
    1.50 +   unsupported operations, hence the overly-general catch block."
    1.51 +  [n attr]
    1.52 +  (try
    1.53 +   (read n attr)
    1.54 +   (catch Exception e
    1.55 +     e)))
    1.56 +
    1.57 +(defn write! [n attr value]
    1.58 +  (.setAttribute
    1.59 +   *connection*
    1.60 +   (as-object-name n)
    1.61 +   (Attribute. (as-str attr) value)))
    1.62 +
    1.63 +(defn attribute-info
    1.64 +  "Get the MBeanAttributeInfo for an attribute."
    1.65 +  [object-name attr-name]
    1.66 +  (filter #(= (as-str attr-name) (.getName %))
    1.67 +          (.getAttributes (mbean-info object-name))))
    1.68 +
    1.69 +(defn readable?
    1.70 +  "Is attribute readable?"
    1.71 +  [n attr]
    1.72 +  (.isReadable () (mbean-info n)))
    1.73 +
    1.74 +(defn operations
    1.75 +  "All oeprations available on an MBean."
    1.76 +  [n]
    1.77 +  (.getOperations (mbean-info n)))
    1.78 +
    1.79 +(defn operation
    1.80 +  "The MBeanOperationInfo for operation op on mbean n. Used by invoke."
    1.81 +  [n op]
    1.82 +  (first  (filter #(= (-> % .getName keyword) op) (operations n))))
    1.83 +
    1.84 +(defn op-param-types 
    1.85 +  "The parameter types (as class name strings) for operation op on n.
    1.86 +   Used for invoke."
    1.87 +  [n op]
    1.88 +  (map #(-> % .getType) (.getSignature (operation n op))))
    1.89 +
    1.90 +