comparison 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
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 ;; JMX client APIs for Clojure
2 ;; docs in clojure/contrib/jmx.clj!!
3
4 ;; by Stuart Halloway
5
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.
13
14
15 (in-ns 'clojure.contrib.jmx)
16
17 (defmacro with-connection
18 "Execute body with JMX connection specified by opts. opts can also
19 include an optional :environment key which is passed as the
20 environment arg to JMXConnectorFactory/connect."
21 [opts & body]
22 `(let [opts# ~opts
23 env# (get opts# :environment {})
24 opts# (dissoc opts# :environment)]
25 (with-open [connector# (javax.management.remote.JMXConnectorFactory/connect
26 (JMXServiceURL. (jmx-url opts#)) env#)]
27 (binding [*connection* (.getMBeanServerConnection connector#)]
28 ~@body))))
29
30 (defn mbean-info [n]
31 (.getMBeanInfo *connection* (as-object-name n)))
32
33 (defn raw-read
34 "Read an mbean property. Returns low-level Java object model for
35 composites, tabulars, etc. Most callers should use read."
36 [n attr]
37 (.getAttribute *connection* (as-object-name n) (as-str attr)))
38
39 (defvar read
40 (comp jmx->clj raw-read)
41 "Read an mbean property.")
42
43 (defn read-supported
44 "Calls read to read an mbean property, *returning* unsupported
45 operation exceptions instead of throwing them. Used to keep mbean
46 from blowing up. Note: There is no good exception that aggregates
47 unsupported operations, hence the overly-general catch block."
48 [n attr]
49 (try
50 (read n attr)
51 (catch Exception e
52 e)))
53
54 (defn write! [n attr value]
55 (.setAttribute
56 *connection*
57 (as-object-name n)
58 (Attribute. (as-str attr) value)))
59
60 (defn attribute-info
61 "Get the MBeanAttributeInfo for an attribute."
62 [object-name attr-name]
63 (filter #(= (as-str attr-name) (.getName %))
64 (.getAttributes (mbean-info object-name))))
65
66 (defn readable?
67 "Is attribute readable?"
68 [n attr]
69 (.isReadable () (mbean-info n)))
70
71 (defn operations
72 "All oeprations available on an MBean."
73 [n]
74 (.getOperations (mbean-info n)))
75
76 (defn operation
77 "The MBeanOperationInfo for operation op on mbean n. Used by invoke."
78 [n op]
79 (first (filter #(= (-> % .getName keyword) op) (operations n))))
80
81 (defn op-param-types
82 "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))))
86
87