2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.web
.mvc
.process
;
19 import java
.io
.BufferedReader
;
20 import java
.io
.ByteArrayInputStream
;
21 import java
.io
.ByteArrayOutputStream
;
22 import java
.io
.InputStream
;
23 import java
.util
.UUID
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
27 import javax
.xml
.transform
.stream
.StreamSource
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.slc
.core
.attachment
.Attachment
;
33 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
34 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
35 import org
.argeo
.slc
.msg
.MsgConstants
;
36 import org
.argeo
.slc
.msg
.ObjectList
;
37 import org
.argeo
.slc
.process
.SlcExecution
;
38 import org
.argeo
.slc
.process
.SlcExecutionStep
;
39 import org
.argeo
.slc
.runtime
.SlcAgent
;
40 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
41 import org
.argeo
.slc
.services
.SlcExecutionService
;
42 import org
.argeo
.slc
.web
.mvc
.AbstractServiceController
;
43 import org
.springframework
.oxm
.Marshaller
;
44 import org
.springframework
.oxm
.Unmarshaller
;
45 import org
.springframework
.util
.Assert
;
46 import org
.springframework
.web
.servlet
.ModelAndView
;
47 import org
.springframework
.xml
.transform
.StringResult
;
48 import org
.springframework
.xml
.transform
.StringSource
;
50 public class SlcExecutionManager
{
51 private final static Log log
= LogFactory
52 .getLog(SlcExecutionManager
.class);
54 private SlcAgentFactory agentFactory
;
55 private Unmarshaller unmarshaller
;
56 private Marshaller marshaller
;
57 private SlcExecutionService slcExecutionService
;
58 private AttachmentsStorage attachmentsStorage
;
60 public SlcExecutionManager(
61 SlcAgentFactory agentFactory
,
62 Unmarshaller unmarshaller
,
63 Marshaller marshaller
,
64 SlcExecutionService slcExecutionService
,
65 AttachmentsStorage attachmentsStorage
){
67 this.agentFactory
= agentFactory
;
68 this.unmarshaller
= unmarshaller
;
69 this.marshaller
= marshaller
;
70 this.slcExecutionService
= slcExecutionService
;
71 this.attachmentsStorage
= attachmentsStorage
;
75 protected void storeRealizedFlows(SlcExecution slcExecution
) {
77 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
78 .toString(), slcExecution
);
79 InputStream in
= null;
81 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
82 StringResult result
= new StringResult();
83 marshaller
.marshal(ol
, result
);
84 in
= new ByteArrayInputStream(result
.toString().getBytes());
85 attachmentsStorage
.storeAttachment(attachment
, in
);
87 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
88 } catch (Exception e
) {
89 log
.error("Could not store realized flows as attachment #"
90 + attachment
.getUuid(), e
);
92 IOUtils
.closeQuietly(in
);
96 protected void retrieveRealizedFlows(SlcExecution slcExecution
) {
97 Attachment attachment
= NewSlcExecutionController
98 .realizedFlowsAttachment(slcExecution
.getRealizedFlowsXml(),
101 ByteArrayOutputStream out
= null;
102 ByteArrayInputStream in
= null;
104 // TODO: optimize with piped streams
105 out
= new ByteArrayOutputStream();
106 attachmentsStorage
.retrieveAttachment(attachment
, out
);
108 byte[] arr
= out
.toByteArray();
109 in
= new ByteArrayInputStream(arr
);
110 StreamSource source
= new StreamSource(in
);
111 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
112 ol
.fill(slcExecution
.getRealizedFlows());
113 } catch (Exception e
) {
114 log
.error("Could not retrieve realized flows from attachment #"
115 + attachment
.getUuid(), e
);
117 IOUtils
.closeQuietly(in
);
118 IOUtils
.closeQuietly(out
);
122 /** Unify labeling in the package */
123 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
124 SlcExecution slcExecution
) {
125 return new SimpleAttachment(attachmentUuid
,
126 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),