1Scriptpad Sample 2 3* Introduction 4 5Scriptpad is a notepad like editor to open/edit/save and run 6script (JavaScript) files. This sample demonstrates the use of 7javax.script (JSR-223) API and JavaScript engine that is bundled 8with JDK 6. 9 10Scriptpad sample demonstrates how to use Javascript to use Java 11classes and objects to perform various tasks such as to modify, 12customize Swing GUI or to connect to a running application and 13monitor it using JMX (Java Management Extensions) API. 14 15* How to run Scriptpad? 16 17Scriptpad can be run with the following command: 18 19 java -jar ./build/scriptpad.jar 20 21(be sure to use the correct version of java). You can 22open/edit/save scripts using menu items under "File" menu. 23To run currently edited script, you can use "Tools->Run" menu. 24 25For example, you may enter 26 27 alert("hello, world"); 28 29in the editor and run the same with "Tools->Run" menu. 30You will see an alert box with the message "hello, world". 31 32In addition to being a simple script editor/runner, scriptpad 33can be used to connect to a JMX MBean server ("Tools->JMX Connect" 34menu). User can specify JMX hostname and port. After connecting, 35user can use "monitoring and management" script functions defined 36in "mm.js" (see below). 37 38* Scriptpad Sources 39 40com.sun.demo.scriptpad.Main class is the entry point of this 41sample. This class creates ScriptEngine and evaluates few 42JavaScript "files" -- which are stored as resources (please 43refer to src/resources/*.js). Actual code for the scriptpad's 44main functionality lives in these JavaScript files. 45 461. conc.js 47 -- simple concurrency utilities for JavaScript 48 492. gui.js 50 -- simple GUI utilities for JavaScript 51 523. mm.js 53 -- Monitoring and Management utilities for JavaScript 54 554. scriptpad.js 56 -- This creates main "notepad"-like GUI for open/edit/save 57 and run script files 58 595. Main.js 60 -- This script file can be used under "jrunscript" tool. 61 jrunscript is an experimental tool shipped with JDK (under 62 $JDK_HOME/bin directory). The scriptpad application can be 63 run by the following commands: 64 65 cd ./src/resources 66 $JDK_HOME/bin/jrunscript -f Main.js -f - 67 68 69* Extending Scriptpad: 70 71It is possible to extend scriptpad using scripts. There is a global 72object called "application". This object has 2 fields and a method. 73 74 Fields of the application object: 75 76 frame -> JFrame of the scriptpad 77 editor -> editor pane of the scriptpad 78 79 Method of the application object: 80 81 addTool -> adds a menu item under "Tools" menu 82 83 Example script to add "Tools->Hello" menu item: 84 85 application.addTool("Hello", 86 function() { alert("hello, world"); }); 87 88After running the above script, you can click Tools->Hello menu item 89and you'll see an alert box. 90 91Scriptpad customization may also be done by defining a file named 92"scriptpad.js" under your home directory,. If this file is found, 93scriptpad loads this file just after initializating everything. 94In your initialization file, you can additional script functions 95by "load" function. 96 97* Script Samples: 98 99On clicking the menu items under "Examples" menu, scriptpad shows 100built-in examples in the editor. Also, there are few script samples 101under the ./src/scripts directory. 102 103* Monitoring and Management with Scriptpad: 104 105(1) Start the application with the JMX agent - here's an example of 106 how the Java2D demo is started 107 108 java -Dcom.sun.management.jmxremote.port=1090 \ 109 -Dcom.sun.management.jmxremote.ssl=false \ 110 -Dcom.sun.management.jmxremote.authenticate=false \ 111 -jar $JDK_HOME/demo/jfc/Font2DTest/Font2DTest.jar 112 113(2) Start scriptpad and click on "Tools->JMX Connect" menu. 114 In the prompt, enter "localhost:1090" to connect to the above 115 program. 116 117After connecting to a MBeanServer (using "Tools->JMX Connect"), 118you can run any script that uses functions defined in "mm.js". 119For example, it is possible to load and run management scripts that 120are part of JConsole script shell plugin under the directory: 121 122 $JDK_HOME/demo/scripting/jconsole-plugin/src/scripts 123