KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > hook > InteractiveBSHook


1 /*
2 *
3 * BeanShellHook.java -
4 * Copyright (C) 2003 Ecoo Team
5 * valdes@loria.fr
6 *
7 *
8 * This program is free software; you can redistribute it and/or
9 * 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 JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 import bsh.Interpreter;
37
38 public class InteractiveBSHook extends Hook {
39
40     public InteractiveBSHook(String JavaDoc name, String JavaDoc event, int type, String JavaDoc script) {
41     super(name,event,type,script);
42     }
43
44     public void execute(Object JavaDoc bean,String JavaDoc eventName,BnNodeLocal node) throws HeroHookException {
45     try {
46         String JavaDoc value = this.getScript();
47         BnNodeLocal nd=node;
48
49         Interpreter i = new Interpreter();
50         i.set("node",node);
51         i.set("bean",bean);
52
53         // BnNode properties mapping
54

55         Collection JavaDoc prop = node.getBnProperties();
56         for (Iterator JavaDoc props=prop.iterator();props.hasNext();) {
57             BnNodePropertyLocal pl=(BnNodePropertyLocal)props.next();
58             i.set(pl.getTheKey(),pl.getTheValue());
59         }
60         
61         // Project properties mapping
62
BnProjectLocal project = node.getBnProject();
63         Collection JavaDoc projectProp = project.getBnProperties();
64         for (Iterator JavaDoc props=projectProp.iterator();props.hasNext();) {
65              BnProjectPropertyLocal pP=(BnProjectPropertyLocal)props.next();
66              if (!pP.getTheKey().matches("iterate_.*"))
67                    i.set(pP.getTheKey(),pP.getTheValue());
68         }
69
70         i.eval(value);
71         i.eval(eventName+"(bean,node)");
72         
73     } catch (Exception JavaDoc t) {t.printStackTrace();
74         if (node.getType()==hero.interfaces.Constants.Nd.SUB_PROCESS_NODE && eventName.equals(hero.interfaces.Constants.Nd.BEFORETERMINATE))
75             throw new HeroHookException("You have to terminate subProcess activities previuosly");
76     else if (eventName.equals(hero.interfaces.Constants.Nd.AFTERSTART) || eventName.equals(hero.interfaces.Constants.Nd.BEFORETERMINATE)
77         || eventName.equals(hero.interfaces.Constants.Nd.ANTICIPATE) || eventName.equals(hero.interfaces.Constants.Nd.ONCANCEL))
78         
79         throw new HeroHookException("Cannot execute beanshell hook "+t.getMessage());
80         }
81     }
82 }
83    
Popular Tags