Path: blob/master/src/java.scripting/share/classes/javax/script/ScriptContext.java
41149 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. If the scope is <code>GLOBAL_SCOPE</code>88* and no Bindings is set for <code>GLOBAL_SCOPE</code>, then setAttribute call is a no-op.89*90* @param name The name of the attribute to set91* @param value The value of the attribute92* @param scope The scope in which to set the attribute93*94* @throws IllegalArgumentException95* if the name is empty or if the scope is invalid.96* @throws NullPointerException if the name is null.97*/98public void setAttribute(String name, Object value, int scope);99100/**101* Gets the value of an attribute in a given scope.102*103* @param name The name of the attribute to retrieve.104* @param scope The scope in which to retrieve the attribute.105* @return The value of the attribute. Returns <code>null</code> is the name106* does not exist in the given scope.107*108* @throws IllegalArgumentException109* if the name is empty or if the value of scope is invalid.110* @throws NullPointerException if the name is null.111*/112public Object getAttribute(String name, int scope);113114/**115* Remove an attribute in a given scope.116*117* @param name The name of the attribute to remove118* @param scope The scope in which to remove the attribute119*120* @return The removed value.121* @throws IllegalArgumentException122* if the name is empty or if the scope is invalid.123* @throws NullPointerException if the name is null.124*/125public Object removeAttribute(String name, int scope);126127/**128* Retrieves the value of the attribute with the given name in129* the scope occurring earliest in the search order. The order130* is determined by the numeric value of the scope parameter (lowest131* scope values first.)132*133* @param name The name of the attribute to retrieve.134* @return The value of the attribute in the lowest scope for135* which an attribute with the given name is defined. Returns136* null if no attribute with the name exists in any scope.137* @throws NullPointerException if the name is null.138* @throws IllegalArgumentException if the name is empty.139*/140public Object getAttribute(String name);141142143/**144* Get the lowest scope in which an attribute is defined.145* @param name Name of the attribute146* .147* @return The lowest scope. Returns -1 if no attribute with the given148* name is defined in any scope.149* @throws NullPointerException if name is null.150* @throws IllegalArgumentException if name is empty.151*/152public int getAttributesScope(String name);153154/**155* Returns the <code>Writer</code> for scripts to use when displaying output.156*157* @return The <code>Writer</code>.158*/159public Writer getWriter();160161162/**163* Returns the <code>Writer</code> used to display error output.164*165* @return The <code>Writer</code>166*/167public Writer getErrorWriter();168169/**170* Sets the <code>Writer</code> for scripts to use when displaying output.171*172* @param writer The new <code>Writer</code>.173*/174public void setWriter(Writer writer);175176177/**178* Sets the <code>Writer</code> used to display error output.179*180* @param writer The <code>Writer</code>.181*/182public void setErrorWriter(Writer writer);183184/**185* Returns a <code>Reader</code> to be used by the script to read186* input.187*188* @return The <code>Reader</code>.189*/190public Reader getReader();191192193/**194* Sets the <code>Reader</code> for scripts to read input195* .196* @param reader The new <code>Reader</code>.197*/198public void setReader(Reader reader);199200/**201* Returns immutable <code>List</code> of all the valid values for202* scope in the ScriptContext.203*204* @return list of scope values205*/206public List<Integer> getScopes();207}208209210