KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > script > CompiledScript


1 /*
2  * @(#)CompiledScript.java 1.3 05/11/17 14:24:13
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
6  */

7
8 package javax.script;
9
10 import java.util.Map JavaDoc;
11
12 /**
13  * Extended by classes that store results of compilations. State
14  * might be stored in the form of Java classes, Java class files or scripting
15  * language opcodes. The script may be executed repeatedly
16  * without reparsing.
17  * <br><br>
18  * Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an <code>eval</code>
19  * method of the <code>CompiledScript</code> causes the execution of the script by the
20  * <code>ScriptEngine</code>. Changes in the state of the <code>ScriptEngine</code> caused by execution
21  * of tne <code>CompiledScript</code> may visible during subsequent executions of scripts by the engine.
22  *
23  * @author Mike Grogan
24  * @version 1.0
25  * @since 1.6
26  */

27 public abstract class CompiledScript {
28     
29     /**
30      * Executes the program stored in this <code>CompiledScript</code> object.
31      *
32      * @param context A <code>ScriptContext</code> that is used in the same way as
33      * the <code>ScriptContext</code> passed to the <code>eval</code> methods of
34      * <code>ScriptEngine</code>.
35      *
36      * @return The value returned by the script execution, if any. Should return <code>null</code>
37      * if no value is returned by the script execution.
38      *
39      * @throws ScriptException if an error occurs.
40      * @throws NullPointerException if context is null.
41      */

42     
43     public abstract Object JavaDoc eval(ScriptContext context) throws ScriptException;
44     
45     /**
46      * Executes the program stored in the <code>CompiledScript</code> object using
47      * the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
48      * associated <code>ScriptEngine</code> during script execution. If bindings is null,
49      * then the effect of calling this method is same as that of eval(getEngine().getContext()).
50      * <p>.
51      * The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
52      * associated with the default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
53      *
54      * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
55      *
56      * @return The return value from the script execution
57      *
58      * @throws ScriptException if an error occurs.
59      */

60     public Object JavaDoc eval(Bindings bindings) throws ScriptException {
61         
62         ScriptContext ctxt = getEngine().getContext();
63         
64         if (bindings != null) {
65             SimpleScriptContext tempctxt = new SimpleScriptContext();
66             tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
67             tempctxt.setBindings(ctxt.getBindings(ScriptContext.GLOBAL_SCOPE),
68                     ScriptContext.GLOBAL_SCOPE);
69             tempctxt.setWriter(ctxt.getWriter());
70             tempctxt.setReader(ctxt.getReader());
71             tempctxt.setErrorWriter(ctxt.getErrorWriter());
72             ctxt = tempctxt;
73         }
74         
75         return eval(ctxt);
76     }
77     
78     
79     /**
80      * Executes the program stored in the <code>CompiledScript</code> object. The
81      * default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.
82      * The effect of calling this method is same as that of eval(getEngine().getContext()).
83      *
84      * @return The return value from the script execution
85      *
86      * @throws ScriptException if an error occurs.
87      */

88     public Object JavaDoc eval() throws ScriptException {
89         return eval(getEngine().getContext());
90     }
91     
92     /**
93      * Returns the <code>ScriptEngine</code> wbose <code>compile</code> method created this <code>CompiledScript</code>.
94      * The <code>CompiledScript</code> will execute in this engine.
95      *
96      * @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>
97      */

98     public abstract ScriptEngine getEngine();
99     
100 }
101
Popular Tags