001: /* Copyright (c) 2006-2007, Vladimir Nikic
002: All rights reserved.
003:
004: Redistribution and use of this software in source and binary forms,
005: with or without modification, are permitted provided that the following
006: conditions are met:
007:
008: * Redistributions of source code must retain the above
009: copyright notice, this list of conditions and the
010: following disclaimer.
011:
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the
014: following disclaimer in the documentation and/or other
015: materials provided with the distribution.
016:
017: * The name of Web-Harvest may not be used to endorse or promote
018: products derived from this software without specific prior
019: written permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: POSSIBILITY OF SUCH DAMAGE.
032:
033: You can contact Vladimir Nikic by sending e-mail to
034: nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
035: subject line.
036: */
037: package org.webharvest.runtime.scripting;
038:
039: import bsh.EvalError;
040: import bsh.Interpreter;
041: import org.webharvest.exception.ScriptException;
042:
043: import java.util.Iterator;
044: import java.util.Map;
045:
046: /**
047: * Wrapper for scripting engine.
048: */
049: public class ScriptEngine extends Interpreter {
050:
051: public static final String CONTEXT_VARIABLE_NAME = "___web_harvest_context___";
052:
053: private Map context;
054:
055: /**
056: * Constructor - initializes context used in engine.
057: * @param context
058: */
059: public ScriptEngine(Map context) {
060: this .getNameSpace().importCommands(
061: "org.webharvest.runtime.scripting");
062: this .context = context;
063: try {
064: this .set(CONTEXT_VARIABLE_NAME, this .context);
065: } catch (EvalError e) {
066: throw new ScriptException(
067: "Cannot set Web-Harvest context in scripter: "
068: + e.getMessage(), e);
069: }
070: }
071:
072: /**
073: * Sets variable in scripter context.
074: * @param name
075: * @param value
076: */
077: public void setVariable(String name, Object value) {
078: try {
079: super .set(name, value);
080: } catch (EvalError e) {
081: throw new ScriptException(
082: "Cannot set variable in scripter: "
083: + e.getMessage(), e);
084: }
085: }
086:
087: /**
088: * Evaluates specified expression or code block.
089: * @return value of evaluation or null if there is nothing.
090: */
091: public Object eval(String expression) {
092: // push all variables from context to the scripter
093: if (this .context != null) {
094: Iterator it = this .context.entrySet().iterator();
095: while (it.hasNext()) {
096: Map.Entry entry = (Map.Entry) it.next();
097: String name = (String) entry.getKey();
098: setVariable(name, entry.getValue());
099: }
100: }
101:
102: try {
103: return super .eval(expression);
104: } catch (EvalError e) {
105: throw new ScriptException("Error during script execution: "
106: + e.getMessage(), e);
107: }
108: }
109:
110: /**
111: * Sets the specified variable in the context.
112: * @param name
113: * @param value
114: */
115: public void setInContext(String name, Object value) {
116: if (this.context != null) {
117: this.context.put(name, value);
118: }
119: }
120:
121: }
|