KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.soto.state.code;
2
3 import groovy.lang.GroovyClassLoader;
4
5 import org.apache.commons.lang.ClassUtils;
6 import org.apache.commons.lang.math.JVMRandom;
7
8 import org.sapia.soto.state.Result;
9 import org.sapia.soto.state.Step;
10
11 import org.sapia.util.xml.confix.ConfigurationException;
12 import org.sapia.util.xml.confix.ObjectCreationCallback;
13
14 import java.io.ByteArrayInputStream 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 GroovyStep implements Step, ObjectCreationCallback {
26   private String JavaDoc _name;
27   private String JavaDoc _src;
28   private Step _inner;
29   private StringBuffer JavaDoc _imports = new StringBuffer JavaDoc();
30
31   public GroovyStep() {
32   }
33
34   public void setName(String JavaDoc name) {
35     _name = name;
36   }
37
38   public void addImport(String JavaDoc imp) {
39     _imports.append("import ").append(imp).append(';');
40   }
41
42   public void setImport(String JavaDoc imp) {
43     addImport(imp);
44   }
45
46   /**
47    * @see org.sapia.soto.state.Step#getName()
48    */

49   public String JavaDoc getName() {
50     if (_name == null) {
51       return ClassUtils.getShortClassName(getClass());
52     }
53
54     return _name;
55   }
56
57   /**
58    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
59    */

60   public Object JavaDoc onCreate() throws ConfigurationException {
61     try {
62       Class JavaDoc clazz = generate();
63       _inner = (Step) clazz.newInstance();
64
65       return this;
66     } catch (RuntimeException JavaDoc e) {
67       throw e;
68     } catch (Exception JavaDoc e) {
69       throw new ConfigurationException("Could not generate Groovy code for: " +
70         _src, e);
71     }
72   }
73
74   public void setText(String JavaDoc src) throws Exception JavaDoc {
75     _src = src;
76
77     // try{
78
// }catch(CompilationFailedException e){
79
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
80
// PrintStream ps = new PrintStream(bos);
81
// e.printErrors(ps);
82
// ps.flush();
83
// ps.close();
84
// throw new CompilationFailedException(bos.toString());
85
// }
86
}
87
88   /**
89    * @see org.sapia.soto.state.Executable#execute(Result)
90    */

91   public void execute(Result st) {
92     if (_inner == null) {
93       throw new IllegalStateException JavaDoc("Source code not defined");
94     }
95
96     _inner.execute(st);
97   }
98
99   private Class JavaDoc generate() throws Exception JavaDoc {
100     if (_src == null) {
101       throw new IllegalStateException JavaDoc("Groovy source not specified");
102     }
103
104     String JavaDoc src = _src;
105
106     src = _imports.toString() + "class Soto_State_Java_" + getName() + "_" +
107       new JVMRandom().nextLong() + " implements org.sapia.soto.state.Step{" +
108       " public String getName(){ return \"" + getName() + "\" }" +
109       " public void execute(" + Result.class.getName() + " result){" + src +
110       " }" + "}";
111
112     // System.out.println(src);
113
ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(src.getBytes());
114     GroovyClassLoader loader = new GroovyClassLoader(Thread.currentThread()
115                                                               .getContextClassLoader());
116
117     return loader.parseClass(bis, getName());
118   }
119 }
120
Popular Tags