1 package org.enhydra.shark.scripting; 2 3 import org.enhydra.shark.api.RootException; 4 import org.enhydra.shark.api.SharkTransaction; 5 import org.enhydra.shark.api.internal.scripting.Evaluator; 6 import org.enhydra.shark.api.internal.working.CallbackUtilities; 7 8 import java.util.*; 9 10 11 import bsh.*; 12 13 17 public class BshEvaluator implements Evaluator { 18 19 private static final String LOG_CHANNEL="Scripting"; 20 21 private CallbackUtilities cus; 22 public void configure (CallbackUtilities cus) throws RootException { 23 this.cus=cus; 24 } 25 26 27 35 public boolean evaluateCondition (SharkTransaction t,String condition,Map context) throws RootException { 36 if(condition==null || condition.trim().length()==0){ 37 return true; 38 } 39 40 java.lang.Object eval=evaluateExpression(t,condition,context,java.lang.Boolean .class); 41 42 try { 43 return ((Boolean )eval).booleanValue(); 44 } catch (Exception ex) { 45 cus.error(LOG_CHANNEL,"BshEvaluator -> The result of condition "+condition+" cannot be converted to boolean"); 46 cus.error("BshEvaluator -> The result of condition "+condition+" cannot be converted to boolean"); 47 throw new RootException("Result cannot be converted to boolean",ex); 48 } 49 } 50 51 60 public java.lang.Object evaluateExpression (SharkTransaction t,String expr,Map context,Class resultClass) throws RootException { 61 Interpreter interpreter = new Interpreter(); 62 63 java.lang.Object eval; 64 try { 65 prepareContext(interpreter,context); 66 eval = interpreter.eval(expr); 68 if (eval instanceof Integer ) { 69 eval=new Long (((Integer )eval).intValue()); 70 } 71 cus.debug(LOG_CHANNEL,"BshScriptEvaluator -> Java expression "+expr+" is evaluated to "+eval); 72 73 75 return eval; 76 } catch (Throwable ee) { 77 cus.error(LOG_CHANNEL,"BshEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+ee.getMessage()); 78 cus.error("BshEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+ee.getMessage()); 79 throw new RootException("Result cannot be evaluated",ee); 80 } 81 } 82 83 private void prepareContext (Interpreter interpreter,Map context) throws Exception { 84 Iterator iter = context.entrySet().iterator(); 85 while(iter.hasNext()){ 86 Map.Entry me=(Map.Entry)iter.next(); 87 String key = me.getKey().toString(); 88 java.lang.Object value = me.getValue(); 89 interpreter.set(key,value); 91 } 92 } 93 94 } 95 | Popular Tags |