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