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.util.Map;
29 
30 /**
31  * Extended by classes that store results of compilations.  State
32  * might be stored in the form of Java classes, Java class files or scripting
33  * language opcodes.  The script may be executed repeatedly
34  * without reparsing.
35  * <br><br>
36  * Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an  <code>eval</code>
37  * method of the <code>CompiledScript</code> causes the execution of the script by the
38  * <code>ScriptEngine</code>.  Changes in the state of the <code>ScriptEngine</code> caused by execution
39  * of the <code>CompiledScript</code>  may visible during subsequent executions of scripts by the engine.
40  *
41  * @author Mike Grogan
42  * @since 1.6
43  */
44 public abstract class CompiledScript {
45 
46     /**
47      * Executes the program stored in this <code>CompiledScript</code> object.
48      *
49      * @param context A <code>ScriptContext</code> that is used in the same way as
50      * the <code>ScriptContext</code> passed to the <code>eval</code> methods of
51      * <code>ScriptEngine</code>.
52      *
53      * @return The value returned by the script execution, if any.  Should return <code>null</code>
54      * if no value is returned by the script execution.
55      *
56      * @throws ScriptException if an error occurs.
57      * @throws NullPointerException if context is null.
58      */
59 
eval(ScriptContext context)60     public abstract Object eval(ScriptContext context) throws ScriptException;
61 
62     /**
63      * Executes the program stored in the <code>CompiledScript</code> object using
64      * the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
65      * associated <code>ScriptEngine</code> during script execution.  If bindings is null,
66      * then the effect of calling this method is same as that of eval(getEngine().getContext()).
67      * <p>.
68      * The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
69      * associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
70      *
71      * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
72      *
73      * @return The return value from the script execution
74      *
75      * @throws ScriptException if an error occurs.
76      */
eval(Bindings bindings)77     public Object eval(Bindings bindings) throws ScriptException {
78 
79         ScriptContext ctxt = getEngine().getContext();
80 
81         if (bindings != null) {
82             SimpleScriptContext tempctxt = new SimpleScriptContext(ctxt.getReader(), ctxt.getWriter(), ctxt.getErrorWriter());
83             tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
84             tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
85                     ScriptContext.GLOBAL_SCOPE);
86             ctxt = tempctxt;
87         }
88 
89         return eval(ctxt);
90     }
91 
92 
93     /**
94      * Executes the program stored in the <code>CompiledScript</code> object.  The
95      * default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.
96      * The effect of calling this method is same as that of eval(getEngine().getContext()).
97      *
98      * @return The return value from the script execution
99      *
100      * @throws ScriptException if an error occurs.
101      */
eval()102     public Object eval() throws ScriptException {
103         return eval(getEngine().getContext());
104     }
105 
106     /**
107      * Returns the <code>ScriptEngine</code> whose <code>compile</code> method created this <code>CompiledScript</code>.
108      * The <code>CompiledScript</code> will execute in this engine.
109      *
110      * @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>
111      */
getEngine()112     public abstract ScriptEngine getEngine();
113 
114 }
115