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
.ByteArrayInputStream
;
20 import java
.io
.ByteArrayOutputStream
;
21 import java
.io
.InputStream
;
22 import java
.util
.UUID
;
24 import javax
.xml
.transform
.stream
.StreamSource
;
26 import org
.apache
.commons
.io
.IOUtils
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
29 import org
.argeo
.slc
.core
.attachment
.Attachment
;
30 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
31 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
32 import org
.argeo
.slc
.msg
.ObjectList
;
33 import org
.argeo
.slc
.process
.SlcExecution
;
34 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
35 import org
.argeo
.slc
.services
.SlcExecutionService
;
36 import org
.springframework
.oxm
.Marshaller
;
37 import org
.springframework
.oxm
.Unmarshaller
;
38 import org
.springframework
.xml
.transform
.StringResult
;
40 public class SlcExecutionManager
{
41 private final static Log log
= LogFactory
42 .getLog(SlcExecutionManager
.class);
44 private SlcAgentFactory agentFactory
;
45 private Unmarshaller unmarshaller
;
46 private Marshaller marshaller
;
47 private SlcExecutionService slcExecutionService
;
48 private AttachmentsStorage attachmentsStorage
;
50 public SlcExecutionManager(
51 SlcAgentFactory agentFactory
,
52 Unmarshaller unmarshaller
,
53 Marshaller marshaller
,
54 SlcExecutionService slcExecutionService
,
55 AttachmentsStorage attachmentsStorage
){
57 this.agentFactory
= agentFactory
;
58 this.unmarshaller
= unmarshaller
;
59 this.marshaller
= marshaller
;
60 this.slcExecutionService
= slcExecutionService
;
61 this.attachmentsStorage
= attachmentsStorage
;
65 protected void storeRealizedFlows(SlcExecution slcExecution
) {
67 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
68 .toString(), slcExecution
);
69 InputStream in
= null;
71 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
72 StringResult result
= new StringResult();
73 marshaller
.marshal(ol
, result
);
74 in
= new ByteArrayInputStream(result
.toString().getBytes());
75 attachmentsStorage
.storeAttachment(attachment
, in
);
77 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
78 } catch (Exception e
) {
79 log
.error("Could not store realized flows as attachment #"
80 + attachment
.getUuid(), e
);
82 IOUtils
.closeQuietly(in
);
86 protected void retrieveRealizedFlows(SlcExecution slcExecution
) {
87 Attachment attachment
= realizedFlowsAttachment(slcExecution
.getRealizedFlowsXml(),
90 ByteArrayOutputStream out
= null;
91 ByteArrayInputStream in
= null;
93 // TODO: optimize with piped streams
94 out
= new ByteArrayOutputStream();
95 attachmentsStorage
.retrieveAttachment(attachment
, out
);
97 byte[] arr
= out
.toByteArray();
98 in
= new ByteArrayInputStream(arr
);
99 StreamSource source
= new StreamSource(in
);
100 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
101 ol
.fill(slcExecution
.getRealizedFlows());
102 } catch (Exception e
) {
103 log
.error("Could not retrieve realized flows from attachment #"
104 + attachment
.getUuid(), e
);
106 IOUtils
.closeQuietly(in
);
107 IOUtils
.closeQuietly(out
);
111 /** Unify labeling in the package */
112 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
113 SlcExecution slcExecution
) {
114 return new SimpleAttachment(attachmentUuid
,
115 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),