Mercurial > lasercutter
view 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 source
1 ;; JMX client APIs for Clojure2 ;; docs in clojure/contrib/jmx.clj!!4 ;; by Stuart Halloway6 ;; Copyright (c) Stuart Halloway, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.15 (in-ns 'clojure.contrib.jmx)17 (defmacro with-connection18 "Execute body with JMX connection specified by opts. opts can also19 include an optional :environment key which is passed as the20 environment arg to JMXConnectorFactory/connect."21 [opts & body]22 `(let [opts# ~opts23 env# (get opts# :environment {})24 opts# (dissoc opts# :environment)]25 (with-open [connector# (javax.management.remote.JMXConnectorFactory/connect26 (JMXServiceURL. (jmx-url opts#)) env#)]27 (binding [*connection* (.getMBeanServerConnection connector#)]28 ~@body))))30 (defn mbean-info [n]31 (.getMBeanInfo *connection* (as-object-name n)))33 (defn raw-read34 "Read an mbean property. Returns low-level Java object model for35 composites, tabulars, etc. Most callers should use read."36 [n attr]37 (.getAttribute *connection* (as-object-name n) (as-str attr)))39 (defvar read40 (comp jmx->clj raw-read)41 "Read an mbean property.")43 (defn read-supported44 "Calls read to read an mbean property, *returning* unsupported45 operation exceptions instead of throwing them. Used to keep mbean46 from blowing up. Note: There is no good exception that aggregates47 unsupported operations, hence the overly-general catch block."48 [n attr]49 (try50 (read n attr)51 (catch Exception e52 e)))54 (defn write! [n attr value]55 (.setAttribute56 *connection*57 (as-object-name n)58 (Attribute. (as-str attr) value)))60 (defn attribute-info61 "Get the MBeanAttributeInfo for an attribute."62 [object-name attr-name]63 (filter #(= (as-str attr-name) (.getName %))64 (.getAttributes (mbean-info object-name))))66 (defn readable?67 "Is attribute readable?"68 [n attr]69 (.isReadable () (mbean-info n)))71 (defn operations72 "All oeprations available on an MBean."73 [n]74 (.getOperations (mbean-info n)))76 (defn operation77 "The MBeanOperationInfo for operation op on mbean n. Used by invoke."78 [n op]79 (first (filter #(= (-> % .getName keyword) op) (operations n))))81 (defn op-param-types82 "The parameter types (as class name strings) for operation op on n.83 Used for invoke."84 [n op]85 (map #(-> % .getType) (.getSignature (operation n op))))