KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.mozilla.javascript.*;
11
12
13 /**
14  * Implementation of the Evaluator interface which evaluates the condition body
15  * as a java script expression.
16  */

17 public class JavaScriptEvaluator 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 script 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       try {
42          return ((Boolean JavaDoc)eval).booleanValue();
43       } catch (Exception JavaDoc ex) {
44          cus.error(LOG_CHANNEL,"JavaScriptEvaluator -> The result of condition "+condition+" cannot be converted to boolean");
45          cus.error("JavaScriptEvaluator -> The result of condition "+condition+" cannot be converted to boolean");
46          throw new RootException("Result cannot be converted to boolean",ex);
47       }
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 expression evaluation.
59     */

60    public java.lang.Object JavaDoc evaluateExpression (SharkTransaction t,String JavaDoc expr,Map context,Class JavaDoc resultClass) throws RootException {
61       org.mozilla.javascript.Context cx=org.mozilla.javascript.Context.enter();
62       Scriptable scope=cx.initStandardObjects(null);
63
64       java.lang.Object JavaDoc eval;
65       try {
66          prepareContext(scope,context);
67          //System.err.println("Evaluating javascript expression:" + expr);
68
if (resultClass!=null) {
69             eval = org.mozilla.javascript.Context.toType(cx.evaluateString(scope,expr,"",1,null),resultClass);
70          } else {
71             eval = cx.evaluateString(scope,expr,"",1,null);
72          }
73          cus.debug(LOG_CHANNEL,"JavaScriptEvaluator -> Javascript expression "+expr+" is evaluated to "+eval);
74          //System.err.println("Evaluated to -- " + eval);
75
return eval;
76
77       } catch (Exception JavaDoc jse) {
78          cus.error(LOG_CHANNEL,"JavaScriptEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+jse.getMessage());
79          cus.error("JavaScriptEvaluator -> The result of expression "+expr+" can't be evaluated - error message="+jse.getMessage());
80          throw new RootException("Result cannot be evaluated",jse);
81       } finally {
82          org.mozilla.javascript.Context.exit();
83       }
84    }
85
86    private void prepareContext (Scriptable scope,Map context) throws Exception JavaDoc {
87       Iterator iter = context.entrySet().iterator();
88       while(iter.hasNext()){
89          Map.Entry me=(Map.Entry)iter.next();
90          String JavaDoc key = me.getKey().toString();
91          java.lang.Object JavaDoc value = me.getValue();
92          //System.err.println("Value for key "+key+" is "+value+", class is "+value.getClass().getName());
93
scope.put(key,scope,value);
94       }
95    }
96
97 }
98
Popular Tags