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
.getLog(SlcExecutionManager
.class);
43 private SlcAgentFactory agentFactory
;
44 private Unmarshaller unmarshaller
;
45 private Marshaller marshaller
;
46 private SlcExecutionService slcExecutionService
;
47 private AttachmentsStorage attachmentsStorage
;
49 public SlcExecutionManager(SlcAgentFactory agentFactory
,
50 Unmarshaller unmarshaller
, Marshaller marshaller
,
51 SlcExecutionService slcExecutionService
,
52 AttachmentsStorage attachmentsStorage
) {
54 this.agentFactory
= agentFactory
;
55 this.unmarshaller
= unmarshaller
;
56 this.marshaller
= marshaller
;
57 this.slcExecutionService
= slcExecutionService
;
58 this.attachmentsStorage
= attachmentsStorage
;
61 public void storeRealizedFlows(SlcExecution slcExecution
) {
63 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
64 .toString(), slcExecution
);
65 InputStream in
= null;
67 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
68 StringResult result
= new StringResult();
69 marshaller
.marshal(ol
, result
);
70 in
= new ByteArrayInputStream(result
.toString().getBytes());
71 attachmentsStorage
.storeAttachment(attachment
, in
);
73 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
74 } catch (Exception e
) {
75 log
.error("Could not store realized flows as attachment #"
76 + attachment
.getUuid(), e
);
78 IOUtils
.closeQuietly(in
);
82 public void retrieveRealizedFlows(SlcExecution slcExecution
) {
83 Attachment attachment
= realizedFlowsAttachment(slcExecution
84 .getRealizedFlowsXml(), slcExecution
);
86 ByteArrayOutputStream out
= null;
87 ByteArrayInputStream in
= null;
89 // TODO: optimize with piped streams
90 out
= new ByteArrayOutputStream();
91 attachmentsStorage
.retrieveAttachment(attachment
, out
);
93 byte[] arr
= out
.toByteArray();
94 in
= new ByteArrayInputStream(arr
);
95 StreamSource source
= new StreamSource(in
);
96 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
97 ol
.fill(slcExecution
.getRealizedFlows());
98 } catch (Exception e
) {
99 log
.error("Could not retrieve realized flows from attachment #"
100 + attachment
.getUuid(), e
);
102 IOUtils
.closeQuietly(in
);
103 IOUtils
.closeQuietly(out
);
107 /** Unify labeling in the package */
108 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
109 SlcExecution slcExecution
) {
110 return new SimpleAttachment(attachmentUuid
,
111 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),