diff -r 4ebc2e2fb97c -r 71c04702a3d5 src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java Tue Sep 12 19:03:39 2017 +0200
@@ -0,0 +1,311 @@
+/*
+ * Copyright (c) 2005, 2013, 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. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 javax.script;
+import java.io.Reader;
+import java.util.Map;
+import java.util.Iterator;
+
+/**
+ * Provides a standard implementation for several of the variants of the eval
+ * method.
+ *
+ * eval(Reader)
eval(String)
+ * eval(String, Bindings)
eval(Reader, Bindings)
+ *
are implemented using the abstract methods
+ *
+ * eval(Reader,ScriptContext)
or
+ * eval(String, ScriptContext)
+ *
+ * with a SimpleScriptContext
.
+ *
+ * A SimpleScriptContext
is used as the default ScriptContext
+ * of the AbstractScriptEngine
..
+ *
+ * @author Mike Grogan
+ * @since 1.6
+ */
+public abstract class AbstractScriptEngine implements ScriptEngine {
+
+ /**
+ * The default ScriptContext
of this AbstractScriptEngine
.
+ */
+
+ protected ScriptContext context;
+
+ /**
+ * Creates a new instance of AbstractScriptEngine using a SimpleScriptContext
+ * as its default ScriptContext
.
+ */
+ public AbstractScriptEngine() {
+
+ context = new SimpleScriptContext();
+
+ }
+
+ /**
+ * Creates a new instance using the specified Bindings
as the
+ * ENGINE_SCOPE
Bindings
in the protected context
field.
+ *
+ * @param n The specified Bindings
.
+ * @throws NullPointerException if n is null.
+ */
+ public AbstractScriptEngine(Bindings n) {
+
+ this();
+ if (n == null) {
+ throw new NullPointerException("n is null");
+ }
+ context.setBindings(n, ScriptContext.ENGINE_SCOPE);
+ }
+
+ /**
+ * Sets the value of the protected context
field to the specified
+ * ScriptContext
.
+ *
+ * @param ctxt The specified ScriptContext
.
+ * @throws NullPointerException if ctxt is null.
+ */
+ public void setContext(ScriptContext ctxt) {
+ if (ctxt == null) {
+ throw new NullPointerException("null context");
+ }
+ context = ctxt;
+ }
+
+ /**
+ * Returns the value of the protected context
field.
+ *
+ * @return The value of the protected context
field.
+ */
+ public ScriptContext getContext() {
+ return context;
+ }
+
+ /**
+ * Returns the Bindings
with the specified scope value in
+ * the protected context
field.
+ *
+ * @param scope The specified scope
+ *
+ * @return The corresponding Bindings
.
+ *
+ * @throws IllegalArgumentException if the value of scope is
+ * invalid for the type the protected context
field.
+ */
+ public Bindings getBindings(int scope) {
+
+ if (scope == ScriptContext.GLOBAL_SCOPE) {
+ return context.getBindings(ScriptContext.GLOBAL_SCOPE);
+ } else if (scope == ScriptContext.ENGINE_SCOPE) {
+ return context.getBindings(ScriptContext.ENGINE_SCOPE);
+ } else {
+ throw new IllegalArgumentException("Invalid scope value.");
+ }
+ }
+
+ /**
+ * Sets the Bindings
with the corresponding scope value in the
+ * context
field.
+ *
+ * @param bindings The specified Bindings
.
+ * @param scope The specified scope.
+ *
+ * @throws IllegalArgumentException if the value of scope is
+ * invalid for the type the context
field.
+ * @throws NullPointerException if the bindings is null and the scope is
+ * ScriptContext.ENGINE_SCOPE
+ */
+ public void setBindings(Bindings bindings, int scope) {
+
+ if (scope == ScriptContext.GLOBAL_SCOPE) {
+ context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);;
+ } else if (scope == ScriptContext.ENGINE_SCOPE) {
+ context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);;
+ } else {
+ throw new IllegalArgumentException("Invalid scope value.");
+ }
+ }
+
+ /**
+ * Sets the specified value with the specified key in the ENGINE_SCOPE
+ * Bindings
of the protected context
field.
+ *
+ * @param key The specified key.
+ * @param value The specified value.
+ *
+ * @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key is empty.
+ */
+ public void put(String key, Object value) {
+
+ Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
+ if (nn != null) {
+ nn.put(key, value);
+ }
+
+ }
+
+ /**
+ * Gets the value for the specified key in the ENGINE_SCOPE
of the
+ * protected context
field.
+ *
+ * @return The value for the specified key.
+ *
+ * @throws NullPointerException if key is null.
+ * @throws IllegalArgumentException if key is empty.
+ */
+ public Object get(String key) {
+
+ Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);
+ if (nn != null) {
+ return nn.get(key);
+ }
+
+ return null;
+ }
+
+
+ /**
+ * eval(Reader, Bindings)
calls the abstract
+ * eval(Reader, ScriptContext)
method, passing it a ScriptContext
+ * whose Reader, Writers and Bindings for scopes other that ENGINE_SCOPE
+ * are identical to those members of the protected context
field. The specified
+ * Bindings
is used instead of the ENGINE_SCOPE
+ *
+ * Bindings
of the context
field.
+ *
+ * @param reader A Reader
containing the source of the script.
+ * @param bindings A Bindings
to use for the ENGINE_SCOPE
+ * while the script executes.
+ *
+ * @return The return value from eval(Reader, ScriptContext)
+ * @throws ScriptException if an error occurs in script.
+ * @throws NullPointerException if any of the parameters is null.
+ */
+ public Object eval(Reader reader, Bindings bindings ) throws ScriptException {
+
+ ScriptContext ctxt = getScriptContext(bindings);
+
+ return eval(reader, ctxt);
+ }
+
+
+ /**
+ * Same as eval(Reader, Bindings)
except that the abstract
+ * eval(String, ScriptContext)
is used.
+ *
+ * @param script A String
containing the source of the script.
+ *
+ * @param bindings A Bindings
to use as the ENGINE_SCOPE
+ * while the script executes.
+ *
+ * @return The return value from eval(String, ScriptContext)
+ * @throws ScriptException if an error occurs in script.
+ * @throws NullPointerException if any of the parameters is null.
+ */
+ public Object eval(String script, Bindings bindings) throws ScriptException {
+
+ ScriptContext ctxt = getScriptContext(bindings);
+
+ return eval(script , ctxt);
+ }
+
+ /**
+ * eval(Reader)
calls the abstract
+ * eval(Reader, ScriptContext)
passing the value of the context
+ * field.
+ *
+ * @param reader A Reader
containing the source of the script.
+ * @return The return value from eval(Reader, ScriptContext)
+ * @throws ScriptException if an error occurs in script.
+ * @throws NullPointerException if any of the parameters is null.
+ */
+ public Object eval(Reader reader) throws ScriptException {
+
+
+ return eval(reader, context);
+ }
+
+ /**
+ * Same as eval(Reader)
except that the abstract
+ * eval(String, ScriptContext)
is used.
+ *
+ * @param script A String
containing the source of the script.
+ * @return The return value from eval(String, ScriptContext)
+ * @throws ScriptException if an error occurs in script.
+ * @throws NullPointerException if any of the parameters is null.
+ */
+ public Object eval(String script) throws ScriptException {
+
+
+ return eval(script, context);
+ }
+
+ /**
+ * Returns a SimpleScriptContext
. The SimpleScriptContext
:
+ *
+ *
Bindings
for its ENGINE_SCOPE
+ * Bindings
returned by the abstract getGlobalScope
+ * method as its GLOBAL_SCOPE
+ * ScriptContext
of this
+ * ScriptEngine
+ * SimpleScriptContext
returned by this method is used to implement eval methods
+ * using the abstract eval(Reader,Bindings)
and eval(String,Bindings)
+ * versions.
+ *
+ * @param nn Bindings to use for the ENGINE_SCOPE
+ * @return The SimpleScriptContext
+ */
+ protected ScriptContext getScriptContext(Bindings nn) {
+
+ SimpleScriptContext ctxt = new SimpleScriptContext();
+ Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);
+
+ if (gs != null) {
+ ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);
+ }
+
+ if (nn != null) {
+ ctxt.setBindings(nn,
+ ScriptContext.ENGINE_SCOPE);
+ } else {
+ throw new NullPointerException("Engine scope Bindings may not be null.");
+ }
+
+ ctxt.setReader(context.getReader());
+ ctxt.setWriter(context.getWriter());
+ ctxt.setErrorWriter(context.getErrorWriter());
+
+ return ctxt;
+
+ }
+}