KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > state > code > BeanshellStep


1 package org.sapia.soto.state.code;
2
3 import bsh.EvalError;
4 import bsh.Interpreter;
5 import bsh.TargetError;
6
7 import org.apache.commons.lang.ClassUtils;
8
9 import org.sapia.soto.state.Result;
10 import org.sapia.soto.state.Step;
11
12 import java.io.StringReader JavaDoc;
13
14
15 /**
16  * @author Yanick Duchesne
17  * <dl>
18  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class BeanshellStep implements Step {
24   private StringReader JavaDoc _src;
25
26   public BeanshellStep() {
27     super();
28   }
29
30   /**
31    * @see org.sapia.soto.state.Step#getName()
32    */

33   public String JavaDoc getName() {
34     return ClassUtils.getShortClassName(getClass());
35   }
36
37   /**
38    * @see org.sapia.soto.state.Executable#execute(org.sapia.soto.state.Result)
39    */

40   public void execute(Result result) {
41     if (_src == null) {
42       throw new IllegalStateException JavaDoc("Beanshell source not specified");
43     }
44
45     Interpreter interp = new Interpreter();
46
47     try {
48       interp.set("result", result);
49     } catch (EvalError e) {
50       result.error("Could not initialize interpreter", e);
51     }
52
53     try {
54       interp.eval(_src);
55     } catch (TargetError e) {
56       result.error(e.getTarget());
57     } catch (EvalError e) {
58       result.error("Could not interpret Beanshell script", e);
59     }
60   }
61
62   public void setText(String JavaDoc src) {
63     _src = new StringReader JavaDoc(src);
64   }
65 }
66
Popular Tags