changeset 48849:290b480df13e

8011697: ScriptEngine "js" randomly means either "rhino" or "nashorn", but should instead select one Summary: Sort ScriptEngineFactory by name so that same engine is retrieved across all OS Reviewed-by: alanb, sundar Contributed-by: srinivas.dama@oracle.com
author sdama
date Fri, 02 Feb 2018 23:21:12 +0530
parents 678e1ec433a0
children ef3557eb4306
files src/java.scripting/share/classes/javax/script/ScriptEngineManager.java test/jdk/javax/script/ScriptEngineOrder.sh test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/jdk/dummyNashorn/api/scripting/DummyNashornJSEngine.java test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/jdk/dummyNashorn/api/scripting/DummyNashornJSEngineFactory.java test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/module-info.java test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/jdk/dummyRhino/api/scripting/DummyRhinoJSEngine.java test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/jdk/dummyRhino/api/scripting/DummyRhinoJSEngineFactory.java test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/module-info.java test/jdk/javax/script/multiEngines/jdk.scripting.testEngines/jdk/test/engines/ScriptEngineTest.java test/jdk/javax/script/multiEngines/jdk.scripting.testEngines/module-info.java
diffstat 10 files changed, 511 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.scripting/share/classes/javax/script/ScriptEngineManager.java	Fri Feb 02 02:55:00 2018 +0000
+++ b/src/java.scripting/share/classes/javax/script/ScriptEngineManager.java	Fri Feb 02 23:21:12 2018 +0530
@@ -77,7 +77,7 @@
 
     private void init(final ClassLoader loader) {
         globalScope = new SimpleBindings();
-        engineSpis = new HashSet<ScriptEngineFactory>();
+        engineSpis = new TreeSet<ScriptEngineFactory>(Comparator.comparing(ScriptEngineFactory::getEngineName));
         nameAssociations = new HashMap<String, ScriptEngineFactory>();
         extensionAssociations = new HashMap<String, ScriptEngineFactory>();
         mimeTypeAssociations = new HashMap<String, ScriptEngineFactory>();
@@ -400,7 +400,7 @@
     }
 
     /** Set of script engine factories discovered. */
-    private HashSet<ScriptEngineFactory> engineSpis;
+    private TreeSet<ScriptEngineFactory> engineSpis;
 
     /** Map of engine name to script engine factory. */
     private HashMap<String, ScriptEngineFactory> nameAssociations;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/ScriptEngineOrder.sh	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,52 @@
+#
+# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+# or visit www.oracle.com if you need additional information or have any
+# questions.
+#
+
+# @test
+# @bug 8011697
+# @summary test to check consistency in discovering and returning script engine 
+# by ScriptEngineManager
+#
+# @run shell ScriptEngineOrder.sh
+
+set -x
+if [ "${TESTSRC}" = "" ]
+then
+  echo "TESTSRC not set.  Test cannot execute.  Failed."
+  exit 1
+fi
+
+. ${TESTSRC}/CommonSetup.sh
+
+echo "Building dummy script engine modules.."
+#test to check the consistency in returning engines by ScriptEngineManager
+$JAVAC --limit-modules java.base,java.logging,java.scripting,jdk.scripting.dummyNashorn,jdk.scripting.dummyRhino,jdk.scripting.testEngines -d ${TESTCLASSES}/mods --module-source-path ${TESTSRC}/multiEngines $(find ${TESTSRC}/multiEngines -name *.java)
+
+echo "Running script engine test.."
+$JAVA --limit-modules java.base,java.logging,java.scripting,jdk.scripting.dummyNashorn,jdk.scripting.dummyRhino,jdk.scripting.testEngines --module-path ${TESTCLASSES}/mods --module jdk.scripting.testEngines/jdk.test.engines.ScriptEngineTest
+
+ret=$?
+if [ $ret -ne 0 ]
+then
+  exit $ret
+fi
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/jdk/dummyNashorn/api/scripting/DummyNashornJSEngine.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ *
+ *
+ * This is a dummyNashorn script engine implementation
+ */
+package jdk.dummyNashorn.api.scripting;
+import javax.script.*;
+import java.io.*;
+
+public class DummyNashornJSEngine extends AbstractScriptEngine {
+    public Object eval(String str, ScriptContext ctx) {
+        return eval(new StringReader(str), ctx);
+    }
+
+    public Object eval(Reader reader, ScriptContext ctx) {
+        System.out.println("eval done!");
+        return null;
+    }
+
+    public ScriptEngineFactory getFactory() {
+        return new DummyNashornJSEngineFactory();
+    }
+
+    public Bindings createBindings() {
+        return new SimpleBindings();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/jdk/dummyNashorn/api/scripting/DummyNashornJSEngineFactory.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ *
+ *
+ * This is script engine factory for dummyNashorn engine.
+ */
+package jdk.dummyNashorn.api.scripting;
+import javax.script.*;
+import java.util.*;
+
+public class DummyNashornJSEngineFactory implements ScriptEngineFactory {
+    public String getEngineName() {
+        return "dummyNashorn";
+    }
+
+    public String getEngineVersion() {
+        return "-1.0";
+    }
+
+    public List<String> getExtensions() {
+        return extensions;
+    }
+
+    public String getLanguageName() {
+        return "dummyNashorn";
+    }
+
+    public String getLanguageVersion() {
+        return "-1.0";
+    }
+
+    public String getMethodCallSyntax(String obj, String m, String... args) {
+        StringBuffer buf = new StringBuffer();
+        buf.append("call " + m + " ");
+        buf.append(" on " + obj + " with ");
+        for (int i = 0; i < args.length; i++) {
+            buf.append(args[i] + ", ");
+        }
+        buf.append(";");
+        return buf.toString();
+    }
+
+    public List<String> getMimeTypes() {
+        return mimeTypes;
+    }
+
+    public List<String> getNames() {
+        return names;
+    }
+
+    public String getOutputStatement(String str) {
+        return "output " + str;
+    }
+
+    public String getParameter(String key) {
+        if (key.equals(ScriptEngine.ENGINE)) {
+            return getEngineName();
+        } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
+            return getEngineVersion();
+        } else if (key.equals(ScriptEngine.NAME)) {
+            return getEngineName();
+        } else if (key.equals(ScriptEngine.LANGUAGE)) {
+            return getLanguageName();
+        } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
+            return getLanguageVersion();
+        } else {
+            return null;
+        }
+    }
+
+    public String getProgram(String... statements) {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < statements.length; i++) {
+            buf.append(statements[i]);
+        }
+        return buf.toString();
+    }
+
+    public ScriptEngine getScriptEngine() {
+        return new DummyNashornJSEngine();
+    }
+
+    private static List<String> names;
+    private static List<String> extensions;
+    private static List<String> mimeTypes;
+    static {
+        names = new ArrayList<String>(1);
+        names.add("dummyNashorn");
+        names.add("js");
+        names = Collections.unmodifiableList(names);
+        extensions = names;
+        mimeTypes = new ArrayList<String>(0);
+        mimeTypes = Collections.unmodifiableList(mimeTypes);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyNashorn/module-info.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+module jdk.scripting.dummyNashorn {
+    requires java.scripting;
+    exports jdk.dummyNashorn.api.scripting;
+        provides javax.script.ScriptEngineFactory with
+            jdk.dummyNashorn.api.scripting.DummyNashornJSEngineFactory;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/jdk/dummyRhino/api/scripting/DummyRhinoJSEngine.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ *
+ *
+ * This is a dummyRhino script engine implementation
+ */
+package jdk.dummyRhino.api.scripting;
+import javax.script.*;
+import java.io.*;
+
+public class DummyRhinoJSEngine extends AbstractScriptEngine {
+    public Object eval(String str, ScriptContext ctx) {
+        return eval(new StringReader(str), ctx);
+    }
+
+    public Object eval(Reader reader, ScriptContext ctx) {
+        System.out.println("eval done!");
+        return null;
+    }
+
+    public ScriptEngineFactory getFactory() {
+        return new DummyRhinoJSEngineFactory();
+    }
+
+    public Bindings createBindings() {
+        return new SimpleBindings();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/jdk/dummyRhino/api/scripting/DummyRhinoJSEngineFactory.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ *
+ *
+ * This is script engine factory for dummyRhino engine.
+ */
+package jdk.dummyRhino.api.scripting;
+import javax.script.*;
+import java.util.*;
+
+public class DummyRhinoJSEngineFactory implements ScriptEngineFactory {
+    public String getEngineName() {
+        return "dummyRhino";
+    }
+
+    public String getEngineVersion() {
+        return "-1.0";
+    }
+
+    public List<String> getExtensions() {
+        return extensions;
+    }
+
+    public String getLanguageName() {
+        return "dummyRhino";
+    }
+
+    public String getLanguageVersion() {
+        return "-1.0";
+    }
+
+    public String getMethodCallSyntax(String obj, String m, String... args) {
+        StringBuffer buf = new StringBuffer();
+        buf.append("call " + m + " ");
+        buf.append(" on " + obj + " with ");
+        for (int i = 0; i < args.length; i++) {
+            buf.append(args[i] + ", ");
+        }
+        buf.append(";");
+        return buf.toString();
+    }
+
+    public List<String> getMimeTypes() {
+        return mimeTypes;
+    }
+
+    public List<String> getNames() {
+        return names;
+    }
+
+    public String getOutputStatement(String str) {
+        return "output " + str;
+    }
+
+    public String getParameter(String key) {
+        if (key.equals(ScriptEngine.ENGINE)) {
+            return getEngineName();
+        } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
+            return getEngineVersion();
+        } else if (key.equals(ScriptEngine.NAME)) {
+            return getEngineName();
+        } else if (key.equals(ScriptEngine.LANGUAGE)) {
+            return getLanguageName();
+        } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
+            return getLanguageVersion();
+        } else {
+            return null;
+        }
+    }
+
+    public String getProgram(String... statements) {
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < statements.length; i++) {
+            buf.append(statements[i]);
+        }
+        return buf.toString();
+    }
+
+    public ScriptEngine getScriptEngine() {
+        return new DummyRhinoJSEngine();
+    }
+
+    private static List<String> names;
+    private static List<String> extensions;
+    private static List<String> mimeTypes;
+    static {
+        names = new ArrayList<String>(1);
+        names.add("dummyRhino");
+        names.add("js");
+        names = Collections.unmodifiableList(names);
+        extensions = names;
+        mimeTypes = new ArrayList<String>(0);
+        mimeTypes = Collections.unmodifiableList(mimeTypes);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.dummyRhino/module-info.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+module jdk.scripting.dummyRhino {
+    requires java.scripting;
+    exports jdk.dummyRhino.api.scripting;
+        provides javax.script.ScriptEngineFactory with
+            jdk.dummyRhino.api.scripting.DummyRhinoJSEngineFactory;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.testEngines/jdk/test/engines/ScriptEngineTest.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package jdk.test.engines;
+import javax.script.*;
+public class ScriptEngineTest {
+
+        public static void main(String[] args) throws Exception {
+            for (int i = 0; i < 100; i++) {
+                ScriptEngineManager manager = new ScriptEngineManager();
+                ScriptEngine scriptEngine = manager.getEngineByName("js");
+                if(!(scriptEngine.toString().contains("jdk.dummyNashorn.api.scripting.DummyNashornJSEngine")))
+                    throw new RuntimeException("Script EngineOrder is inconsistent");
+            }
+        }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/script/multiEngines/jdk.scripting.testEngines/module-info.java	Fri Feb 02 23:21:12 2018 +0530
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+module jdk.scripting.testEngines{
+    requires java.scripting;
+}
+