KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.soto.state.code;
2
3 import instantj.compile.CompilationFailedException;
4 import instantj.compile.Compiler;
5 import instantj.compile.Source;
6
7 import org.apache.commons.lang.ClassUtils;
8 import org.apache.commons.lang.math.JVMRandom;
9
10 import org.sapia.soto.state.Result;
11 import org.sapia.soto.state.Step;
12
13 import java.io.ByteArrayOutputStream JavaDoc;
14 import java.io.PrintStream JavaDoc;
15
16
17 /**
18  * @author Yanick Duchesne
19  * <dl>
20  * <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>
21  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
22  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
23  * </dl>
24  */

25 public class JavaStep implements Step {
26   static {
27     System.setProperty("instantj.compile.compiler",
28       "instantj.compile.pizza.PizzaSourceCompiler");
29   }
30
31   private String JavaDoc _name;
32   private Step _inner;
33
34   /**
35    *
36    */

37   public JavaStep() {
38   }
39
40   public void setName(String JavaDoc name) {
41     _name = name;
42   }
43
44   /**
45    * @see org.sapia.soto.state.Step#getName()
46    */

47   public String JavaDoc getName() {
48     if (_name == null) {
49       return ClassUtils.getShortClassName(getClass());
50     }
51
52     return _name;
53   }
54
55   public void setText(String JavaDoc src) throws Exception JavaDoc {
56     try {
57       Class JavaDoc clazz = generate(src);
58       _inner = (Step) clazz.newInstance();
59     } catch (CompilationFailedException e) {
60       ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
61       PrintStream JavaDoc ps = new PrintStream JavaDoc(bos);
62       e.printErrors(ps);
63       ps.flush();
64       ps.close();
65       throw new CompilationFailedException(bos.toString());
66     }
67   }
68
69   /**
70    * @see org.sapia.soto.state.Executable#execute(Result)
71    */

72   public void execute(Result st) {
73     if (_inner == null) {
74       throw new IllegalStateException JavaDoc("Source code not defined");
75     }
76
77     _inner.execute(st);
78   }
79
80   private Class JavaDoc generate(String JavaDoc src) throws Exception JavaDoc {
81     src = "public class Soto_State_Java_" + new JVMRandom().nextLong() +
82       " implements org.sapia.soto.state.Step{" +
83       " public String getName(){ return \"" + getName() + "\"; }" +
84       " public void execute(org.sapia.soto.state.Result result){" + src +
85       " }" + "}";
86
87     return Compiler.compile(new Source(src), true).getType();
88   }
89
90   //
91
// public static void main(String[] args) {
92
// try {
93
// JavaStep step = new JavaStep();
94
// step.setText("System.out.println(state);");
95
// } catch (Exception e) {
96
// e.printStackTrace();
97
// }
98
// }
99
}
100
Popular Tags