Skip to content

Commit

Permalink
Merge pull request #13201 from bobjacobsen/deprecated-javascript-warning
Browse files Browse the repository at this point in the history
Request JavaScript/ECMAscript JMRI scripting users contact us
  • Loading branch information
danielb987 committed Jun 20, 2024
2 parents 682f339 + ca4600d commit 29b18d6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion help/en/html/tools/scripting/index.shtml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<p>Any <a href=
"https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html"
target="_blank">Java Scripting API</a> (commonly refered to JSR-223) compliant scripting
language can be added to JMRI, however, only JavaScript and Python (Jython) are directly
language can be added to JMRI, however, only Python (Jython) is directly
supported in the default installation. If you want to add another scripting language, find a
JSR-223 compliant interpreter and add it to the JMRI classpath as <a href=
"../../doc/Technical/StartUpScripts.shtml" target="_blank">documented for your operating
Expand Down
9 changes: 8 additions & 1 deletion help/en/releasenotes/current-draft-note.shtml
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,14 @@
<h3>Scripting</h3>
<a id="Scripting" name="Scripting"></a>
<ul>
<li></li>
<li>Added a warning that requests people
contact us if they are using JavaScript/ECMAscript
for JMRI scripting. This will help us
understand how much that feature is being used.
<p>
There's also a new "jython/DontWarnJavaScript.py"
script that can be loaded at startup to
suppress this warning.</li>
</ul>

<h3>Signals</h3>
Expand Down
37 changes: 37 additions & 0 deletions java/src/jmri/script/JmriScriptEngineManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ public ScriptEngine getEngine(@CheckForNull String name) {
* @throws javax.script.ScriptException if there is an error in the script.
*/
public Object eval(String script, ScriptEngine engine) throws ScriptException {

if (engine.getFactory().getEngineName().equals("Oracle Nashorn")) {
warnJavaScriptUsers();
}

if (JYTHON.equals(engine.getFactory().getEngineName()) && this.jython != null) {
this.jython.exec(script);
return null;
Expand Down Expand Up @@ -387,6 +392,11 @@ private Object eval(File file, @CheckForNull ScriptContext context, @CheckForNul
@CheckForNull
private ScriptEngine getEngineOrEval(File file) throws ScriptException, IOException {
ScriptEngine engine = this.getEngine(FilenameUtils.getExtension(file.getName()), EXTENSION);

if (engine.getFactory().getEngineName().equals("Oracle Nashorn")) {
warnJavaScriptUsers();
}

if (JYTHON.equals(engine.getFactory().getEngineName()) && this.jython != null) {
try (FileInputStream fi = new FileInputStream(file)) {
this.jython.execfile(fi);
Expand Down Expand Up @@ -426,6 +436,33 @@ public void initializeAllEngines() {
this.factories.keySet().stream().forEach(this::getEngine);
}

/**
* This is a temporary method to warn users that the JavaScript/ECMAscript
* support may be going away soon.
*/
private void warnJavaScriptUsers() {
if (! dontWarnJavaScript) {
log.warn("*** Scripting with JavaScript/ECMAscript is being deprecated ***");
log.warn("*** and may soon be removed. If you are using this, please ***");
log.warn("*** contact us on the jmriusers group for assistance. ***");

if (! java.awt.GraphicsEnvironment.isHeadless()) {
jmri.util.swing.JmriJOptionPane.showMessageDialog(null,
"<html>"+
"Scripting with JavaScript/ECMAscript is being deprecated <br/>"+
"and may soon be removed. If you are using this, please<br/>"+
"contact us on the jmriusers group for assistance.<br/>"+
"</html>"
);
}
}
dontWarnJavaScript = true;
}
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "MS_PKGPROTECT",
justification = "Public accessibility for script to override warning")
static public boolean dontWarnJavaScript = false;
// The jython/DontWarnJavaScript.py script will disable the warning

/**
* Get the default {@link javax.script.ScriptContext} for all
* {@link javax.script.ScriptEngine}s.
Expand Down
11 changes: 10 additions & 1 deletion java/src/jmri/script/ScriptEngineSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void setSelectedEngine(@Nonnull String languageName) {
Engine engine = InstanceManager.getDefault(
InternalScriptEngineSelector.class).getEngineFromLanguage(languageName);
if (engine != null) {
_selectedEngine = engine;
setSelectedEngine(engine);
} else {
log.warn("Cannot select engine for the language {}", languageName);
}
Expand Down Expand Up @@ -114,6 +114,15 @@ public boolean isJython() {
|| "jython".equalsIgnoreCase(_languageName);
}

/**
* Is this engine for JavaScript/ECMAscript?
* @return true if JavaScript/ECMAscript, false otherwise
*/
public boolean isJavaScript() {
return "javascript".equalsIgnoreCase(_languageName)
|| "ecmascript".equalsIgnoreCase(_languageName);
}

/** {@inheritDoc} */
@Override
public String toString() {
Expand Down
2 changes: 0 additions & 2 deletions java/test/jmri/script/ScriptEngineSelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ private void runEcmaScriptOldStyle() throws ScriptException {
}

private void runJythonScriptNewStyle() throws ScriptException {
System.out.format("runJythonScriptNewStyle()%n");
Bindings bindings = new SimpleBindings();
ScriptEngineSelector.Engine engine =
_scriptEngineSelector.getSelectedEngine();
Expand All @@ -52,7 +51,6 @@ private void runJythonScriptNewStyle() throws ScriptException {
}

private void runEcmaScriptNewStyle() throws ScriptException {
System.out.format("runEcmaScriptNewStyle()%n");
Bindings bindings = new SimpleBindings();
_scriptEngineSelector.setSelectedEngine(ScriptEngineSelector.ECMA_SCRIPT);
ScriptEngineSelector.Engine engine =
Expand Down
15 changes: 15 additions & 0 deletions jython/DontWarnJavaScript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# JavaScript/ECMAscript support is being deprecated and may soon be removed.
#
# As part of this, use of that support will fire a dialog box to warn people.
# Since that can be really annoying if it happens on every JMRI start, we
# provide this script to disable the warning.

import jmri
jmri.script.JmriScriptEngineManager.dontWarnJavaScript = True

import org.slf4j
log = org.slf4j.LoggerFactory.getLogger("JavaScript Interpreter")
log.warn("The following warning has been suppressed:")
log.warn("*** Scripting with JavaScript/ECMAscript is being deprecated ***")
log.warn("*** and may soon be removed. If you are using this, please ***")
log.warn("*** contact us on the jmriusers group for assistance. ***")
5 changes: 0 additions & 5 deletions jython/test/JavaScriptTest.js

This file was deleted.

0 comments on commit 29b18d6

Please sign in to comment.