001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ScriptedEngineBSF.java 3784 2007-06-11 16:44:35Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.engine.exceptions.EngineException;
011: import com.uwyn.rife.engine.exceptions.ScriptErrorException;
012: import com.uwyn.rife.tools.exceptions.LightweightError;
013: import java.lang.reflect.InvocationTargetException;
014: import org.apache.bsf.BSFEngine;
015: import org.apache.bsf.BSFException;
016: import org.apache.bsf.BSFManager;
017:
018: class ScriptedEngineBSF extends ScriptedEngine {
019: protected BSFManager mManager = null;
020: protected BSFEngine mEngine = null;
021:
022: ScriptedEngineBSF(String language, String code)
023: throws EngineException {
024: super (language, code);
025:
026: mManager = new BSFManager();
027: try {
028: mEngine = mManager.loadScriptingEngine(mLanguage);
029: } catch (BSFException e) {
030: e.fillInStackTrace();
031: throw new EngineException(e);
032: }
033: }
034:
035: void processElement() {
036: try {
037: mManager.declareBean("element", getElement(),
038: ElementScripted.class);
039: mManager.exec(mLanguage, getElement().getElementInfo()
040: .getImplementation(), 0, 0, mCode);
041: mManager.undeclareBean("element");
042: mEngine = null;
043: mManager.terminate();
044: mManager = null;
045: } catch (BSFException e) {
046: extractEngineException(e);
047: }
048: }
049:
050: boolean childTriggered(String name, String[] values) {
051: try {
052: Object result = null;
053:
054: result = mEngine.call(null, "childTriggered", new Object[] {
055: name, values });
056:
057: if (result != null
058: && (result.toString().equals("true") || result
059: .toString().equals("1"))) {
060: return true;
061: } else {
062: return false;
063: }
064: } catch (BSFException e) {
065: extractEngineException(e);
066: return false;
067: } catch (Throwable e) {
068: return false;
069: }
070: }
071:
072: private void extractEngineException(BSFException e)
073: throws EngineException {
074: if (e.getTargetException() != null) {
075: // Rhino and Pnuts
076: if (e.getTargetException() instanceof LightweightError) {
077: throw (LightweightError) e.getTargetException();
078: } else if (e.getTargetException() instanceof EngineException) {
079: throw (EngineException) e.getTargetException();
080: }
081:
082: // Jython
083: // using string comparison to make it possible for scripting engines to be pluggable
084: if (e.getTargetException().getClass().getName().equals(
085: "org.python.core.PyException")) {
086: ElementJythonExceptionHandler python_handler = new ElementJythonExceptionHandler();
087: python_handler.execute(e);
088: }
089:
090: // JRuby
091: // using string comparison to make it possible for scripting engines to be pluggable
092: if (e.getTargetException().getClass().getName().equals(
093: "org.jruby.exceptions.RaiseException")
094: && e.getTargetException().getCause() != null) {
095: if (e.getTargetException().getCause() instanceof EngineException) {
096: throw (EngineException) e.getTargetException()
097: .getCause();
098: } else if (e.getTargetException().getCause() instanceof LightweightError) {
099: throw (LightweightError) e.getTargetException()
100: .getCause();
101: }
102: }
103:
104: // Jacl
105: // using string comparison to make it possible for scripting engines to be pluggable
106: if (e.getTargetException().getClass().getName().equals(
107: "tcl.lang.ReflectException")
108: && e.getTargetException().getCause() != null) {
109: Throwable cause = null;
110:
111: // Jacl 1.2.6
112: if (e.getTargetException().getCause() instanceof InvocationTargetException
113: && e.getTargetException().getCause().getCause() != null) {
114: cause = e.getTargetException().getCause()
115: .getCause();
116: }
117: // Jacl 1.3.3
118: else {
119: cause = e.getTargetException().getCause();
120: }
121:
122: if (cause != null) {
123: if (cause instanceof EngineException) {
124: throw (EngineException) cause;
125: } else if (cause instanceof LightweightError) {
126: throw (LightweightError) cause;
127: }
128: }
129: }
130:
131: // Beanshell
132: // using string comparison to make it possible for scripting engines to be pluggable
133: if (e.getTargetException().getClass().getName().equals(
134: "bsh.TargetError")
135: && ((bsh.TargetError) e.getTargetException())
136: .getTarget() != null) {
137: if (((bsh.TargetError) e.getTargetException())
138: .getTarget() instanceof EngineException) {
139: throw (EngineException) ((bsh.TargetError) e
140: .getTargetException()).getTarget();
141: } else if (((bsh.TargetError) e.getTargetException())
142: .getTarget() instanceof LightweightError) {
143: throw (LightweightError) ((bsh.TargetError) e
144: .getTargetException()).getTarget();
145: }
146: }
147: }
148:
149: throw new ScriptErrorException(getElement().getElementInfo()
150: .getDeclarationName(), e);
151: }
152:
153: private class ElementJythonExceptionHandler {
154: private void execute(BSFException e) {
155: org.python.core.PyException python_exception = (org.python.core.PyException) e
156: .getTargetException();
157: try {
158: Object converted;
159:
160: converted = python_exception.value
161: .__tojava__(EngineException.class);
162: if (converted != org.python.core.Py.NoConversion) {
163: throw (EngineException) converted;
164: }
165:
166: converted = python_exception.value
167: .__tojava__(LightweightError.class);
168: if (converted != org.python.core.Py.NoConversion) {
169: throw (LightweightError) converted;
170: }
171:
172: throw new ScriptErrorException(getElement()
173: .getElementInfo().getDeclarationName(), e);
174: } catch (ClassCastException e2) {
175: throw new EngineException(e);
176: }
177: }
178: }
179: }
|