1 /*
2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package javax.script;
27 
28 import java.io.Reader;
29 import java.util.Map;
30 import java.util.Set;
31 
32 /**
33  * <code>ScriptEngine</code> is the fundamental interface whose methods must be
34  * fully functional in every implementation of this specification.
35  * <br><br>
36  * These methods provide basic scripting functionality.  Applications written to this
37  * simple interface are expected to work with minimal modifications in every implementation.
38  * It includes methods that execute scripts, and ones that set and get values.
39  * <br><br>
40  * The values are key/value pairs of two types.  The first type of pairs consists of
41  * those whose keys are reserved and defined in this specification or  by individual
42  * implementations.  The values in the pairs with reserved keys have specified meanings.
43  * <br><br>
44  * The other type of pairs consists of those that create Java language Bindings, the values are
45  * usually represented in scripts by the corresponding keys or by decorated forms of them.
46  *
47  * @author Mike Grogan
48  * @since 1.6
49  */
50 
51 public interface ScriptEngine  {
52 
53     /**
54      * Reserved key for a named value that passes
55      * an array of positional arguments to a script.
56      */
57     public static final String ARGV="javax.script.argv";
58 
59     /**
60      * Reserved key for a named value that is
61      * the name of the file being executed.
62      */
63     public static final String FILENAME = "javax.script.filename";
64 
65     /**
66      * Reserved key for a named value that is
67      * the name of the <code>ScriptEngine</code> implementation.
68      */
69     public static final String ENGINE = "javax.script.engine";
70 
71     /**
72      * Reserved key for a named value that identifies
73      * the version of the <code>ScriptEngine</code> implementation.
74      */
75     public static final String ENGINE_VERSION = "javax.script.engine_version";
76 
77     /**
78      * Reserved key for a named value that identifies
79      * the short name of the scripting language.  The name is used by the
80      * <code>ScriptEngineManager</code> to locate a <code>ScriptEngine</code>
81      * with a given name in the <code>getEngineByName</code> method.
82      */
83     public static final String NAME = "javax.script.name";
84 
85     /**
86      * Reserved key for a named value that is
87      * the full name of Scripting Language supported by the implementation.
88      */
89     public static final String LANGUAGE = "javax.script.language";
90 
91     /**
92      * Reserved key for the named value that identifies
93      * the version of the scripting language supported by the implementation.
94      */
95     public static final String LANGUAGE_VERSION ="javax.script.language_version";
96 
97 
98     /**
99      * Causes the immediate execution of the script whose source is the String
100      * passed as the first argument.  The script may be reparsed or recompiled before
101      * execution.  State left in the engine from previous executions, including
102      * variable values and compiled procedures may be visible during this execution.
103      *
104      * @param script The script to be executed by the script engine.
105      *
106      * @param context A <code>ScriptContext</code> exposing sets of attributes in
107      * different scopes.  The meanings of the scopes <code>ScriptContext.GLOBAL_SCOPE</code>,
108      * and <code>ScriptContext.ENGINE_SCOPE</code> are defined in the specification.
109      * <br><br>
110      * The <code>ENGINE_SCOPE</code> <code>Bindings</code> of the <code>ScriptContext</code> contains the
111      * bindings of scripting variables to application objects to be used during this
112      * script execution.
113      *
114      *
115      * @return The value returned from the execution of the script.
116      *
117      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
118      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
119      * implementations.
120      * @throws NullPointerException if either argument is null.
121      */
eval(String script, ScriptContext context)122     public Object eval(String script, ScriptContext context) throws ScriptException;
123 
124 
125     /**
126      * Same as <code>eval(String, ScriptContext)</code> where the source of the script
127      * is read from a <code>Reader</code>.
128      *
129      * @param reader The source of the script to be executed by the script engine.
130      *
131      * @param context The <code>ScriptContext</code> passed to the script engine.
132      *
133      * @return The value returned from the execution of the script.
134      *
135      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
136      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
137      * implementations.
138      * @throws NullPointerException if either argument is null.
139      */
eval(Reader reader , ScriptContext context)140     public Object eval(Reader reader , ScriptContext context) throws ScriptException;
141 
142     /**
143      * Executes the specified script.  The default <code>ScriptContext</code> for the <code>ScriptEngine</code>
144      * is used.
145      *
146      * @param script The script language source to be executed.
147      *
148      * @return The value returned from the execution of the script.
149      *
150      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
151      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
152      * implementations.
153      * @throws NullPointerException if the argument is null.
154      */
eval(String script)155     public Object eval(String script) throws ScriptException;
156 
157     /**
158      * Same as <code>eval(String)</code> except that the source of the script is
159      * provided as a <code>Reader</code>
160      *
161      * @param reader The source of the script.
162      *
163      * @return The value returned by the script.
164      *
165      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
166      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
167      * implementations.
168      * @throws NullPointerException if the argument is null.
169      */
eval(Reader reader)170     public Object eval(Reader reader) throws ScriptException;
171 
172     /**
173      * Executes the script using the <code>Bindings</code> argument as the <code>ENGINE_SCOPE</code>
174      * <code>Bindings</code> of the <code>ScriptEngine</code> during the script execution.  The
175      * <code>Reader</code>, <code>Writer</code> and non-<code>ENGINE_SCOPE</code> <code>Bindings</code> of the
176      * default <code>ScriptContext</code> are used. The <code>ENGINE_SCOPE</code>
177      * <code>Bindings</code> of the <code>ScriptEngine</code> is not changed, and its
178      * mappings are unaltered by the script execution.
179      *
180      * @param script The source for the script.
181      *
182      * @param n The <code>Bindings</code> of attributes to be used for script execution.
183      *
184      * @return The value returned by the script.
185      *
186      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
187      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
188      * implementations.
189      * @throws NullPointerException if either argument is null.
190      */
eval(String script, Bindings n)191     public Object eval(String script, Bindings n) throws ScriptException;
192 
193     /**
194      * Same as <code>eval(String, Bindings)</code> except that the source of the script
195      * is provided as a <code>Reader</code>.
196      *
197      * @param reader The source of the script.
198      * @param n The <code>Bindings</code> of attributes.
199      *
200      * @return The value returned by the script.
201      *
202      * @throws ScriptException if an error occurs in script. ScriptEngines should create and throw
203      * <code>ScriptException</code> wrappers for checked Exceptions thrown by underlying scripting
204      * implementations.
205      * @throws NullPointerException if either argument is null.
206      */
eval(Reader reader , Bindings n)207     public Object eval(Reader reader , Bindings n) throws ScriptException;
208 
209 
210 
211     /**
212      * Sets a key/value pair in the state of the ScriptEngine that may either create
213      * a Java Language Binding to be used in the execution of scripts or be used in some
214      * other way, depending on whether the key is reserved.  Must have the same effect as
215      * <code>getBindings(ScriptContext.ENGINE_SCOPE).put</code>.
216      *
217      * @param key The name of named value to add
218      * @param value The value of named value to add.
219      *
220      * @throws NullPointerException if key is null.
221      * @throws IllegalArgumentException if key is empty.
222      */
put(String key, Object value)223     public void put(String key, Object value);
224 
225 
226     /**
227      * Retrieves a value set in the state of this engine.  The value might be one
228      * which was set using <code>setValue</code> or some other value in the state
229      * of the <code>ScriptEngine</code>, depending on the implementation.  Must have the same effect
230      * as <code>getBindings(ScriptContext.ENGINE_SCOPE).get</code>
231      *
232      * @param key The key whose value is to be returned
233      * @return the value for the given key
234      *
235      * @throws NullPointerException if key is null.
236      * @throws IllegalArgumentException if key is empty.
237      */
get(String key)238     public Object get(String key);
239 
240 
241     /**
242      * Returns a scope of named values.  The possible scopes are:
243      * <br><br>
244      * <ul>
245      * <li><code>ScriptContext.GLOBAL_SCOPE</code> - The set of named values representing global
246      * scope. If this <code>ScriptEngine</code> is created by a <code>ScriptEngineManager</code>,
247      * then the manager sets global scope bindings. This may be <code>null</code> if no global
248      * scope is associated with this <code>ScriptEngine</code></li>
249      * <li><code>ScriptContext.ENGINE_SCOPE</code> - The set of named values representing the state of
250      * this <code>ScriptEngine</code>.  The values are generally visible in scripts using
251      * the associated keys as variable names.</li>
252      * <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
253      * </li>
254      * </ul>
255      * <br><br>
256      * The <code>Bindings</code> instances that are returned must be identical to those returned by the
257      * <code>getBindings</code> method of <code>ScriptContext</code> called with corresponding arguments on
258      * the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
259      *
260      * @param scope Either <code>ScriptContext.ENGINE_SCOPE</code> or <code>ScriptContext.GLOBAL_SCOPE</code>
261      * which specifies the <code>Bindings</code> to return.  Implementations of <code>ScriptContext</code>
262      * may define additional scopes.  If the default <code>ScriptContext</code> of the <code>ScriptEngine</code>
263      * defines additional scopes, any of them can be passed to get the corresponding <code>Bindings</code>.
264      *
265      * @return The <code>Bindings</code> with the specified scope.
266      *
267      * @throws IllegalArgumentException if specified scope is invalid
268      *
269      */
getBindings(int scope)270     public Bindings getBindings(int scope);
271 
272     /**
273      * Sets a scope of named values to be used by scripts.  The possible scopes are:
274      *<br><br>
275      * <ul>
276      * <li><code>ScriptContext.ENGINE_SCOPE</code> - The specified <code>Bindings</code> replaces the
277      * engine scope of the <code>ScriptEngine</code>.
278      * </li>
279      * <li><code>ScriptContext.GLOBAL_SCOPE</code> - The specified <code>Bindings</code> must be visible
280      * as the <code>GLOBAL_SCOPE</code>.
281      * </li>
282      * <li>Any other value of scope defined in the default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
283      *</li>
284      * </ul>
285      * <br><br>
286      * The method must have the same effect as calling the <code>setBindings</code> method of
287      * <code>ScriptContext</code> with the corresponding value of <code>scope</code> on the default
288      * <code>ScriptContext</code> of the <code>ScriptEngine</code>.
289      *
290      * @param bindings The <code>Bindings</code> for the specified scope.
291      * @param scope The specified scope.  Either <code>ScriptContext.ENGINE_SCOPE</code>,
292      * <code>ScriptContext.GLOBAL_SCOPE</code>, or any other valid value of scope.
293      *
294      * @throws IllegalArgumentException if the scope is invalid
295      * @throws NullPointerException if the bindings is null and the scope is
296      * <code>ScriptContext.ENGINE_SCOPE</code>
297      */
setBindings(Bindings bindings, int scope)298     public void setBindings(Bindings bindings, int scope);
299 
300 
301     /**
302      * Returns an uninitialized <code>Bindings</code>.
303      *
304      * @return A <code>Bindings</code> that can be used to replace the state of this <code>ScriptEngine</code>.
305      **/
createBindings()306     public Bindings createBindings();
307 
308 
309     /**
310      * Returns the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader
311      * and Writers are used for script executions when no <code>ScriptContext</code> is specified.
312      *
313      * @return The default <code>ScriptContext</code> of the <code>ScriptEngine</code>.
314      */
getContext()315     public ScriptContext getContext();
316 
317     /**
318      * Sets the default <code>ScriptContext</code> of the <code>ScriptEngine</code> whose Bindings, Reader
319      * and Writers are used for script executions when no <code>ScriptContext</code> is specified.
320      *
321      * @param context A <code>ScriptContext</code> that will replace the default <code>ScriptContext</code> in
322      * the <code>ScriptEngine</code>.
323      * @throws NullPointerException if context is null.
324      */
setContext(ScriptContext context)325     public void setContext(ScriptContext context);
326 
327     /**
328      * Returns a <code>ScriptEngineFactory</code> for the class to which this <code>ScriptEngine</code> belongs.
329      *
330      * @return The <code>ScriptEngineFactory</code>
331      */
getFactory()332     public ScriptEngineFactory getFactory();
333 }
334