01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util.beanshell;
06:
07: import bsh.Interpreter;
08: import bsh.TargetError;
09:
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;
19:
20: /**
21: * Beanshell inline script validator.
22: * The input is determined to be invalid of the script throws a {@link InvalidInputException}.
23: */
24: public class BeanShellValidator implements Validator {
25: //~ Static fields/initializers /////////////////////////////////////////////
26:
27: private static final Log log = LogFactory
28: .getLog(BeanShellValidator.class);
29:
30: //~ Methods ////////////////////////////////////////////////////////////////
31:
32: public void validate(Map transientVars, Map args, PropertySet ps)
33: throws WorkflowException {
34: Interpreter i = new Interpreter();
35: ClassLoader loader = Thread.currentThread()
36: .getContextClassLoader();
37:
38: try {
39: String contents = (String) args
40: .get(AbstractWorkflow.BSH_SCRIPT);
41:
42: WorkflowContext context = (WorkflowContext) transientVars
43: .get("context");
44: WorkflowEntry entry = (WorkflowEntry) transientVars
45: .get("entry");
46:
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:
56: Object o = i.eval(contents);
57:
58: if (o != null) {
59: throw new InvalidInputException(o);
60: }
61: } catch (TargetError e) {
62: if (e.getTarget() instanceof WorkflowException) {
63: throw (WorkflowException) e.getTarget();
64: } else {
65: throw new WorkflowException(
66: "Unexpected exception in beanshell validator script:"
67: + e.getMessage(), e);
68: }
69: } catch (Exception e) {
70: String message = "Error executing beanshell validator";
71: throw new WorkflowException(message, e);
72: } finally {
73: if (loader != null) {
74: i.setClassLoader(null);
75: }
76: }
77: }
78: }
|