Path: blob/master/src/java.scripting/share/classes/javax/script/AbstractScriptEngine.java
41128 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.script;26import java.io.Reader;2728/**29* Provides a standard implementation for several of the variants of the <code>eval</code>30* method.31* <br><br>32* <code><b>eval(Reader)</b></code><p><code><b>eval(String)</b></code><p>33* <code><b>eval(String, Bindings)</b></code><p><code><b>eval(Reader, Bindings)</b></code>34* <br><br> are implemented using the abstract methods35* <br><br>36* <code><b>eval(Reader,ScriptContext)</b></code> or37* <code><b>eval(String, ScriptContext)</b></code>38* <br><br>39* with a <code>SimpleScriptContext</code>.40* <br><br>41* A <code>SimpleScriptContext</code> is used as the default <code>ScriptContext</code>42* of the <code>AbstractScriptEngine</code>..43*44* @author Mike Grogan45* @since 1.646*/47public abstract class AbstractScriptEngine implements ScriptEngine {4849/**50* The default <code>ScriptContext</code> of this <code>AbstractScriptEngine</code>.51*/5253protected ScriptContext context;5455/**56* Creates a new instance of AbstractScriptEngine using a <code>SimpleScriptContext</code>57* as its default <code>ScriptContext</code>.58*/59public AbstractScriptEngine() {6061context = new SimpleScriptContext();6263}6465/**66* Creates a new instance using the specified <code>Bindings</code> as the67* <code>ENGINE_SCOPE</code> <code>Bindings</code> in the protected <code>context</code> field.68*69* @param n The specified <code>Bindings</code>.70* @throws NullPointerException if n is null.71*/72public AbstractScriptEngine(Bindings n) {7374this();75if (n == null) {76throw new NullPointerException("n is null");77}78context.setBindings(n, ScriptContext.ENGINE_SCOPE);79}8081/**82* Sets the value of the protected <code>context</code> field to the specified83* <code>ScriptContext</code>.84*85* @param ctxt The specified <code>ScriptContext</code>.86* @throws NullPointerException if ctxt is null.87*/88public void setContext(ScriptContext ctxt) {89if (ctxt == null) {90throw new NullPointerException("null context");91}92context = ctxt;93}9495/**96* Returns the value of the protected <code>context</code> field.97*98* @return The value of the protected <code>context</code> field.99*/100public ScriptContext getContext() {101return context;102}103104/**105* Returns the <code>Bindings</code> with the specified scope value in106* the protected <code>context</code> field.107*108* @param scope The specified scope109*110* @return The corresponding <code>Bindings</code>.111*112* @throws IllegalArgumentException if the value of scope is113* invalid for the type the protected <code>context</code> field.114*/115public Bindings getBindings(int scope) {116117if (scope == ScriptContext.GLOBAL_SCOPE) {118return context.getBindings(ScriptContext.GLOBAL_SCOPE);119} else if (scope == ScriptContext.ENGINE_SCOPE) {120return context.getBindings(ScriptContext.ENGINE_SCOPE);121} else {122throw new IllegalArgumentException("Invalid scope value.");123}124}125126/**127* Sets the <code>Bindings</code> with the corresponding scope value in the128* <code>context</code> field.129*130* @param bindings The specified <code>Bindings</code>.131* @param scope The specified scope.132*133* @throws IllegalArgumentException if the value of scope is134* invalid for the type the <code>context</code> field.135* @throws NullPointerException if the bindings is null and the scope is136* <code>ScriptContext.ENGINE_SCOPE</code>137*/138public void setBindings(Bindings bindings, int scope) {139140if (scope == ScriptContext.GLOBAL_SCOPE) {141context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);142} else if (scope == ScriptContext.ENGINE_SCOPE) {143context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);144} else {145throw new IllegalArgumentException("Invalid scope value.");146}147}148149/**150* Sets the specified value with the specified key in the <code>ENGINE_SCOPE</code>151* <code>Bindings</code> of the protected <code>context</code> field.152*153* @param key The specified key.154* @param value The specified value.155*156* @throws NullPointerException if key is null.157* @throws IllegalArgumentException if key is empty.158*/159public void put(String key, Object value) {160161Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);162if (nn != null) {163nn.put(key, value);164}165166}167168/**169* Gets the value for the specified key in the <code>ENGINE_SCOPE</code> of the170* protected <code>context</code> field.171*172* @return The value for the specified key.173*174* @throws NullPointerException if key is null.175* @throws IllegalArgumentException if key is empty.176*/177public Object get(String key) {178179Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE);180if (nn != null) {181return nn.get(key);182}183184return null;185}186187188/**189* <code>eval(Reader, Bindings)</code> calls the abstract190* <code>eval(Reader, ScriptContext)</code> method, passing it a <code>ScriptContext</code>191* whose Reader, Writers and Bindings for scopes other that <code>ENGINE_SCOPE</code>192* are identical to those members of the protected <code>context</code> field. The specified193* <code>Bindings</code> is used instead of the <code>ENGINE_SCOPE</code>194*195* <code>Bindings</code> of the <code>context</code> field.196*197* @param reader A <code>Reader</code> containing the source of the script.198* @param bindings A <code>Bindings</code> to use for the <code>ENGINE_SCOPE</code>199* while the script executes.200*201* @return The return value from <code>eval(Reader, ScriptContext)</code>202* @throws ScriptException if an error occurs in script.203* @throws NullPointerException if any of the parameters is null.204*/205public Object eval(Reader reader, Bindings bindings ) throws ScriptException {206207ScriptContext ctxt = getScriptContext(bindings);208209return eval(reader, ctxt);210}211212213/**214* Same as <code>eval(Reader, Bindings)</code> except that the abstract215* <code>eval(String, ScriptContext)</code> is used.216*217* @param script A <code>String</code> containing the source of the script.218*219* @param bindings A <code>Bindings</code> to use as the <code>ENGINE_SCOPE</code>220* while the script executes.221*222* @return The return value from <code>eval(String, ScriptContext)</code>223* @throws ScriptException if an error occurs in script.224* @throws NullPointerException if any of the parameters is null.225*/226public Object eval(String script, Bindings bindings) throws ScriptException {227228ScriptContext ctxt = getScriptContext(bindings);229230return eval(script , ctxt);231}232233/**234* <code>eval(Reader)</code> calls the abstract235* <code>eval(Reader, ScriptContext)</code> passing the value of the <code>context</code>236* field.237*238* @param reader A <code>Reader</code> containing the source of the script.239* @return The return value from <code>eval(Reader, ScriptContext)</code>240* @throws ScriptException if an error occurs in script.241* @throws NullPointerException if any of the parameters is null.242*/243public Object eval(Reader reader) throws ScriptException {244245246return eval(reader, context);247}248249/**250* Same as <code>eval(Reader)</code> except that the abstract251* <code>eval(String, ScriptContext)</code> is used.252*253* @param script A <code>String</code> containing the source of the script.254* @return The return value from <code>eval(String, ScriptContext)</code>255* @throws ScriptException if an error occurs in script.256* @throws NullPointerException if any of the parameters is null.257*/258public Object eval(String script) throws ScriptException {259260261return eval(script, context);262}263264/**265* Returns a <code>SimpleScriptContext</code>. The <code>SimpleScriptContext</code>:266*<br><br>267* <ul>268* <li>Uses the specified <code>Bindings</code> for its <code>ENGINE_SCOPE</code>269* </li>270* <li>Uses the <code>Bindings</code> returned by the abstract <code>getGlobalScope</code>271* method as its <code>GLOBAL_SCOPE</code>272* </li>273* <li>Uses the Reader and Writer in the default <code>ScriptContext</code> of this274* <code>ScriptEngine</code>275* </li>276* </ul>277* <br><br>278* A <code>SimpleScriptContext</code> returned by this method is used to implement eval methods279* using the abstract <code>eval(Reader,Bindings)</code> and <code>eval(String,Bindings)</code>280* versions.281*282* @param nn Bindings to use for the <code>ENGINE_SCOPE</code>283* @return The <code>SimpleScriptContext</code>284*/285protected ScriptContext getScriptContext(Bindings nn) {286287SimpleScriptContext ctxt = new SimpleScriptContext(context.getReader(), context.getWriter(), context.getErrorWriter());288Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE);289290if (gs != null) {291ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE);292}293294if (nn != null) {295ctxt.setBindings(nn,296ScriptContext.ENGINE_SCOPE);297} else {298throw new NullPointerException("Engine scope Bindings may not be null.");299}300301return ctxt;302303}304}305306307