1 24 package org.ofbiz.base.util; 25 26 35 36 import java.io.StringReader ; 37 import java.util.Vector ; 38 39 import bsh.EvalError; 40 import bsh.Interpreter; 41 import bsh.InterpreterError; 42 import bsh.TargetError; 43 44 import com.ibm.bsf.BSFDeclaredBean; 45 import com.ibm.bsf.BSFException; 46 import com.ibm.bsf.BSFManager; 47 import com.ibm.bsf.util.BSFEngineImpl; 48 49 import org.ofbiz.base.util.cache.UtilCache; 50 51 64 public class OfbizBshBsfEngine extends BSFEngineImpl { 65 66 public static final String module = OfbizBshBsfEngine.class.getName(); 67 68 protected Interpreter interpreter; 69 protected boolean installedApplyMethod; 70 71 public static UtilCache parsedScripts = new UtilCache("script.BshBsfParsedCache", 0, 0, false); 72 73 public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { 74 super.initialize(mgr, lang, declaredBeans); 75 76 interpreter = BshUtil.getMasterInterpreter(null); 77 78 try { 80 interpreter.set("bsf", mgr); 81 } catch (EvalError e) { 82 throw new BSFException("bsh internal error: "+e.toString()); 83 } 84 85 for(int i=0; i<declaredBeans.size(); i++) { 86 BSFDeclaredBean bean = (BSFDeclaredBean)declaredBeans.get(i); 87 declareBean(bean); 88 } 89 } 90 91 public void setDebug(boolean debug) { 92 Interpreter.DEBUG=debug; 93 } 94 95 101 public Object call(Object object, String name, Object [] args) throws BSFException { 102 if (object == null) { 103 try { 104 object = interpreter.get("global"); 105 } catch (EvalError e) { 106 throw new BSFException("bsh internal error: "+e.toString()); 107 } 108 } 109 110 if (object instanceof bsh.This) { 111 try { 112 return ((bsh.This)object).invokeMethod(name, args); 113 } catch (InterpreterError e) { 114 throw new BSFException("BeanShell interpreter internal error: "+e); 115 } catch (TargetError e2) { 116 throw new BSFException("The application script threw an exception: " + e2.getTarget()); 117 } catch (EvalError e3) { 118 throw new BSFException("BeanShell script error: "+e3); 119 } 120 } else { 121 throw new BSFException("Cannot invoke method: " + name + 122 ". Object: "+object +" is not a BeanShell scripted object."); 123 } 124 } 125 126 127 133 final static String bsfApplyMethod = 134 "_bsfApply(_bsfNames, _bsfArgs, _bsfText) {" 135 +"for(i=0;i<_bsfNames.length;i++)" 136 +"this.namespace.setVariable(_bsfNames[i], _bsfArgs[i]);" 137 +"return this.interpreter.eval(_bsfText, this.namespace);" 138 +"}"; 139 140 145 public Object apply(String source, int lineNo, int columnNo, Object funcBody, Vector namesVec, Vector argsVec) throws BSFException { 146 if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch"); 147 if (!(funcBody instanceof String )) throw new BSFException("apply: function body must be a string"); 148 149 String [] names = new String [ namesVec.size() ]; 150 namesVec.copyInto(names); 151 Object [] args = new String [ argsVec.size() ]; 152 argsVec.copyInto(args); 153 154 try { 155 if (!installedApplyMethod) { 156 interpreter.eval(bsfApplyMethod); 157 installedApplyMethod = true; 158 } 159 160 bsh.This global = (bsh.This)interpreter.get("global"); 161 return global.invokeMethod("_bsfApply", new Object [] { names, args, (String )funcBody }); 162 163 } catch (InterpreterError e) { 164 throw new BSFException("BeanShell interpreter internal error: " + e + sourceInfo(source,lineNo,columnNo)); 165 } catch (TargetError e2) { 166 throw new BSFException("The application script threw an exception: " + e2.getTarget() + sourceInfo(source,lineNo,columnNo)); 167 } catch (EvalError e3) { 168 throw new BSFException("BeanShell script error: " + e3 + sourceInfo(source,lineNo,columnNo)); 169 } 170 } 171 172 public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException { 173 if (!(expr instanceof String )) throw new BSFException("BeanShell expression must be a string"); 174 175 try { 176 178 Interpreter.ParsedScript script = null; 179 180 if (source != null && source.length() > 0) { 181 script = (Interpreter.ParsedScript) parsedScripts.get(source); 182 if (script == null) { 183 synchronized (OfbizBshBsfEngine.class) { 184 script = (Interpreter.ParsedScript) parsedScripts.get(source); 185 if (script == null) { 186 script = interpreter.parseScript(source, new StringReader ((String ) expr)); 187 Debug.logVerbose("Caching BSH script at: " + source, module); 188 parsedScripts.put(source, script); 189 } 190 } 191 } 192 } else { 193 script = interpreter.parseScript(source, new StringReader ((String ) expr)); 194 } 195 196 return interpreter.evalParsedScript(script); 197 } catch (InterpreterError e) { 198 throw new BSFException("BeanShell interpreter internal error: "+ e + sourceInfo(source,lineNo,columnNo)); 199 } catch (TargetError e2) { 200 Debug.logError(e2, "Error thrown in BeanShell script called through BSF at: " + sourceInfo(source,lineNo,columnNo), module); 201 throw new BSFException("The application script threw an exception: " + e2 + " " + sourceInfo(source,lineNo,columnNo)); 203 } catch (EvalError e3) { 204 throw new BSFException("BeanShell script error: " + e3 + sourceInfo(source,lineNo,columnNo)); 205 } 206 } 207 208 209 public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { 210 eval(source, lineNo, columnNo, script); 211 } 212 213 214 225 226 public void declareBean(BSFDeclaredBean bean) throws BSFException { 227 try { 228 interpreter.set(bean.name, bean.bean); 229 } catch (EvalError e) { 230 throw new BSFException("error declaring bean: " + bean.name + " : " + e.toString()); 231 } 232 } 233 234 public void undeclareBean(BSFDeclaredBean bean) throws BSFException { 235 try { 236 interpreter.unset(bean.name); 237 } catch (EvalError e) { 238 throw new BSFException("bsh internal error: " + e.toString()); 239 } 240 } 241 242 public void terminate() { } 243 244 private String sourceInfo(String source, int lineNo, int columnNo) { 245 return "BSF info: " + source + " at line: " + lineNo +" column: " + columnNo; 246 } 247 } 248 | Popular Tags |