001: /*
002: * %W% %E% %U%
003: *
004: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
005: * SUN PROPRIETARY/CONFIDENTAIL. Use is subject to license terms.
006: */
007:
008: package javax.script;
009:
010: import java.util.Map;
011:
012: /**
013: * Extended by classes that store results of compilations. State
014: * might be stored in the form of Java classes, Java class files or scripting
015: * language opcodes. The script may be executed repeatedly
016: * without reparsing.
017: * <br><br>
018: * Each <code>CompiledScript</code> is associated with a <code>ScriptEngine</code> -- A call to an <code>eval</code>
019: * method of the <code>CompiledScript</code> causes the execution of the script by the
020: * <code>ScriptEngine</code>. Changes in the state of the <code>ScriptEngine</code> caused by execution
021: * of tne <code>CompiledScript</code> may visible during subsequent executions of scripts by the engine.
022: *
023: * @author Mike Grogan
024: * @version 1.0
025: * @since 1.6
026: */
027: public abstract class CompiledScript {
028:
029: /**
030: * Executes the program stored in this <code>CompiledScript</code> object.
031: *
032: * @param context A <code>ScriptContext</code> that is used in the same way as
033: * the <code>ScriptContext</code> passed to the <code>eval</code> methods of
034: * <code>ScriptEngine</code>.
035: *
036: * @return The value returned by the script execution, if any. Should return <code>null</code>
037: * if no value is returned by the script execution.
038: *
039: * @throws ScriptException if an error occurs.
040: */
041:
042: public abstract Object eval(ScriptContext context)
043: throws ScriptException;
044:
045: /**
046: * Executes the program stored in the <code>CompiledScript</code> object using
047: * the supplied <code>Bindings</code> of attributes as the <code>ENGINE_SCOPE</code> of the
048: * associated <code>ScriptEngine</code> during script execution.
049: * <p>.
050: * The <code>GLOBAL_SCOPE</code> <code>Bindings</code>, <code>Reader</code> and <code>Writer</code>
051: * associated with the defaule <code>ScriptContext</code> of the associated <code>ScriptEngine</code> are used.
052: *
053: * @param bindings The bindings of attributes used for the <code>ENGINE_SCOPE</code>.
054: *
055: * @return The return value from the script execution
056: *
057: * @throws ScriptException if an error occurs.
058: *
059: */
060: public Object eval(Bindings bindings) throws ScriptException {
061:
062: ScriptContext ctxt = getEngine().getContext();
063:
064: if (bindings != null) {
065: SimpleScriptContext tempctxt = new SimpleScriptContext();
066: tempctxt.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
067: tempctxt.setBindings(ctxt
068: .getBindings(ScriptContext.GLOBAL_SCOPE),
069: ScriptContext.GLOBAL_SCOPE);
070: tempctxt.setWriter(ctxt.getWriter());
071: tempctxt.setReader(ctxt.getReader());
072: tempctxt.setErrorWriter(ctxt.getErrorWriter());
073: ctxt = tempctxt;
074: }
075:
076: return eval(ctxt);
077: }
078:
079: /**
080: * Executes the program stored in the <code>CompiledScript</code> object. The
081: * default <code>ScriptContext</code> of the associated <code>ScriptEngine</code> is used.
082: *
083: * @return The return value from the script execution
084: *
085: * @throws ScriptException if an error occurs.
086: *
087: */
088: public Object eval() throws ScriptException {
089:
090: return eval(getEngine().getContext());
091: }
092:
093: /**
094: * Returns the <code>ScriptEngine</code> wbose <code>compile</code> method created this <code>CompiledScript</code>.
095: * The <code>CompiledScript</code> will execute in this engine.
096: *
097: * @return The <code>ScriptEngine</code> that created this <code>CompiledScript</code>
098: */
099: public abstract ScriptEngine getEngine();
100:
101: }
|