This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjolokia-simple.js
291 lines (275 loc) · 11.1 KB
/
jolokia-simple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2009-2013 Roland Huss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Simplified access to the Jolokia agent.
*
* This script will add convenience methods to <code>Jolokia</code> for
* simplified access to JMX information. Before including this script, "jolokia.js"
* must be included.
*
* It is recommended to compress this script before using it in production.
*
* @author roland
*/
(function() {
var builder = function($,Jolokia) {
/**
* Get one or more attributes
*
* @param mbean objectname of MBean to query. Can be a pattern.
* @param attribute attribute name. If an array, multiple attributes are fetched.
* If <code>null</code>, all attributes are fetched.
* @param path optional path within the return value. For multi-attribute fetch, the path
* is ignored.
* @param opts options passed to Jolokia.request()
* @return the value of the attribute, possibly a complex object
*/
function getAttribute(mbean,attribute,path,opts) {
if (arguments.length === 3 && $.isPlainObject(path)) {
opts = path;
path = null;
} else if (arguments.length == 2 && $.isPlainObject(attribute)) {
opts = attribute;
attribute = null;
path = null;
}
var req = { type: "read", mbean: mbean, attribute: attribute };
addPath(req,path);
return extractValue(this.request(req,prepareSucessCallback(opts)),opts);
}
/**
* Set an attribute on a MBean.
*
* @param mbean objectname of MBean to set
* @param attribute the attribute to set
* @param value the value to set
* @param path an optional <em>inner path</em> which, when given, is used to determine
* an inner object to set the value on
* @param opts additional options passed to Jolokia.request()
* @return the previous value
*/
function setAttribute(mbean,attribute,value,path,opts) {
if (arguments.length === 4 && $.isPlainObject(path)) {
opts = path;
path = null;
}
var req = { type: "write", mbean: mbean, attribute: attribute, value: value };
addPath(req,path);
return extractValue(this.request(req,prepareSucessCallback(opts)),opts);
}
/**
* Execute a JMX operation and return the result value
*
* @param mbean objectname of the MBean to operate on
* @param operation name of operation to execute. Can contain a signature in case overloaded
* operations are to be called (comma separated fully qualified argument types
* append to the operation name within parentheses)
* @param arg1, arg2, ..... one or more argument required for executing the operation.
* @param opts optional options for Jolokia.request() (must be an object)
* @return the return value of the JMX operation.
*/
function execute(mbean,operation) {
var req = { type: "exec", mbean: mbean, operation: operation };
var opts, end = arguments.length;
if (arguments.length > 2 && $.isPlainObject(arguments[arguments.length-1])) {
opts = arguments[arguments.length-1];
end = arguments.length-1;
}
if (end > 2) {
var args = [];
for (var i = 2; i < end; i++) {
args[i-2] = arguments[i];
}
req.arguments = args;
}
return extractValue(this.request(req,prepareSucessCallback(opts)),opts);
}
/**
* Search for MBean based on a pattern and return a reference to the list of found
* MBeans names (as string). If no MBean can be found, <code>null</code> is returned. For
* example,
*
* jolokia.search("*:j2eeType=J2EEServer,*")
*
* searches all MBeans whose name are matching this pattern, which are according
* to JSR77 all application servers in all available domains.
*
* @param mbeanPattern pattern to search for
* @param opts optional options for Jolokia.request()
* @return an array with ObjectNames as string
*/
function search(mbeanPattern,opts) {
var req = { type: "search", mbean: mbeanPattern};
return extractValue(this.request(req,prepareSucessCallback(opts)),opts);
}
/**
* This method return the version of the agent and the Jolokia protocol
* version as part of an object. If available, server specific information
* like the application server's name are returned as wel.
* A typical response looks like
*
* <pre>
* {
* protocol: "4.0",
* agent: "0.82",
* info: {
* product: "glassfish",
* vendor": "Sun",
* extraInfo: {
* amxBooted: false
* }
* }
* </pre>
*
* @param opts optional options for Jolokia.request()
* @param version and other meta information as object
*/
function version(opts) {
return extractValue(this.request({type: "version"},prepareSucessCallback(opts)),opts);
}
/**
* Get all MBeans as registered at the specified server. A C<$path> can be
* specified in order to fetchy only a subset of the information. When no path is
* given, the returned value has the following format
*
* <pre>
* {
* <domain> :
* {
* <canonical property list> :
* {
* "attr" :
* {
* <atrribute name> :
* {
* desc : <description of attribute>
* type : <java type>,
* rw : true/false
* },
* ....
* },
* "op" :
* {
* <operation name> :
* {
* "desc" : <description of operation>
* "ret" : <return java type>
* "args" :
* [
* {
* "desc" : <description>,
* "name" : <name>,
* "type" : <java type>
* },
* ....
* ]
* },
* ....
* },
* ....
* }
* ....
* }
* </pre>
*
* A complete path has the format <domain>/property
* list>/("attribute"|"operation")/<index>">
* (e.g. <code>java.lang/name=Code Cache,type=MemoryPool/attribute/0</code>). A path can be
* provided partially, in which case the remaining map/array is returned. The path given must
* be already properly escaped (i.e. slashes must be escaped like <code>!/</code> and exlamation
* marks like <code>!!</code>.
* See also the Jolokia Reference Manual for a more detailed discussion of inner paths and escaping.
*
*
* @param path optional path for diving into the list
* @param opts optional opts passed to Jolokia.request()
*/
function list(path,opts) {
if (arguments.length == 1 && !$.isArray(path) && $.isPlainObject(path)) {
opts = path;
path = null;
}
var req = { type: "list" };
addPath(req,path);
return extractValue(this.request(req,prepareSucessCallback(opts)),opts);
}
// =======================================================================
// Private methods:
// If path is an array, the elements get escaped. If not, it is
// taken directly
function addPath(req,path) {
if (path != null) {
if ($.isArray(path)) {
req.path = $.map(path,Jolokia.escape).join("/");
} else {
req.path = path;
}
}
}
function extractValue(response,opts) {
if (response == null) {
return null;
}
if (response.status == 200) {
return response.value;
}
if (opts && opts.error) {
return opts.error(response);
} else {
throw new Error("Jolokia-Error: " + JSON.stringify(response));
}
}
// Prepare callback to receive directly the value (instead of the full blown response)
function prepareSucessCallback(opts) {
if (opts && opts.success) {
var parm = $.extend({},opts);
parm.success = function(resp) {
opts.success(resp.value);
};
return parm;
} else {
return opts;
}
}
// Extend the Jolokia prototype with new functionality (mixin)
$.extend(Jolokia.prototype,
{
"getAttribute" : getAttribute,
"setAttribute" : setAttribute,
"execute": execute,
"search": search,
"version": version,
"list": list
});
return Jolokia;
};
// =====================================================================================================
// Register either at the global Jolokia object global or as an AMD module
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as a named module
define(["jquery","./jolokia"], factory);
} else {
if (root.Jolokia) {
builder(jQuery,root.Jolokia);
} else {
console.error("No Jolokia definition found. Please include jolokia.js before jolokia-simple.js");
}
}
}(this, function (jQuery,Jolokia) {
return builder(jQuery,Jolokia);
}));
})();