Skip to content

Commit e9f0667

Browse files
committed
Migrate Rhino engine from java.net scripting
This move is in preparation for continued Rhino support in Java 8. It is a necessary change because Java 8 ships the new Nashorn engine instead, which breaks backwards compatibility with Rhino scripts.
1 parent 965b097 commit e9f0667

19 files changed

+2776
-0
lines changed

NOTICE.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
This component adapts code originally developed for the Java.net Scripting
2+
project (http://scripting.java.net/), with the following copyright notice:
3+
4+
Copyright (c) 2006, Sun Microsystems, Inc.
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
- Redistributions of source code must retain the above copyright notice, this
11+
list of conditions and the following disclaimer.
12+
13+
- Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
- Neither the name of the Sun Microsystems, Inc. nor the names of
18+
contributors may be used to endorse or promote products derived from this
19+
software without specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
23+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26+
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
27+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30+
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33+
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35+
DAMAGE.

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
<artifactId>scijava-common</artifactId>
8888
</dependency>
8989

90+
<!-- Other dependencies -->
91+
<dependency>
92+
<groupId>org.mozilla</groupId>
93+
<artifactId>rhino</artifactId>
94+
<version>1.7R5</version>
95+
</dependency>
96+
9097
<!-- Test dependencies -->
9198
<dependency>
9299
<groupId>junit</groupId>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved.
3+
* Use is subject to license terms.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met: Redistributions of source code
7+
* must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright notice, this list of
9+
* conditions and the following disclaimer in the documentation and/or other materials
10+
* provided with the distribution. Neither the name of the Sun Microsystems nor the names of
11+
* is contributors may be used to endorse or promote products derived from this software
12+
* without specific prior written permission.
13+
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
15+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
17+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
* POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
package org.scijava.plugins.scripting.javascript;
26+
27+
import java.io.FileInputStream;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
import java.io.Reader;
31+
import java.io.StringReader;
32+
33+
import javax.script.ScriptEngine;
34+
import javax.script.ScriptException;
35+
import javax.script.SimpleScriptContext;
36+
37+
import org.scijava.plugins.scripting.javascript.util.DeTagifier;
38+
39+
/**
40+
* Embedded javascript interpreter.
41+
*/
42+
public class EmbeddedRhinoScriptEngine extends RhinoScriptEngine {
43+
44+
protected DeTagifier detagifier;
45+
46+
public EmbeddedRhinoScriptEngine() {
47+
detagifier = new DeTagifier("context.getWriter().write(\"",
48+
"\");\n",
49+
"context.getWriter().write(",
50+
");\n");
51+
}
52+
53+
protected Reader preProcessScriptSource(Reader reader) throws ScriptException {
54+
try {
55+
String s = detagifier.parse(reader);
56+
return new StringReader(s);
57+
}
58+
catch (IOException ee) {
59+
throw new ScriptException(ee);
60+
}
61+
}
62+
63+
public static void main(String[] args) throws Exception {
64+
if (args.length == 0) {
65+
System.out.println("No file specified");
66+
return;
67+
}
68+
69+
InputStreamReader r = new InputStreamReader(new FileInputStream(args[0]));
70+
ScriptEngine engine = new EmbeddedRhinoScriptEngine();
71+
72+
SimpleScriptContext context = new SimpleScriptContext();
73+
engine.put(ScriptEngine.FILENAME, args[0]);
74+
engine.eval(r, context);
75+
context.getWriter().flush();
76+
}
77+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (C) 2006 Sun Microsystems, Inc. All rights reserved.
3+
* Use is subject to license terms.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification, are
6+
* permitted provided that the following conditions are met: Redistributions of source code
7+
* must retain the above copyright notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright notice, this list of
9+
* conditions and the following disclaimer in the documentation and/or other materials
10+
* provided with the distribution. Neither the name of the Sun Microsystems nor the names of
11+
* is contributors may be used to endorse or promote products derived from this software
12+
* without specific prior written permission.
13+
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
15+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
16+
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
17+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21+
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22+
* POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
package org.scijava.plugins.scripting.javascript;
26+
import java.util.ArrayList;
27+
import java.util.Collections;
28+
import java.util.List;
29+
import java.util.Properties;
30+
31+
import javax.script.ScriptEngine;
32+
33+
import org.scijava.plugins.scripting.javascript.util.ScriptEngineFactoryBase;
34+
35+
/**
36+
* Factory to create EmbeddedRhinoScriptEngine
37+
*
38+
*/
39+
public class EmbeddedRhinoScriptEngineFactory extends ScriptEngineFactoryBase {
40+
41+
private Properties properties;
42+
private boolean initialized;
43+
44+
public EmbeddedRhinoScriptEngineFactory() {
45+
}
46+
47+
public List<String> getExtensions() {
48+
return extensions;
49+
}
50+
51+
public List<String> getMimeTypes() {
52+
return mimeTypes;
53+
}
54+
55+
public List<String> getNames() {
56+
return names;
57+
}
58+
59+
public Object getParameter(String key) {
60+
if (key.equals(ScriptEngine.NAME)) {
61+
return "embedded-javascript";
62+
} else if (key.equals(ScriptEngine.ENGINE)) {
63+
return "Mozilla Rhino";
64+
} else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
65+
return "1.6 release 2";
66+
} else if (key.equals(ScriptEngine.LANGUAGE)) {
67+
return "EmbeddedECMAScript";
68+
} else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
69+
return "1.6";
70+
} else if (key.equals("THREADING")) {
71+
return "MULTITHREADED";
72+
} else {
73+
throw new IllegalArgumentException("Invalid key");
74+
}
75+
}
76+
77+
public void setProperties(Properties properties) {
78+
this.properties = properties;
79+
}
80+
81+
public ScriptEngine getScriptEngine() {
82+
EmbeddedRhinoScriptEngine ret = new EmbeddedRhinoScriptEngine();
83+
ret.setEngineFactory(this);
84+
return ret;
85+
}
86+
87+
public String getMethodCallSyntax(String obj, String method, String... args) {
88+
89+
String ret = obj + "." + method + "(";
90+
int len = args.length;
91+
if (len == 0) {
92+
ret += ")";
93+
return ret;
94+
}
95+
96+
for (int i = 0; i < len; i++) {
97+
ret += args[i];
98+
if (i != len - 1) {
99+
ret += ",";
100+
} else {
101+
ret += ")";
102+
}
103+
}
104+
return ret;
105+
}
106+
107+
public String getOutputStatement(String toDisplay) {
108+
return "print(" + toDisplay + ")";
109+
}
110+
111+
public String getProgram(String... statements) {
112+
int len = statements.length;
113+
String ret = "";
114+
for (int i = 0; i < len; i++) {
115+
ret += statements[i] + ";";
116+
}
117+
118+
return ret;
119+
}
120+
121+
public static void main(String[] args) {
122+
EmbeddedRhinoScriptEngineFactory fact = new EmbeddedRhinoScriptEngineFactory();
123+
System.out.println(fact.getParameter(ScriptEngine.ENGINE_VERSION));
124+
}
125+
126+
private static List<String> names;
127+
private static List<String> mimeTypes;
128+
private static List<String> extensions;
129+
130+
static {
131+
names = new ArrayList<String>(6);
132+
names.add("ejs");
133+
names.add("EmbeddedJavaScript");
134+
names.add("embeddedjavascript");
135+
names = Collections.unmodifiableList(names);
136+
137+
mimeTypes = new ArrayList<String>(4);
138+
mimeTypes.add("application/embeddedjavascript");
139+
mimeTypes.add("text/embeddedjavascript");
140+
mimeTypes = Collections.unmodifiableList(mimeTypes);
141+
142+
extensions = new ArrayList<String>(1);
143+
extensions.add("ejs");
144+
extensions = Collections.unmodifiableList(extensions);
145+
}
146+
}

0 commit comments

Comments
 (0)