KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.opensymphony.workflow.util.beanshell;
6
7 import bsh.Interpreter;
8 import bsh.TargetError;
9
10 import com.opensymphony.module.propertyset.PropertySet;
11
12 import com.opensymphony.workflow.*;
13 import com.opensymphony.workflow.spi.WorkflowEntry;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import java.util.Map JavaDoc;
19
20
21 /**
22  * Beanshell inline script validator.
23  * The input is determined to be invalid of the script throws a {@link InvalidInputException}.
24  * @author $Author: hani $
25  * @version $Revision: 1.4 $
26  */

27 public class BeanShellValidator implements Validator {
28     //~ Static fields/initializers /////////////////////////////////////////////
29

30     private static final Log log = LogFactory.getLog(BeanShellValidator.class);
31
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     public void validate(Map JavaDoc transientVars, Map JavaDoc args, PropertySet ps) throws WorkflowException {
35         Interpreter i = new Interpreter();
36         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
37
38         try {
39             String JavaDoc contents = (String JavaDoc) args.get(AbstractWorkflow.BSH_SCRIPT);
40
41             WorkflowContext context = (WorkflowContext) transientVars.get("context");
42             WorkflowEntry entry = (WorkflowEntry) transientVars.get("entry");
43
44             if (loader != null) {
45                 i.setClassLoader(loader);
46             }
47
48             i.set("entry", entry);
49             i.set("context", context);
50             i.set("transientVars", transientVars);
51             i.set("propertySet", ps);
52
53             Object JavaDoc o = i.eval(contents);
54
55             if (o != null) {
56                 throw new InvalidInputException(o);
57             }
58         } catch (TargetError e) {
59             if (e.getTarget() instanceof WorkflowException) {
60                 throw (WorkflowException) e.getTarget();
61             } else {
62                 throw new WorkflowException("Unexpected exception in beanshell validator script:" + e.getMessage(), e);
63             }
64         } catch (Exception JavaDoc e) {
65             String JavaDoc message = "Error executing beanshell validator";
66             throw new WorkflowException(message, e);
67         } finally {
68             if (loader != null) {
69                 i.setClassLoader(null);
70             }
71         }
72     }
73 }
74
Popular Tags