View Javadoc

1   /*$Id: GroovyInterpreterFactory.java,v 1.6 2004/11/05 16:52:42 jdt Exp $
2    * Created on 14-May-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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          // see if its already there first..
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          //StringWriter writer =  new StringWriter();
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 $Log: GroovyInterpreterFactory.java,v $
136 Revision 1.6  2004/11/05 16:52:42  jdt
137 Merges from branch nww-itn07-scratchspace
138 
139 Revision 1.5.18.1  2004/11/05 16:13:08  nw
140 replaced stringWriter with TemporaryBuffer
141 updated to work with new ruleStore
142 
143 Revision 1.5  2004/09/16 21:47:56  nw
144 made sure all streams are closed
145 
146 Revision 1.4  2004/09/06 16:30:25  nw
147 javadoc
148 
149 Revision 1.3  2004/08/09 17:32:18  nw
150 updated due to removing RuleStore
151 
152 Revision 1.2  2004/07/30 15:42:34  nw
153 merged in branch nww-itn06-bz#441 (groovy scripting)
154 
155 Revision 1.1.2.2  2004/07/28 16:24:23  nw
156 finished groovy beans.
157 moved useful tests from old python package.
158 removed python implemntation
159 
160 Revision 1.1.2.1  2004/07/27 23:37:59  nw
161 refactoed framework.
162 experimented with betwixt - can't get it to work.
163 got XStream working in 5 mins.
164 about to remove betwixt code.
165 
166 Revision 1.1.2.1  2004/07/26 15:51:19  nw
167 first stab at a groovy scheduler.
168 transcribed all the classes in the python prototype, and took copies of the
169 classes in the 'scripting' package.
170 
171 Revision 1.1  2004/07/09 09:30:28  nw
172 merged in scripting workflow interpreter from branch
173 nww-x-workflow-extensions
174 
175 Revision 1.1.2.1  2004/05/21 11:25:19  nw
176 first checkin of prototype scrpting workflow interpreter
177  
178 */