From b803c8a4ee5e6bf84233f7be48afab8a68f1ba25 Mon Sep 17 00:00:00 2001 From: sundar Date: Mon, 5 Aug 2013 21:31:40 +0530 Subject: [PATCH] 8016531: jconsole-plugin script demo does not work with nashorn Reviewed-by: lagergren, hannesw Contributed-by: rieberandreas@gmail.com --- .../scripting/jconsole/ScriptShellPanel.java | 4 +- .../jconsole-plugin/src/resources/jconsole.js | 144 +++++++++++++----- .../jconsole-plugin/src/scripts/invoke.js | 4 +- .../jconsole-plugin/src/scripts/jstack.js | 10 +- .../jconsole-plugin/src/scripts/jtop.js | 12 +- .../jconsole-plugin/src/scripts/sysprops.js | 10 +- .../sample/scripting/scriptpad/README.txt | 2 +- .../scripting/scriptpad/src/resources/conc.js | 4 +- .../scripting/scriptpad/src/resources/mm.js | 57 ++++--- 9 files changed, 155 insertions(+), 92 deletions(-) diff --git a/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java b/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java index 40cf80881..c7e4dee51 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java +++ b/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -54,7 +54,7 @@ import javax.swing.text.*; * jconsole's script console. */ -class ScriptShellPanel extends JPanel { +public class ScriptShellPanel extends JPanel { private static final long serialVersionUID = 4116273141148726319L; diff --git a/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js b/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js index 715da1d9a..f39dea300 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js +++ b/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -77,12 +77,37 @@ function jcontext() { return plugin.getContext(); } -jcontext.docString = "returns JConsoleContext for the current jconsole plugin" +jcontext.docString = "returns JConsoleContext for the current jconsole plugin"; function mbeanConnection() { return jcontext().getMBeanServerConnection(); } -mbeanConnection.docString = "returns current MBeanServer connection" +mbeanConnection.docString = "returns current MBeanServer connection"; + +// check if there is a build in sync function, define one if missing +if (typeof sync === "undefined") { + var sync = function(func, obj) { + if (arguments.length < 1 || arguments.length > 2 ) { + throw "sync(function [,object]) parameter count mismatch"; + } + + var syncobj = (arguments.length == 2 ? obj : this); + + if (!syncobj._syncLock) { + syncobj._syncLock = new Lock(); + } + + return function() { + syncobj._syncLock.lock(); + try { + func.apply(null, arguments); + } finally { + syncobj._syncLock.unlock(); + } + }; + }; + sync.docString = "synchronize a function, optionally on an object"; +} /** * Prints one liner help message for each function exposed here @@ -188,22 +213,12 @@ 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; + return Java.to(array, "java.lang.Object[]"); } // 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; + return Java.to(array, "java.lang.String[]"); } // script array to Java List @@ -286,16 +301,18 @@ invokeMBean.docString = "invokes MBean operation on given ObjectName"; * will be of type FutureTask. When you need value, call 'get' on it. */ function mbean(objName, async) { + var index; + objName = objectName(objName); var info = mbeanInfo(objName); var attrs = info.attributes; var attrMap = new Object; - for (var index in attrs) { + for (index in attrs) { attrMap[attrs[index].name] = attrs[index]; } var opers = info.operations; var operMap = new Object; - for (var index in opers) { + for (index in opers) { operMap[opers[index].name] = opers[index]; } @@ -318,21 +335,30 @@ function mbean(objName, async) { } else { return getMBeanAttribute(objName, name); } - } else if (isOperation(name)) { + } else { + return undefined; + } + }, + __call__: function(name) { + 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); - } + + var params = []; + for (var j = 1; j < arguments.length; j++) { + params[j-1]= arguments[j]; + } + + 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; @@ -520,7 +546,7 @@ Function.prototype.sync = function (lock) { } finally { lock.unlock(); } -} +}; /** * Causes current thread to sleep for specified @@ -534,8 +560,7 @@ function sleep(interval) { sleep.docString = "wrapper for java.lang.Thread.sleep method"; /** - * Schedules a task to be executed once in - * every N milliseconds specified. + * Schedules a task to be executed once in N milliseconds specified. * * @param callback function or expression to evaluate * @param interval in milliseconds to sleep @@ -549,22 +574,61 @@ function setTimeout(callback, interval) { // start a new thread that sleeps given time // and calls callback in an infinite loop return (function() { - while (true) { + try { sleep(interval); + } catch (x) { } + callback(); + }).daemon(); +} +setTimeout.docString = "calls given callback once after specified interval"; + +/** + * Cancels a timeout set earlier. + * @param tid timeout ID returned from setTimeout + */ +function clearTimeout(tid) { + // we just interrupt the timer thread + tid.interrupt(); +} +clearTimeout.docString = "interrupt a setTimeout timer"; + +/** + * Schedules a task to be executed once in + * every N milliseconds specified. + * + * @param callback function or expression to evaluate + * @param interval in milliseconds to sleep + * @return timeout ID (which is nothing but Thread instance) + */ +function setInterval(callback, interval) { + if (! (callback instanceof Function)) { + callback = new Function(callback); + } + + // start a new thread that sleeps given time + // and calls callback in an infinite loop + return (function() { + while (true) { + try { + sleep(interval); + } catch (x) { + break; + } callback(); } }).daemon(); } -setTimeout.docString = "calls given callback once after specified interval" +setInterval.docString = "calls given callback every specified interval"; -/** +/** * Cancels a timeout set earlier. * @param tid timeout ID returned from setTimeout */ -function clearTimeout(tid) { +function clearInterval(tid) { // we just interrupt the timer thread tid.interrupt(); } +clearInterval.docString = "interrupt a setInterval timer"; /** * Simple access to thread local storage. @@ -680,7 +744,7 @@ function msgBox(msg, title, msgType) { if (msg === undefined) msg = "undefined"; if (msg === null) msg = "null"; if (title == undefined) title = msg; - if (msgType == undefined) type = JOptionPane.INFORMATION_MESSAGE; + if (msgType == undefined) msgType = JOptionPane.INFORMATION_MESSAGE; JOptionPane.showMessageDialog(window, msg, title, msgType); } if (isEventThread()) { @@ -800,7 +864,7 @@ echo.docString = "echoes arguments to interactive console screen"; * Clear the screen */ function clear() { - (function() { window.clear(false) }).invokeLater(); + (function() { window.clear(false); }).invokeLater(); } clear.docString = "clears interactive console screen"; diff --git a/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js b/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js index 8c66e0df3..919769bc4 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js +++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -53,6 +53,6 @@ * */ function resetPeakThreadCount() { - return invokeMBean("java.lang:type=Threading", "resetPeakThreadCount", [], ""); + return invokeMBean("java.lang:type=Threading", "resetPeakThreadCount", [], {}); } diff --git a/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js b/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js index b05dc198b..aff7d7c04 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js +++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -43,16 +43,16 @@ * threads.'jstack' function which can be called once or periodically * from a timer thread (calling it periodically would slow down the target * application). To call this once, just call 'jstack()' in script - * console prompt. To call jtop in a timer thread, you can use + * console prompt. To call jstack in a timer thread, you can use * - * var t = setTimeout(function () { jstack(print); }, 5000); + * var t = setInterval(function () { jstack(print); }, 5000); * * The above call prints threads in sorted order for every 5 seconds. * The print output goes to OS console window from which jconsole was * started. The timer can be cancelled later by clearTimeout() function * as shown below: * - * clearTimeout(t); + * clearInterval(t); */ @@ -87,7 +87,7 @@ function jstack(printFunc, maxFrames) { var tmbean = newPlatformMXBeanProxy( "java.lang:type=Threading", - java.lang.management.ThreadMXBean); + java.lang.management.ThreadMXBean.class); var tids = tmbean.allThreadIds; var tinfos = tmbean["getThreadInfo(long[],int)"](tids, maxFrames); diff --git a/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js b/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js index daebfc63a..2733b12a6 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js +++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -45,14 +45,14 @@ * To call this once, just call 'jtop()' in script console prompt. * To call jtop in a timer thread, you can use * - * var t = setTimeout(function () { jtop(print); }, 2000); + * var t = setInterval(function () { jtop(print); }, 2000); * * The above call prints threads in sorted order for every 2 seconds. * The print output goes to OS console window from which jconsole was * started. The timer can be cancelled later by clearTimeout() function * as shown below: - * - * clearTimeout(t); + * + * clearInterval(t); */ /** @@ -62,10 +62,10 @@ function getThreadList() { var tmbean = newPlatformMXBeanProxy( "java.lang:type=Threading", - java.lang.management.ThreadMXBean); + java.lang.management.ThreadMXBean.class); if (!tmbean.isThreadCpuTimeSupported()) { - return; + return java.util.Collections.EMPTY_LIST; } tmbean.setThreadCpuTimeEnabled(true); diff --git a/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js b/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js index d15d482ae..5ff958d83 100644 --- a/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js +++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -43,16 +43,16 @@ * properties.'sysprops' function which can be called once or periodically * from a timer thread (calling it periodically would slow down the target * application). To call this once, just call 'sysprops()' in script - * console prompt. To call jtop in a timer thread, you can use + * console prompt. To call sysprops in a timer thread, you can use * - * var t = setTimeout(function () { sysprops(print); }, 5000); + * var t = setInterval(function () { sysprops(print); }, 5000); * * The above call prints threads in sorted order for every 5 seconds. * The print output goes to OS console window from which jconsole was * started. The timer can be cancelled later by clearTimeout() function * as shown below: * - * clearTimeout(t); + * clearInterval(t); */ @@ -62,7 +62,7 @@ function getSystemProps() { var runtimeBean = newPlatformMXBeanProxy( "java.lang:type=Runtime", - java.lang.management.RuntimeMXBean); + java.lang.management.RuntimeMXBean.class); return runtimeBean.systemProperties; } diff --git a/src/share/sample/scripting/scriptpad/README.txt b/src/share/sample/scripting/scriptpad/README.txt index 446feae4e..ab4bd5fed 100644 --- a/src/share/sample/scripting/scriptpad/README.txt +++ b/src/share/sample/scripting/scriptpad/README.txt @@ -108,7 +108,7 @@ under the ./src/scripts directory. java -Dcom.sun.management.jmxremote.port=1090 \ -Dcom.sun.management.jmxremote.ssl=false \ -Dcom.sun.management.jmxremote.authenticate=false \ - -jar $JDK_HOME/demo/jfc/Java2D/Java2Demo.jar + -jar $JDK_HOME/demo/jfc/Font2DTest/Font2DTest.jar (2) Start scriptpad and click on "Tools->JMX Connect" menu. In the prompt, enter "localhost:1090" to connect to the above diff --git a/src/share/sample/scripting/scriptpad/src/resources/conc.js b/src/share/sample/scripting/scriptpad/src/resources/conc.js index 7e56fad47..aaca49aea 100644 --- a/src/share/sample/scripting/scriptpad/src/resources/conc.js +++ b/src/share/sample/scripting/scriptpad/src/resources/conc.js @@ -221,7 +221,7 @@ sleep.docString = "wrapper for java.lang.Thread.sleep method"; * @return timeout ID (which is nothing but Thread instance) */ function setTimeout(callback, interval) { - if (! (callback instanceof Function) && typeof callback !== "function") { + if (! (callback instanceof Function)) { callback = new Function(callback); } @@ -255,7 +255,7 @@ clearTimeout.docString = "interrupt a setTimeout timer"; * @return timeout ID (which is nothing but Thread instance) */ function setInterval(callback, interval) { - if (! (callback instanceof Function) && typeof callback !== "function") { + if (! (callback instanceof Function)) { callback = new Function(callback); } diff --git a/src/share/sample/scripting/scriptpad/src/resources/mm.js b/src/share/sample/scripting/scriptpad/src/resources/mm.js index 2b75f243b..07efad278 100644 --- a/src/share/sample/scripting/scriptpad/src/resources/mm.js +++ b/src/share/sample/scripting/scriptpad/src/resources/mm.js @@ -159,22 +159,12 @@ 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; + return Java.to(array, "java.lang.Object[]"); } // 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; + return Java.to(array, "java.lang.String[]"); } // script array to Java List @@ -284,26 +274,35 @@ function mbean(objName, async) { __get__: function (name) { if (isAttribute(name)) { if (async) { - return getMBeanAttribute.future(objName, name); + return getMBeanAttribute.future(objName, name); } else { - return getMBeanAttribute(objName, name); + return getMBeanAttribute(objName, name); } - } else if (isOperation(name)) { + } else { + return undefined; + } + }, + __call__: function(name) { + 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); - } - }; + + var params = []; + for (var j = 1; j < arguments.length; j++) { + params[j-1]= arguments[j]; + } + + 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; } -- GitLab