01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util.bsf;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.util.TextUtils;
10:
11: import com.opensymphony.workflow.AbstractWorkflow;
12: import com.opensymphony.workflow.InvalidInputException;
13: import com.opensymphony.workflow.Validator;
14: import com.opensymphony.workflow.WorkflowContext;
15: import com.opensymphony.workflow.WorkflowException;
16: import com.opensymphony.workflow.spi.WorkflowEntry;
17:
18: import org.apache.bsf.BSFEngine;
19: import org.apache.bsf.BSFManager;
20:
21: import java.util.Map;
22:
23: /**
24: * Validates a step using a BSF script.
25: *
26: * @author $Author: hani $
27: * @version $Revision: 1.5 $
28: */
29: public class BSFValidator implements Validator {
30: //~ Methods ////////////////////////////////////////////////////////////////
31:
32: public void validate(Map transientVars, Map args, PropertySet ps)
33: throws WorkflowException {
34: String language = (String) args
35: .get(AbstractWorkflow.BSF_LANGUAGE);
36: String source = (String) args.get(AbstractWorkflow.BSF_SOURCE);
37: int row = TextUtils.parseInt((String) args
38: .get(AbstractWorkflow.BSF_ROW));
39: int col = TextUtils.parseInt((String) args
40: .get(AbstractWorkflow.BSF_COL));
41: String script = (String) args.get(AbstractWorkflow.BSF_SCRIPT);
42:
43: WorkflowContext context = (WorkflowContext) transientVars
44: .get("context");
45: WorkflowEntry entry = (WorkflowEntry) transientVars
46: .get("entry");
47:
48: BSFManager mgr = new BSFManager();
49: ClassLoader loader = Thread.currentThread()
50: .getContextClassLoader();
51:
52: if (loader != null) {
53: mgr.setClassLoader(loader);
54: }
55:
56: mgr.registerBean("entry", entry);
57: mgr.registerBean("context", context);
58: mgr.registerBean("transientVars", transientVars);
59: mgr.registerBean("propertySet", ps);
60:
61: try {
62: BSFEngine engine = mgr.loadScriptingEngine(language);
63: Object o = engine.eval(source, row, col, script);
64:
65: if (o != null) {
66: throw new InvalidInputException(o);
67: }
68: } catch (Exception e) {
69: String message = "Could not execute BSF validator";
70: throw new WorkflowException(message, e);
71: }
72: }
73: }
|