01: /*
02: *
03: * BeanShellHook.java -
04: * Copyright (C) 2003 Ecoo Team
05: * valdes@loria.fr
06: *
07: *
08: * This program is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU Lesser General Public License
10: * as published by the Free Software Foundation; either version 2
11: * of the License, or (at your option) any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU Lesser General Public License for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public License
19: * along with this program; if not, write to the Free Software
20: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21: */
22:
23: package hero.hook;
24:
25: import hero.interfaces.BnNodeInterHookLocal;
26: import hero.interfaces.BnNodeLocal;
27: import hero.interfaces.BnNodePropertyLocal;
28: import hero.interfaces.BnProjectPropertyLocal;
29: import hero.interfaces.BnProjectInterHookLocal;
30: import hero.interfaces.BnProjectLocal;
31: import hero.util.HeroHookException;
32:
33: import java.util.Collection;
34: import java.util.Iterator;
35:
36: import bsh.Interpreter;
37:
38: public class InteractiveBSHook extends Hook {
39:
40: public InteractiveBSHook(String name, String event, int type,
41: String script) {
42: super (name, event, type, script);
43: }
44:
45: public void execute(Object bean, String eventName, BnNodeLocal node)
46: throws HeroHookException {
47: try {
48: String value = this .getScript();
49: BnNodeLocal nd = node;
50:
51: Interpreter i = new Interpreter();
52: i.set("node", node);
53: i.set("bean", bean);
54:
55: // BnNode properties mapping
56:
57: Collection prop = node.getBnProperties();
58: for (Iterator props = prop.iterator(); props.hasNext();) {
59: BnNodePropertyLocal pl = (BnNodePropertyLocal) props
60: .next();
61: i.set(pl.getTheKey(), pl.getTheValue());
62: }
63:
64: // Project properties mapping
65: BnProjectLocal project = node.getBnProject();
66: Collection projectProp = project.getBnProperties();
67: for (Iterator props = projectProp.iterator(); props
68: .hasNext();) {
69: BnProjectPropertyLocal pP = (BnProjectPropertyLocal) props
70: .next();
71: i.set(pP.getTheKey(), pP.getTheValue());
72: }
73:
74: i.eval(value);
75: i.eval(eventName + "(bean,node)");
76:
77: } catch (Exception t) {
78: t.printStackTrace();
79: if (node.getType() == hero.interfaces.Constants.Nd.SUB_PROCESS_NODE
80: && eventName
81: .equals(hero.interfaces.Constants.Nd.BEFORETERMINATE))
82: throw new HeroHookException(
83: "You have to terminate subProcess activities previuosly");
84: else if (eventName
85: .equals(hero.interfaces.Constants.Nd.AFTERSTART)
86: || eventName
87: .equals(hero.interfaces.Constants.Nd.BEFORETERMINATE)
88: || eventName
89: .equals(hero.interfaces.Constants.Nd.ANTICIPATE)
90: || eventName
91: .equals(hero.interfaces.Constants.Nd.ONCANCEL))
92:
93: throw new HeroHookException(
94: "Cannot execute beanshell hook "
95: + t.getMessage());
96: }
97: }
98: }
|