📄 jconsole.js
字号:
/* * @(#)jconsole.js 1.3 06/07/18 06:17:49 * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */// This function depends on the pre-defined variable// "plugin" of type com.sun.tools.jconsole.JConsolePluginfunction jcontext() { return plugin.getContext();}jcontext.docString = "returns JConsoleContext for the current jconsole plugin" function mbeanConnection() { return jcontext().getMBeanServerConnection();}mbeanConnection.docString = "returns current MBeanServer connection"/** * Prints one liner help message for each function exposed here * Note that this function depends on docString meta-data for * each function */function help() { var i; for (i in this) { var func = this[i]; if (typeof(func) == "function" && ("docString" in func)) { echo(i + " - " + func["docString"]); } }}help.docString = "prints help message for global functions";function connectionState() { return jcontext().connectionState;}connectionState.docString = "return connection state of the current jcontext";/** * Returns a platform MXBean proxy for given MXBean name and interface class */function newPlatformMXBeanProxy(name, intf) { var factory = java.lang.management.ManagementFactory; return factory.newPlatformMXBeanProxy(mbeanConnection(), name, intf);}newPlatformMXBeanProxy.docString = "returns a proxy for a platform MXBean";/** * Wraps a string to ObjectName if needed. */function objectName(objName) { var ObjectName = Packages.javax.management.ObjectName; if (objName instanceof ObjectName) { return objName; } else { return new ObjectName(objName); }}objectName.docString = "creates JMX ObjectName for a given String";/** * Creates a new (M&M) Attribute object * * @param name name of the attribute * @param value value of the attribute */function attribute(name, value) { var Attribute = Packages.javax.management.Attribute; return new Attribute(name, value);}attribute.docString = "returns a new JMX Attribute using name and value given";/** * Returns MBeanInfo for given ObjectName. Strings are accepted. */function mbeanInfo(objName) { objName = objectName(objName); return mbeanConnection().getMBeanInfo(objName);}mbeanInfo.docString = "returns MBeanInfo of a given ObjectName";/** * Returns ObjectInstance for a given ObjectName. */function objectInstance(objName) { objName = objectName(objName); return mbeanConnection().objectInstance(objectName);}objectInstance.docString = "returns ObjectInstance for a given ObjectName";/** * Queries with given ObjectName and QueryExp. * QueryExp may be null. * * @return set of ObjectNames. */function queryNames(objName, query) { objName = objectName(objName); if (query == undefined) query = null; return mbeanConnection().queryNames(objName, query);}queryNames.docString = "returns QueryNames using given ObjectName and optional query";/** * Queries with given ObjectName and QueryExp. * QueryExp may be null. * * @return set of ObjectInstances. */function queryMBeans(objName, query) { objName = objectName(objName); if (query == undefined) query = null; return mbeanConnection().queryMBeans(objName, query);}queryMBeans.docString = "return MBeans using given ObjectName and optional query";// wraps a script array as java.lang.Object[]function objectArray(array) { var len = array.length; var res = java.lang.reflect.Array.newInstance(java.lang.Object, len); for (var i = 0; i < array.length; i++) { res[i] = array[i]; } return res;}// wraps a script (string) array as java.lang.String[]function stringArray(array) { var len = array.length; var res = java.lang.reflect.Array.newInstance(java.lang.String, len); for (var i = 0; i < array.length; i++) { res[i] = String(array[i]); } return res;}// script array to Java Listfunction toAttrList(array) { var AttributeList = Packages.javax.management.AttributeList; if (array instanceof AttributeList) { return array; } var list = new AttributeList(array.length); for (var index = 0; index < array.length; index++) { list.add(array[index]); } return list;}// Java Collection (Iterable) to script arrayfunction toArray(collection) { if (collection instanceof Array) { return collection; } var itr = collection.iterator(); var array = new Array(); while (itr.hasNext()) { array[array.length] = itr.next(); } return array;}// gets MBean attributesfunction getMBeanAttributes(objName, attributeNames) { objName = objectName(objName); return mbeanConnection().getAttributes(objName,stringArray(attributeNames));}getMBeanAttributes.docString = "returns specified Attributes of given ObjectName";// gets MBean attributefunction getMBeanAttribute(objName, attrName) { objName = objectName(objName); return mbeanConnection().getAttribute(objName, attrName);}getMBeanAttribute.docString = "returns a single Attribute of given ObjectName";// sets MBean attributesfunction setMBeanAttributes(objName, attrList) { objName = objectName(objName); attrList = toAttrList(attrList); return mbeanConnection().setAttributes(objName, attrList);}setMBeanAttributes.docString = "sets specified Attributes of given ObjectName";// sets MBean attributefunction setMBeanAttribute(objName, attrName, attrValue) { var Attribute = Packages.javax.management.Attribute; objName = objectName(objName); mbeanConnection().setAttribute(objName, new Attribute(attrName, attrValue));}setMBeanAttribute.docString = "sets a single Attribute of given ObjectName";// invokes an operation on given MBeanfunction invokeMBean(objName, operation, params, signature) { objName = objectName(objName); params = objectArray(params); signature = stringArray(signature); return mbeanConnection().invoke(objName, operation, params, signature);}invokeMBean.docString = "invokes MBean operation on given ObjectName";/** * Wraps a MBean specified by ObjectName as a convenient * script object -- so that setting/getting MBean attributes * and invoking MBean method can be done with natural syntax. * * @param objName ObjectName of the MBean * @param async asynchornous mode [optional, default is false] * @return script wrapper for MBean * * With async mode, all field, operation access is async. Results * will be of type FutureTask. When you need value, call 'get' on it. */function mbean(objName, async) { objName = objectName(objName); var info = mbeanInfo(objName); var attrs = info.attributes; var attrMap = new Object; for (var index in attrs) { attrMap[attrs[index].name] = attrs[index]; } var opers = info.operations; var operMap = new Object; for (var index in opers) { operMap[opers[index].name] = opers[index]; } function isAttribute(name) { return name in attrMap; } function isOperation(name) { return name in operMap; } return new JSAdapter() { __has__: function (name) { return isAttribute(name) || isOperation(name); }, __get__: function (name) { if (isAttribute(name)) { if (async) { return getMBeanAttribute.future(objName, name); } else { return getMBeanAttribute(objName, name); } } else if (isOperation(name)) { var oper = operMap[name]; return function() { var params = objectArray(arguments); var sigs = oper.signature; var sigNames = new Array(sigs.length); for (var index in sigs) { sigNames[index] = sigs[index].getType(); } if (async) { return invokeMBean.future(objName, name, params, sigNames); } else { return invokeMBean(objName, name, params, sigNames); } } } else { return undefined; } }, __put__: function (name, value) { if (isAttribute(name)) { if (async) { setMBeanAttribute.future(objName, name, value); } else { setMBeanAttribute(objName, name, value); } } else { return undefined; } } };}mbean.docString = "returns a conveninent script wrapper for a MBean of given ObjectName";/** * load and evaluate script file. If no script file is * specified, file dialog is shown to choose the script. * * @param file script file name [optional] * @return value returned from evaluating script */function load(file) { if (file == undefined || file == null) { // file not specified, show file dialog to choose file = fileDialog(); } if (file == null) return; var reader = new java.io.FileReader(file); var oldFilename = engine.get(engine.FILENAME); engine.put(engine.FILENAME, file); try { engine.eval(reader); } finally { engine.put(engine.FILENAME, oldFilename); } reader.close();}load.docString = "loads a script file and evaluates it";/** * Concurrency utilities for JavaScript. These are based on * java.lang and java.util.concurrent API. The following functions * provide a simpler API for scripts. Instead of directly using java.lang * and java.util.concurrent classes, scripts can use functions and * objects exported from here. *//** * Wrapper for java.lang.Object.wait * * can be called only within a sync method */function wait(object) { var objClazz = java.lang.Class.forName('java.lang.Object'); var waitMethod = objClazz.getMethod('wait', null); waitMethod.invoke(object, null);}wait.docString = "convenient wrapper for java.lang.Object.wait method";/** * Wrapper for java.lang.Object.notify * * can be called only within a sync method */function notify(object) { var objClazz = java.lang.Class.forName('java.lang.Object'); var notifyMethod = objClazz.getMethod('notify', null); notifyMethod.invoke(object, null);}notify.docString = "convenient wrapper for java.lang.Object.notify method";/** * Wrapper for java.lang.Object.notifyAll * * can be called only within a sync method */function notifyAll(object) { var objClazz = java.lang.Class.forName('java.lang.Object'); var notifyAllMethod = objClazz.getMethod('notifyAll', null); notifyAllMethod.invoke(object, null);}notifyAll.docString = "convenient wrapper for java.lang.Object.notifyAll method";/** * Creates a java.lang.Runnable from a given script * function. */Function.prototype.runnable = function() { var args = arguments;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -