1 24 package org.ofbiz.minilang; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.minilang.method.MethodContext; 33 34 import com.ibm.bsf.BSFDeclaredBean; 35 import com.ibm.bsf.BSFException; 36 import com.ibm.bsf.BSFManager; 37 import com.ibm.bsf.util.BSFEngineImpl; 38 39 48 public class SimpleMethodBsfEngine extends BSFEngineImpl { 49 50 public static final String module = SimpleMethodBsfEngine.class.getName(); 51 52 protected Map context = new HashMap (); 53 54 public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { 55 super.initialize(mgr, lang, declaredBeans); 56 57 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 58 59 context.put("bsf", mgr); 61 62 for(int i=0; i<declaredBeans.size(); i++) { 63 BSFDeclaredBean bean = (BSFDeclaredBean)declaredBeans.get(i); 64 declareBean(bean); 65 } 66 } 67 68 public void setDebug(boolean debug) { 69 } 71 72 78 public Object call(Object object, String name, Object [] args) throws BSFException { 79 throw new BSFException("The call method is not yet supported for SimpleMethods"); 80 } 81 82 83 88 public Object apply(String source, int lineNo, int columnNo, Object funcBody, Vector namesVec, Vector argsVec) throws BSFException { 89 92 throw new BSFException("The apply method is not yet supported for simple-methods"); 93 } 94 95 public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException { 96 if (!(expr instanceof String )) throw new BSFException("simple-method expression must be a string"); 97 98 Map simpleMethods = null; 100 try { 101 simpleMethods = SimpleMethod.getDirectSimpleMethods(source, (String ) expr, "<bsf source>"); 102 } catch (MiniLangException e) { 103 throw new BSFException("Error loading/parsing simple-method XML source: " + e.getMessage()); 104 } 105 Set smNames = simpleMethods.keySet(); 106 if (smNames.size() == 0) throw new BSFException("Did not find any simple-methods in the file"); 107 108 String methodName = (String ) smNames.iterator().next(); 109 if (smNames.size() > 1) Debug.logWarning("Found more than one simple-method in the file, running the [" + methodName + "] method, you should remove all but one method from this file", module); 110 111 SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName); 112 MethodContext methodContext = new MethodContext(context, null, MethodContext.EVENT); 113 return simpleMethod.exec(methodContext); 114 } 116 117 118 public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { 119 eval(source, lineNo, columnNo, script); 120 } 121 122 123 134 135 public void declareBean(BSFDeclaredBean bean) throws BSFException { 136 context.put(bean.name, bean.bean); 137 } 138 139 public void undeclareBean(BSFDeclaredBean bean) throws BSFException { 140 context.remove(bean.name); 141 } 142 143 public void terminate() { } 144 145 private String sourceInfo(String source, int lineNo, int columnNo) { 146 return "SimpleMethod: " + source + " at line: " + lineNo +" column: " + columnNo; 147 } 148 } 149 | Popular Tags |