1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.jobscheduler.impl.groovy;
12
13 import org.astrogrid.jes.util.TemporaryBuffer;
14 import org.astrogrid.workflow.beans.v1.Workflow;
15 import org.astrogrid.workflow.beans.v1.execution.Extension;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.io.Writer;
23 import java.util.Map;
24
25 /*** Factory object that takes care of creating fresh and unpickling existing groovy interpreters.
26 * @see GroovyInterpreter
27 * @author Noel Winstanley nw@jb.man.ac.uk 14-May-2004
28 */
29 public class GroovyInterpreterFactory {
30 /***
31 * interface to a component that actually performs the serialization / deserialization in some way.
32 * @author Noel Winstanley nw@jb.man.ac.uk 27-Jul-2004
33 *
34 */
35 public interface Pickler {
36
37 /*** serialize / pickle an intepreter (i.e. all hte state of an executing workflow) to a writer
38 * @param out
39 * @param interp
40 * @throws Exception
41 */
42 void marshallInterpreter(Writer out, GroovyInterpreter interp) throws Exception;
43 /*** unmarshal a previously pickled interpreter.
44 * @param in
45 * @return
46 * @throws Exception
47 */
48 GroovyInterpreter unmarshallInterpreter(Reader in) throws Exception;
49 /*** unmarshal a previously pickled rulestore (subcomponent of an intepreter).
50 * @param reader
51 * @return
52 * @throws Exception
53 */
54 Map unmarshallRuleStore(Reader reader) throws Exception;
55 }
56 /*** construct a new factory, passing in the pickler implementatio to use */
57 public GroovyInterpreterFactory(Pickler p, TemporaryBuffer buff) {
58 this.pickler = p;
59 this.buff = buff;
60 }
61 protected final Pickler pickler;
62 /*** @todo maybe better to soft-reference wrap this */
63 protected final TemporaryBuffer buff;
64
65 /*** so our buffer can be shared amongst friends.. */
66 public TemporaryBuffer getBuffer() {
67 return buff;
68 }
69
70 private static final Log log = LogFactory.getLog(GroovyInterpreterFactory.class);
71 /*** key used in workflow extension to store pickled workflow.
72 */
73 public static final String EXTENSION_KEY = "pickled.groovy.interpreter";
74 static final String EXTENSION_XPATH = "jobExecutionRecord/extension[key='" + EXTENSION_KEY+ "']";
75
76 /*** write a groovy interpreter back into the extension fragment of a workflow */
77 public void pickleTo(GroovyInterpreter interp,Workflow wf) throws PickleException{
78
79 Extension pickleJar = (Extension)wf.findXPathValue(GroovyInterpreterFactory.EXTENSION_XPATH);
80 if (pickleJar == null) {
81 pickleJar = new Extension();
82 pickleJar.setKey(GroovyInterpreterFactory.EXTENSION_KEY);
83
84 wf.getJobExecutionRecord().addExtension(pickleJar);
85 }
86
87 buff.writeMode();
88 Writer writer = buff.getWriter();
89 try {
90 pickler.marshallInterpreter(writer,interp);
91 writer.close();
92 buff.readMode();
93 pickleJar.setContent(buff.getContents());
94 }catch (Exception e) {
95 throw new PickleException("Could not pickle interpreter",e);
96 }
97 }
98
99 /***
100 * deserialize previously created workflow interpreter. */
101 public GroovyInterpreter unpickleFrom(JesInterface env) throws PickleException {
102 Extension pickleJar = (Extension)env.getWorkflow().findXPathValue(GroovyInterpreterFactory.EXTENSION_XPATH);
103 StringReader sr = new StringReader(pickleJar.getContent());
104 try {
105 GroovyInterpreter gi = pickler.unmarshallInterpreter(sr);
106 gi.setJesInterface(env);
107 return gi;
108 } catch (Exception e) {
109 throw new PickleException("Could not unpickle interpreter",e);
110 } finally {
111 sr.close();
112 }
113 }
114
115 /*** create a new workflow interpreter, populated with contents of rulesDoc */
116 public GroovyInterpreter newInterpreter(String rulesDoc,JesInterface env) throws PickleException{
117 StringReader sr = new StringReader(rulesDoc);
118 try {
119 Map rs = pickler.unmarshallRuleStore(sr);
120 GroovyInterpreter interp = new GroovyInterpreter(rs);
121 interp.setJesInterface(env);
122 return interp;
123 } catch (Exception e) {
124 throw new PickleException("Could not create new interpreter",e);
125 } finally {
126 sr.close();
127 }
128 }
129
130
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178