rlm@10: ;; JMX client APIs for Clojure rlm@10: ;; docs in clojure/contrib/jmx.clj!! rlm@10: rlm@10: ;; by Stuart Halloway rlm@10: rlm@10: ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use rlm@10: ;; and distribution terms for this software are covered by the Eclipse rlm@10: ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: ;; which can be found in the file epl-v10.html at the root of this rlm@10: ;; distribution. By using this software in any fashion, you are rlm@10: ;; agreeing to be bound by the terms of this license. You must not rlm@10: ;; remove this notice, or any other, from this software. rlm@10: rlm@10: rlm@10: (in-ns 'clojure.contrib.jmx) rlm@10: rlm@10: (defmacro with-connection rlm@10: "Execute body with JMX connection specified by opts. opts can also rlm@10: include an optional :environment key which is passed as the rlm@10: environment arg to JMXConnectorFactory/connect." rlm@10: [opts & body] rlm@10: `(let [opts# ~opts rlm@10: env# (get opts# :environment {}) rlm@10: opts# (dissoc opts# :environment)] rlm@10: (with-open [connector# (javax.management.remote.JMXConnectorFactory/connect rlm@10: (JMXServiceURL. (jmx-url opts#)) env#)] rlm@10: (binding [*connection* (.getMBeanServerConnection connector#)] rlm@10: ~@body)))) rlm@10: rlm@10: (defn mbean-info [n] rlm@10: (.getMBeanInfo *connection* (as-object-name n))) rlm@10: rlm@10: (defn raw-read rlm@10: "Read an mbean property. Returns low-level Java object model for rlm@10: composites, tabulars, etc. Most callers should use read." rlm@10: [n attr] rlm@10: (.getAttribute *connection* (as-object-name n) (as-str attr))) rlm@10: rlm@10: (defvar read rlm@10: (comp jmx->clj raw-read) rlm@10: "Read an mbean property.") rlm@10: rlm@10: (defn read-supported rlm@10: "Calls read to read an mbean property, *returning* unsupported rlm@10: operation exceptions instead of throwing them. Used to keep mbean rlm@10: from blowing up. Note: There is no good exception that aggregates rlm@10: unsupported operations, hence the overly-general catch block." rlm@10: [n attr] rlm@10: (try rlm@10: (read n attr) rlm@10: (catch Exception e rlm@10: e))) rlm@10: rlm@10: (defn write! [n attr value] rlm@10: (.setAttribute rlm@10: *connection* rlm@10: (as-object-name n) rlm@10: (Attribute. (as-str attr) value))) rlm@10: rlm@10: (defn attribute-info rlm@10: "Get the MBeanAttributeInfo for an attribute." rlm@10: [object-name attr-name] rlm@10: (filter #(= (as-str attr-name) (.getName %)) rlm@10: (.getAttributes (mbean-info object-name)))) rlm@10: rlm@10: (defn readable? rlm@10: "Is attribute readable?" rlm@10: [n attr] rlm@10: (.isReadable () (mbean-info n))) rlm@10: rlm@10: (defn operations rlm@10: "All oeprations available on an MBean." rlm@10: [n] rlm@10: (.getOperations (mbean-info n))) rlm@10: rlm@10: (defn operation rlm@10: "The MBeanOperationInfo for operation op on mbean n. Used by invoke." rlm@10: [n op] rlm@10: (first (filter #(= (-> % .getName keyword) op) (operations n)))) rlm@10: rlm@10: (defn op-param-types rlm@10: "The parameter types (as class name strings) for operation op on n. rlm@10: Used for invoke." rlm@10: [n op] rlm@10: (map #(-> % .getType) (.getSignature (operation n op)))) rlm@10: rlm@10: