KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > scripting > BshEvaluator


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 /**
14  * Implementation of the evaluator interface which evaluates the condition body
15  * as a java expression.
16  */

17 public class BshEvaluator implements Evaluator {
18
19    private static final String JavaDoc LOG_CHANNEL="Scripting";
20
21    private CallbackUtilities cus;
22    public void configure (CallbackUtilities cus) throws RootException {
23       this.cus=cus;
24    }
25
26
27    /**
28     * Evaluate the condition using java as the expression language. This
29     * method returns true if the condition is satisfied.
30     * @param t a SharkTransaction
31     * @param condition The condition
32     * @param context The context
33     * @return True if the condition is true
34     */

35    public boolean evaluateCondition (SharkTransaction t,String JavaDoc condition,Map context) throws RootException {
36       if(condition==null || condition.trim().length()==0){
37          return true;
38       }
39
40       java.lang.Object JavaDoc eval=evaluateExpression(t,condition,context,java.lang.Boolean JavaDoc.class);
41
42       try {
43          return ((Boolean JavaDoc)eval).booleanValue();
44       } catch (Exception JavaDoc 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    /**
52     * Evaluates the given expression.
53     *
54     * @param t a SharkTransaction
55     * @param expr The expression String
56     * @param context The workflow context
57     * @param resultClass Returned object should be the instance of this Java class
58     * @return The result of the expression evaluation
59     */

60    public java.lang.Object JavaDoc evaluateExpression (SharkTransaction t,String JavaDoc expr,Map context,Class JavaDoc resultClass) throws RootException {
61       Interpreter interpreter = new Interpreter();
62
63       java.lang.Object JavaDoc eval;
64       try {
65          prepareContext(interpreter,context);
66          //System.err.println("Evaluating java condition:" + expr);
67
eval = interpreter.eval(expr);
68          if (eval instanceof Integer JavaDoc) {
69             eval=new Long JavaDoc(((Integer JavaDoc)eval).intValue());
70          }
71          cus.debug(LOG_CHANNEL,"BshScriptEvaluator -> Java expression "+expr+" is evaluated to "+eval);
72
73          //System.err.println("Evaluated to -- " + eval);
74

75          return eval;
76       } catch (Throwable JavaDoc 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 JavaDoc {
84       Iterator iter = context.entrySet().iterator();
85       while(iter.hasNext()){
86          Map.Entry me=(Map.Entry)iter.next();
87          String JavaDoc key = me.getKey().toString();
88          java.lang.Object JavaDoc value = me.getValue();
89          //System.err.println("Value for key "+key+" is "+value);
90
interpreter.set(key,value);
91       }
92    }
93
94 }
95
Popular Tags