1 package org
.argeo
.slc
.web
.mvc
.process
;
3 import java
.io
.BufferedReader
;
4 import java
.io
.ByteArrayInputStream
;
5 import java
.io
.ByteArrayOutputStream
;
6 import java
.io
.InputStream
;
9 import javax
.servlet
.http
.HttpServletRequest
;
10 import javax
.servlet
.http
.HttpServletResponse
;
11 import javax
.xml
.transform
.stream
.StreamSource
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.core
.attachment
.Attachment
;
17 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
18 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
19 import org
.argeo
.slc
.msg
.MsgConstants
;
20 import org
.argeo
.slc
.msg
.ObjectList
;
21 import org
.argeo
.slc
.process
.SlcExecution
;
22 import org
.argeo
.slc
.process
.SlcExecutionStep
;
23 import org
.argeo
.slc
.runtime
.SlcAgent
;
24 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
25 import org
.argeo
.slc
.services
.SlcExecutionService
;
26 import org
.argeo
.slc
.web
.mvc
.AbstractServiceController
;
27 import org
.springframework
.oxm
.Marshaller
;
28 import org
.springframework
.oxm
.Unmarshaller
;
29 import org
.springframework
.util
.Assert
;
30 import org
.springframework
.web
.servlet
.ModelAndView
;
31 import org
.springframework
.xml
.transform
.StringResult
;
32 import org
.springframework
.xml
.transform
.StringSource
;
34 public class SlcExecutionManager
{
35 private final static Log log
= LogFactory
36 .getLog(SlcExecutionManager
.class);
38 private SlcAgentFactory agentFactory
;
39 private Unmarshaller unmarshaller
;
40 private Marshaller marshaller
;
41 private SlcExecutionService slcExecutionService
;
42 private AttachmentsStorage attachmentsStorage
;
44 public SlcExecutionManager(
45 SlcAgentFactory agentFactory
,
46 Unmarshaller unmarshaller
,
47 Marshaller marshaller
,
48 SlcExecutionService slcExecutionService
,
49 AttachmentsStorage attachmentsStorage
){
51 this.agentFactory
= agentFactory
;
52 this.unmarshaller
= unmarshaller
;
53 this.marshaller
= marshaller
;
54 this.slcExecutionService
= slcExecutionService
;
55 this.attachmentsStorage
= attachmentsStorage
;
59 protected void storeRealizedFlows(SlcExecution slcExecution
) {
61 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
62 .toString(), slcExecution
);
63 InputStream in
= null;
65 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
66 StringResult result
= new StringResult();
67 marshaller
.marshal(ol
, result
);
68 in
= new ByteArrayInputStream(result
.toString().getBytes());
69 attachmentsStorage
.storeAttachment(attachment
, in
);
71 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
72 } catch (Exception e
) {
73 log
.error("Could not store realized flows as attachment #"
74 + attachment
.getUuid(), e
);
76 IOUtils
.closeQuietly(in
);
80 protected void retrieveRealizedFlows(SlcExecution slcExecution
) {
81 Attachment attachment
= NewSlcExecutionController
82 .realizedFlowsAttachment(slcExecution
.getRealizedFlowsXml(),
85 ByteArrayOutputStream out
= null;
86 ByteArrayInputStream in
= null;
88 // TODO: optimize with piped streams
89 out
= new ByteArrayOutputStream();
90 attachmentsStorage
.retrieveAttachment(attachment
, out
);
92 byte[] arr
= out
.toByteArray();
93 in
= new ByteArrayInputStream(arr
);
94 StreamSource source
= new StreamSource(in
);
95 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
96 ol
.fill(slcExecution
.getRealizedFlows());
97 } catch (Exception e
) {
98 log
.error("Could not retrieve realized flows from attachment #"
99 + attachment
.getUuid(), e
);
101 IOUtils
.closeQuietly(in
);
102 IOUtils
.closeQuietly(out
);
106 /** Unify labeling in the package */
107 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
108 SlcExecution slcExecution
) {
109 return new SimpleAttachment(attachmentUuid
,
110 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),