KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > beanshell > BeanShellCondition


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.util.beanshell;
6
7 import bsh.EvalError;
8 import bsh.Interpreter;
9 import bsh.TargetError;
10
11 import com.opensymphony.module.propertyset.PropertySet;
12
13 import com.opensymphony.util.TextUtils;
14
15 import com.opensymphony.workflow.*;
16 import com.opensymphony.workflow.spi.WorkflowEntry;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import java.util.Map JavaDoc;
22
23
24 /**
25  *
26  *
27  * @author $Author: hani $
28  * @version $Revision: 1.3 $
29  */

30 public class BeanShellCondition implements Condition {
31     //~ Static fields/initializers /////////////////////////////////////////////
32

33     private static final Log log = LogFactory.getLog(BeanShellCondition.class);
34
35     //~ Methods ////////////////////////////////////////////////////////////////
36

37     public boolean passesCondition(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) throws WorkflowException {
38         String JavaDoc script = (String JavaDoc) args.get(AbstractWorkflow.BSH_SCRIPT);
39
40         WorkflowContext context = (WorkflowContext) transientVars.get("context");
41         WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry");
42
43         Interpreter i = new Interpreter();
44         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
45
46         try {
47             if (loader != null) {
48                 i.setClassLoader(loader);
49             }
50
51             i.set("entry", entry);
52             i.set("context", context);
53             i.set("transientVars", transientVars);
54             i.set("propertySet", ps);
55             i.set("jn", transientVars.get("jn"));
56
57             Object JavaDoc o = i.eval(script);
58
59             if (o == null) {
60                 return false;
61             } else {
62                 return TextUtils.parseBoolean(o.toString());
63             }
64         } catch (TargetError targetError) {
65             if (targetError.getTarget() instanceof WorkflowException) {
66                 throw (WorkflowException) targetError.getTarget();
67             } else {
68                 String JavaDoc message = "Could not execute BeanShell script";
69                 throw new WorkflowException(message, targetError.getTarget());
70             }
71         } catch (EvalError e) {
72             String JavaDoc message = "Could not execute BeanShell script";
73             log.error(message, e);
74             throw new WorkflowException(message, e);
75         } finally {
76             if (loader != null) {
77                 i.setClassLoader(null);
78             }
79         }
80     }
81 }
82
Popular Tags