01: package org.mvel.jsr223;
02:
03: import org.mvel.ExecutableStatement;
04: import org.mvel.integration.impl.MapVariableResolverFactory;
05: import org.mvel.integration.VariableResolverFactory;
06:
07: import javax.script.CompiledScript;
08: import javax.script.ScriptContext;
09: import javax.script.ScriptException;
10: import javax.script.ScriptEngine;
11: import java.util.Map;
12:
13: /**
14: * The optional interface <code>CompiledScript</code> is implemented by MVEL's
15: * JSR 223 extention jar. If the ASM dependency is in the class path, then this
16: * ExecutableStatement will truly be compiled into a Class on the fly. Otherwise,
17: * this each invocation of eval(..) will parse up the expression provided. For more
18: * information, check out <a href="http://mvel.codehaus.org">http://mvel.codehaus.org<a>.
19: * @author Richard L. Burton III
20: * @version 1.0
21: * @since MVEL 1.2
22: */
23: public class MVELCompiledScript extends CompiledScript {
24:
25: /**
26: * The ScriptEngine that created the CompiledScript.
27: */
28: private ScriptEngine scriptEngine;
29:
30: /**
31: * The compiled ExecutableStatement object.
32: */
33: private ExecutableStatement executableStatement;
34:
35: /**
36: * Constructs a CompiledScript with reference to the ScriptEngine that
37: * created it.
38: * @param scriptEngine The ScriptEngine that created the CompiledScript.
39: * @param executableStatement The ExecutableStatement to be executed.
40: */
41: public MVELCompiledScript(ScriptEngine scriptEngine,
42: ExecutableStatement executableStatement) {
43: this .scriptEngine = scriptEngine;
44: this .executableStatement = executableStatement;
45: }
46:
47: /**
48: * Executes the program stored in this CompiledScript object.
49: * @param scriptContext A ScriptContext that is used in the same way as the
50: * ScriptContext passed to the eval methods of ScriptEngine.
51: * @return The value returned by the script execution, if any. Should return
52: * null if no value is returned by the script execution.
53: * @throws ScriptException if an error occurs.
54: * @throws NullPointerException if context is null.
55: */
56: public Object eval(ScriptContext scriptContext)
57: throws ScriptException {
58: Map<String, Object> scriptContextEngineView = new MVELScriptContextMap(
59: scriptContext);
60: VariableResolverFactory variableResolverFactory = new MapVariableResolverFactory(
61: scriptContextEngineView);
62: return executableStatement.getValue(scriptContext,
63: variableResolverFactory);
64: }
65:
66: /**
67: * Returns the ScriptEngine wbose compile method created this CompiledScript. The
68: * CompiledScript will execute in this engine.
69: * @return The ScriptEngine that created this CompiledScript
70: */
71: public ScriptEngine getEngine() {
72: return scriptEngine;
73: }
74:
75: }
|