Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

This approach works:

* runner than reads from jar files
* put the .js files in a jar
* sign the jar
  • Loading branch information...
commit 9ca9a1776ff90326bf0a3d7a494a4d2b7f2e3039 1 parent c17c70f
@kylecordes authored
View
14 build.xml
@@ -16,28 +16,34 @@
</target>
<target name="init">
+ <mkdir dir="dist/classes" />
<mkdir dir="dist/web" />
<mkdir dir="dist/install" />
</target>
- <target name="jar" depends="init">
+ <target name="compile" depends="init">
+ <javac destdir="dist/classes" optimize="true" source="1.6" target="1.6" classpathref="cp.all">
+ <src path="src" />
+ </javac>
+ </target>
+
+ <target name="jar" depends="compile">
<delete>
<fileset dir="dist" includes="**/rsta.jar" />
</delete>
<jar destfile="dist/install/rsta.jar" compress="true">
+ <fileset dir="dist/classes" />
<fileset dir="js" includes="*.js" />
- <!--
<manifest>
<attribute name="Main-Class" value="Runner" />
<attribute name="Class-Path" value="js.jar" />
</manifest>
- -->
</jar>
</target>
<target name="dist" depends="jar">
- <echo file="dist/install/rsta.bat" message="@start javaw -jar js.jar -strict jar:file:rsta.jar!/application.js" />
+ <echo file="dist/install/rsta.bat" message="@start javaw -jar rsta.jar" />
<copy todir="dist/install">
<fileset dir="lib" includes="*.jar" />
View
25 js/application.js
@@ -8,32 +8,25 @@ var frame = new JFrame("Rhino Swing Test gApplication");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
-var labelPrefix = "Number of button clicks: ";
+var labelPrefix = "Button clicks: ";
var numClicks = 0;
var label = new JLabel(labelPrefix + numClicks);
-var button = new JButton("I'm a Swing button!");
+var button = new JButton("Count Another Click");
button.mnemonic = KeyEvent.VK_I;
button.addActionListener( function() {
numClicks += 1;
label.setText(labelPrefix + numClicks);
});
-label.setLabelFor(button);
-
-var pane = new JPanel();
-pane.border = BorderFactory.createEmptyBorder(30, 30, 10, 30);
-pane.setLayout(new GridLayout(0, 1));
-pane.add(button);
-pane.add(label);
-button = new JButton("Click Me");
-
-function clicked() {
- print("button was clicked");
-}
+label.setLabelFor(button);
-button.addActionListener(clicked);
+var panel = new JPanel();
+panel.border = BorderFactory.createEmptyBorder(30, 30, 10, 30);
+panel.setLayout(new GridLayout(0, 1));
+panel.add(button);
+panel.add(label);
-frame.add(pane);
+frame.add(panel);
frame.pack();
frame.show();
View
4 js/launch.js
@@ -0,0 +1,4 @@
+// The Runner class automatically runs the contents of this
+// file; use it to bootstrap the real code (in other files).
+
+intf.load("application.js");
View
7 misc/rsta.jnlp
@@ -13,11 +13,8 @@
</security>
<resources>
<j2se version="1.6+" initial-heap-size="64M" max-heap-size="512M" />
- <jar href="js.jar" />
<jar href="rsta.jar" />
+ <jar href="js.jar" />
</resources>
- <application-desc main-class="org.mozilla.javascript.tools.shell.Main">
- <argument>-strict</argument>
- <argument>jar:file:rsta.jar!/application.js</argument>
- </application-desc>
+ <application-desc main-class="Runner" />
</jnlp>
View
130 src/Runner.java
@@ -0,0 +1,130 @@
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.EvaluatorException;
+import org.mozilla.javascript.Function;
+import org.mozilla.javascript.ImporterTopLevel;
+import org.mozilla.javascript.JavaScriptException;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.WrappedException;
+
+public class Runner extends ScriptableObject {
+
+ private static final long serialVersionUID = -3177482493159110328L;
+ private Scriptable topLevelScope;
+
+ public String getClassName() {
+ return "global";
+ }
+
+ public static void main(String args[]) {
+ new Runner().go(args);
+ }
+
+ private void go(String args[]) {
+ Context cx = new ContextFactory().enterContext();
+ try {
+ cx.setLanguageVersion(170); // This is the latest, as of July 2008.
+
+ // publish a subset of what the "Shell" offers
+ String[] names = { "print", "load" };
+ defineFunctionProperties(names, Runner.class,
+ ScriptableObject.DONTENUM);
+
+ // Publish command line args, might be useful for JWS launching
+ Object[] array;
+ if (args.length == 0) {
+ array = new Object[0];
+ } else {
+ int length = args.length - 1;
+ array = new Object[length];
+ System.arraycopy(args, 1, array, 0, length);
+ }
+ Scriptable argsObj = cx.newArray(this, array);
+ defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
+
+ String launchJS = "launch.js";
+ if (args.length > 0) {
+ launchJS = args[0];
+ }
+
+ topLevelScope = new ImporterTopLevel(cx);
+ topLevelScope.put("intf", topLevelScope, this);
+
+ processSource(cx, launchJS);
+ } finally {
+ Context.exit();
+ }
+ }
+
+ public static void print(Context cx, Scriptable thisObj, Object[] args,
+ Function funObj) {
+ for (int i = 0; i < args.length; i++) {
+ if (i > 0)
+ System.out.print(" ");
+
+ String s = Context.toString(args[i]);
+
+ System.out.print(s);
+ }
+ System.out.println();
+ }
+
+ public static void load(Context cx, Scriptable thisObj, Object[] args,
+ Function funObj) {
+ // Find the instance
+ Runner runner = (Runner) getTopLevelScope(thisObj);
+ for (int i = 0; i < args.length; i++) {
+ runner.processSource(cx, Context.toString(args[i]));
+ }
+ }
+
+ public static InputStream getResourceAsStream(String resource)
+ throws IOException {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ InputStream in = null;
+ if (loader != null)
+ in = loader.getResourceAsStream(resource);
+ if (in == null)
+ in = ClassLoader.getSystemResourceAsStream(resource);
+ if (in == null)
+ throw new IOException("Could not find resource " + resource);
+ return in;
+ }
+
+ private void processSource(Context cx, String filename) {
+ Reader in = null;
+ try {
+ in = new InputStreamReader(getResourceAsStream(filename));
+ } catch (IOException ex) {
+ Context
+ .reportError("Couldn't open resource: \"" + filename
+ + "\".");
+ return;
+ }
+
+ try {
+ cx.evaluateReader(topLevelScope, in, filename, 1, null);
+ } catch (WrappedException we) {
+ System.err.println(we.getWrappedException().toString());
+ we.printStackTrace();
+ } catch (EvaluatorException ee) {
+ System.err.println("js: " + ee.getMessage());
+ } catch (JavaScriptException jse) {
+ System.err.println("js: " + jse.getMessage());
+ } catch (IOException ioe) {
+ System.err.println(ioe.toString());
+ } finally {
+ try {
+ in.close();
+ } catch (IOException ioe) {
+ System.err.println(ioe.toString());
+ }
+ }
+ }
+}
View
1  src/empty.txt
@@ -1 +0,0 @@
-There is no Java source.
Please sign in to comment.
Something went wrong with that request. Please try again.