changeset 7799:87367a1c7f76

8016531: jconsole-plugin script demo does not work with nashorn Reviewed-by: lagergren, hannesw Contributed-by: rieberandreas@gmail.com
author sundar
date Mon, 05 Aug 2013 21:31:40 +0530
parents 80da091343af
children 31759750ff63 1f4af3e0447e
files src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js src/share/sample/scripting/scriptpad/README.txt src/share/sample/scripting/scriptpad/src/resources/conc.js src/share/sample/scripting/scriptpad/src/resources/mm.js
diffstat 9 files changed, 156 insertions(+), 93 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/com/sun/demo/scripting/jconsole/ScriptShellPanel.java	Mon Aug 05 21:31:40 2013 +0530
@@ -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 @@
  * jconsole's script console.
  */
 
-class ScriptShellPanel extends JPanel {
+public class ScriptShellPanel extends JPanel {
 
     private static final long serialVersionUID = 4116273141148726319L;
 
--- a/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/resources/jconsole.js	Mon Aug 05 21:31:40 2013 +0530
@@ -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 @@
 
 // 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 @@
  * 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 @@
                 } 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 @@
     } finally {
         lock.unlock();
     }
-}
+};
 
 /**
  * Causes current thread to sleep for specified
@@ -534,8 +560,7 @@
 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,15 +574,15 @@
     // start a new thread that sleeps given time
     // and calls callback in an infinite loop
     return (function() {
-         while (true) {
+         try {
              sleep(interval);
-             callback();
-         }
+         } catch (x) { }
+         callback();
     }).daemon();
 }
-setTimeout.docString = "calls given callback once after specified interval"
+setTimeout.docString = "calls given callback once after specified interval";
 
-/** 
+/**
  * Cancels a timeout set earlier.
  * @param tid timeout ID returned from setTimeout
  */
@@ -565,6 +590,45 @@
     // 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();
+}
+setInterval.docString = "calls given callback every specified interval";
+
+/**
+ * Cancels a timeout set earlier.
+ * @param tid timeout ID returned from setTimeout
+ */
+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 @@
         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 @@
  * Clear the screen
  */
 function clear() {
-    (function() { window.clear(false) }).invokeLater();
+    (function() { window.clear(false); }).invokeLater();
 }
 clear.docString = "clears interactive console screen";
 
--- a/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/invoke.js	Mon Aug 05 21:31:40 2013 +0530
@@ -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", [], {});    
 }
 
--- a/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/jstack.js	Mon Aug 05 21:31:40 2013 +0530
@@ -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 @@
 
     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);
--- a/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/jtop.js	Mon Aug 05 21:31:40 2013 +0530
@@ -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);
--- a/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/demo/scripting/jconsole-plugin/src/scripts/sysprops.js	Mon Aug 05 21:31:40 2013 +0530
@@ -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;
 }
 
--- a/src/share/sample/scripting/scriptpad/README.txt	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/sample/scripting/scriptpad/README.txt	Mon Aug 05 21:31:40 2013 +0530
@@ -108,7 +108,7 @@
       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
--- a/src/share/sample/scripting/scriptpad/src/resources/conc.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/sample/scripting/scriptpad/src/resources/conc.js	Mon Aug 05 21:31:40 2013 +0530
@@ -221,7 +221,7 @@
  * @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 @@
  * @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);
     }
 
--- a/src/share/sample/scripting/scriptpad/src/resources/mm.js	Mon Aug 05 07:50:16 2013 -0700
+++ b/src/share/sample/scripting/scriptpad/src/resources/mm.js	Mon Aug 05 21:31:40 2013 +0530
@@ -159,22 +159,12 @@
 
 // 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 @@
         __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;
             }