Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/script/ScriptContext.java
38829 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.util.List;27import java.io.Writer;28import java.io.Reader;2930/**31* The interface whose implementing classes are used to connect Script Engines32* with objects, such as scoped Bindings, in hosting applications. Each scope is a set33* of named attributes whose values can be set and retrieved using the34* <code>ScriptContext</code> methods. ScriptContexts also expose Readers and Writers35* that can be used by the ScriptEngines for input and output.36*37* @author Mike Grogan38* @since 1.639*/40public interface ScriptContext {414243/**44* EngineScope attributes are visible during the lifetime of a single45* <code>ScriptEngine</code> and a set of attributes is maintained for each46* engine.47*/48public static final int ENGINE_SCOPE = 100;4950/**51* GlobalScope attributes are visible to all engines created by same ScriptEngineFactory.52*/53public static final int GLOBAL_SCOPE = 200;545556/**57* Associates a <code>Bindings</code> instance with a particular scope in this58* <code>ScriptContext</code>. Calls to the <code>getAttribute</code> and59* <code>setAttribute</code> methods must map to the <code>get</code> and60* <code>put</code> methods of the <code>Bindings</code> for the specified scope.61*62* @param bindings The <code>Bindings</code> to associate with the given scope63* @param scope The scope64*65* @throws IllegalArgumentException If no <code>Bindings</code> is defined for the66* specified scope value in ScriptContexts of this type.67* @throws NullPointerException if value of scope is <code>ENGINE_SCOPE</code> and68* the specified <code>Bindings</code> is null.69*70*/71public void setBindings(Bindings bindings, int scope);7273/**74* Gets the <code>Bindings</code> associated with the given scope in this75* <code>ScriptContext</code>.76*77* @return The associated <code>Bindings</code>. Returns <code>null</code> if it has not78* been set.79*80* @param scope The scope81* @throws IllegalArgumentException If no <code>Bindings</code> is defined for the82* specified scope value in <code>ScriptContext</code> of this type.83*/84public Bindings getBindings(int scope);8586/**87* Sets the value of an attribute in a given scope.88*89* @param name The name of the attribute to set90* @param value The value of the attribute91* @param scope The scope in which to set the attribute92*93* @throws IllegalArgumentException94* if the name is empty or if the scope is invalid.95* @throws NullPointerException if the name is null.96*/97public void setAttribute(String name, Object value, int scope);9899/**100* Gets the value of an attribute in a given scope.101*102* @param name The name of the attribute to retrieve.103* @param scope The scope in which to retrieve the attribute.104* @return The value of the attribute. Returns <code>null</code> is the name105* does not exist in the given scope.106*107* @throws IllegalArgumentException108* if the name is empty or if the value of scope is invalid.109* @throws NullPointerException if the name is null.110*/111public Object getAttribute(String name, int scope);112113/**114* Remove an attribute in a given scope.115*116* @param name The name of the attribute to remove117* @param scope The scope in which to remove the attribute118*119* @return The removed value.120* @throws IllegalArgumentException121* if the name is empty or if the scope is invalid.122* @throws NullPointerException if the name is null.123*/124public Object removeAttribute(String name, int scope);125126/**127* Retrieves the value of the attribute with the given name in128* the scope occurring earliest in the search order. The order129* is determined by the numeric value of the scope parameter (lowest130* scope values first.)131*132* @param name The name of the the attribute to retrieve.133* @return The value of the attribute in the lowest scope for134* which an attribute with the given name is defined. Returns135* null if no attribute with the name exists in any scope.136* @throws NullPointerException if the name is null.137* @throws IllegalArgumentException if the name is empty.138*/139public Object getAttribute(String name);140141142/**143* Get the lowest scope in which an attribute is defined.144* @param name Name of the attribute145* .146* @return The lowest scope. Returns -1 if no attribute with the given147* name is defined in any scope.148* @throws NullPointerException if name is null.149* @throws IllegalArgumentException if name is empty.150*/151public int getAttributesScope(String name);152153/**154* Returns the <code>Writer</code> for scripts to use when displaying output.155*156* @return The <code>Writer</code>.157*/158public Writer getWriter();159160161/**162* Returns the <code>Writer</code> used to display error output.163*164* @return The <code>Writer</code>165*/166public Writer getErrorWriter();167168/**169* Sets the <code>Writer</code> for scripts to use when displaying output.170*171* @param writer The new <code>Writer</code>.172*/173public void setWriter(Writer writer);174175176/**177* Sets the <code>Writer</code> used to display error output.178*179* @param writer The <code>Writer</code>.180*/181public void setErrorWriter(Writer writer);182183/**184* Returns a <code>Reader</code> to be used by the script to read185* input.186*187* @return The <code>Reader</code>.188*/189public Reader getReader();190191192/**193* Sets the <code>Reader</code> for scripts to read input194* .195* @param reader The new <code>Reader</code>.196*/197public void setReader(Reader reader);198199/**200* Returns immutable <code>List</code> of all the valid values for201* scope in the ScriptContext.202*203* @return list of scope values204*/205public List<Integer> getScopes();206}207208209