| 1 | |
package org.webslinger.bsf; |
| 2 | |
|
| 3 | |
import java.util.Arrays; |
| 4 | |
import java.util.Iterator; |
| 5 | |
import java.util.Vector; |
| 6 | |
|
| 7 | |
import org.apache.bsf.BSFDeclaredBean; |
| 8 | |
import org.apache.bsf.BSFException; |
| 9 | |
import org.apache.bsf.BSFManager; |
| 10 | |
import org.apache.bsf.util.CodeBuffer; |
| 11 | |
|
| 12 | 67 | public abstract class CompilingLanguageEngineImpl<V, C> extends LanguageEngineImpl { |
| 13 | |
protected Compiler<V, C> compiler; |
| 14 | |
|
| 15 | |
public void initialize(BSFManager manager, String lang, Vector declaredBeans) throws BSFException { |
| 16 | 67 | super.initialize(manager, lang, declaredBeans); |
| 17 | 67 | if (compiler != null) { |
| 18 | 67 | compiler.initialize(manager); |
| 19 | 67 | Iterator it = declaredBeans.iterator(); |
| 20 | 1248 | while (it.hasNext()) { |
| 21 | 1181 | BSFDeclaredBean bean = (BSFDeclaredBean) it.next(); |
| 22 | 1181 | compiler.declareBean(bean); |
| 23 | 1181 | } |
| 24 | |
} |
| 25 | 67 | } |
| 26 | |
|
| 27 | |
public Object call(Object object, String name, Object[] args) throws BSFException { |
| 28 | 0 | throw new BSFException(BSFException.REASON_UNSUPPORTED_FEATURE, object + "." + name + "(" + Arrays.asList(args) + ")"); |
| 29 | |
} |
| 30 | |
|
| 31 | |
public Object apply(String source, int lineNo, int columnNo, Object body, String[] names, Object[] args) throws BSFException { |
| 32 | |
try { |
| 33 | 0 | ApplyKeyDynamic<C> key = new ApplyKeyDynamic<C>(getId(body), names, args); |
| 34 | 0 | return compiler.unwrap(apply(body, compiler.getObject(key), key.getArgs(compiler))); |
| 35 | 0 | } catch (BSFException e) { |
| 36 | 0 | throw e; |
| 37 | 0 | } catch (RuntimeException e) { |
| 38 | 0 | throw e; |
| 39 | 0 | } catch (Error e) { |
| 40 | 0 | throw e; |
| 41 | 0 | } catch (Throwable t) { |
| 42 | 0 | throw (RuntimeException) new RuntimeException(t.getMessage(), t); |
| 43 | |
} |
| 44 | |
} |
| 45 | |
|
| 46 | |
public Object apply(String source, int lineNo, int columnNo, Object body, String[] names, Object[] args, Class[] types) throws BSFException { |
| 47 | |
|
| 48 | |
try { |
| 49 | 1468 | ApplyKey<C> key = new ApplyKey<C>(getId(body), names, types); |
| 50 | 1468 | V compiled = compiler.getObject(key); |
| 51 | 1470 | Object[] wrappedArgs = compiler.newWrapArray(args); |
| 52 | 6881 | for (int i = 0; i < args.length; i++) { |
| 53 | 5417 | wrappedArgs[i] = compiler.wrapForArgs(i, args[i], types[i]); |
| 54 | |
} |
| 55 | 1472 | return compiler.unwrap(apply(body, compiled, names, wrappedArgs)); |
| 56 | 0 | } catch (BSFException e) { |
| 57 | 0 | throw e; |
| 58 | 0 | } catch (RuntimeException e) { |
| 59 | 0 | throw e; |
| 60 | 0 | } catch (Error e) { |
| 61 | 0 | throw e; |
| 62 | 0 | } catch (Throwable t) { |
| 63 | 0 | throw (RuntimeException) new RuntimeException(t.getMessage(), t); |
| 64 | |
} |
| 65 | |
} |
| 66 | |
|
| 67 | |
public void compileApply(String source, int lineNo, int columnNo, Object body, Vector paramNames, Vector args, CodeBuffer cb) throws BSFException { |
| 68 | 0 | throw new IllegalArgumentException(body.toString()); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public void compileExpr(String source, int lineNo, int columnNo, Object expr, CodeBuffer cb) throws BSFException { |
| 72 | 0 | throw new IllegalArgumentException(expr.toString()); |
| 73 | |
} |
| 74 | |
|
| 75 | |
public void compileScript(String source, int lineNo, int columnNo, Object script, CodeBuffer cb) throws BSFException { |
| 76 | 0 | throw new IllegalArgumentException(script.toString()); |
| 77 | |
} |
| 78 | |
|
| 79 | |
public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException { |
| 80 | |
try { |
| 81 | 0 | return compiler.unwrap(eval(expr, compiler.getObject(new EvalKey<C>(getId(expr))))); |
| 82 | 0 | } catch (BSFException e) { |
| 83 | 0 | throw e; |
| 84 | 0 | } catch (RuntimeException e) { |
| 85 | 0 | throw e; |
| 86 | 0 | } catch (Error e) { |
| 87 | 0 | throw e; |
| 88 | 0 | } catch (Throwable t) { |
| 89 | 0 | throw (RuntimeException) new RuntimeException(t.getMessage(), t); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | |
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { |
| 94 | |
try { |
| 95 | 0 | exec(script, compiler.getObject(new ExecKey<C>(getId(script)))); |
| 96 | 0 | } catch (BSFException e) { |
| 97 | 0 | throw e; |
| 98 | 0 | } catch (RuntimeException e) { |
| 99 | 0 | throw e; |
| 100 | 0 | } catch (Error e) { |
| 101 | 0 | throw e; |
| 102 | 0 | } catch (Throwable t) { |
| 103 | 0 | throw (RuntimeException) new RuntimeException(t.getMessage(), t); |
| 104 | 0 | } |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
public void iexec(String source, int lineNo, int columnNo, Object script) throws BSFException { |
| 108 | 0 | throw new IllegalArgumentException(script.toString()); |
| 109 | |
} |
| 110 | |
|
| 111 | |
public void undeclareBean(BSFDeclaredBean bean) throws BSFException { |
| 112 | 0 | super.undeclareBean(bean); |
| 113 | 0 | if (compiler != null) compiler.undeclareBean(bean); |
| 114 | 0 | } |
| 115 | |
|
| 116 | |
public void declareBean(BSFDeclaredBean bean) throws BSFException { |
| 117 | 0 | super.declareBean(bean); |
| 118 | 0 | if (compiler != null) compiler.declareBean(bean); |
| 119 | 0 | } |
| 120 | |
|
| 121 | |
protected void reset() { |
| 122 | 67 | super.reset(); |
| 123 | 67 | if (compiler != null) compiler.clear(); |
| 124 | 67 | } |
| 125 | |
|
| 126 | |
protected Object apply(Object body, V object, String[] names, Object[] args) throws Throwable { |
| 127 | 1465 | return apply(body, object, args); |
| 128 | |
} |
| 129 | |
|
| 130 | |
protected abstract Object apply(Object body, V compiled, Object[] args) throws Throwable; |
| 131 | |
protected abstract Object eval(Object expr, V compiled) throws Throwable; |
| 132 | |
protected abstract void exec(Object script, V compiled) throws Throwable; |
| 133 | |
} |