2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.web
.mvc
.controllers
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.ByteArrayOutputStream
;
20 import java
.io
.InputStream
;
21 import java
.util
.UUID
;
23 import javax
.xml
.transform
.stream
.StreamSource
;
25 import org
.apache
.commons
.io
.IOUtils
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.core
.attachment
.Attachment
;
29 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
30 import org
.argeo
.slc
.core
.attachment
.SimpleAttachment
;
31 import org
.argeo
.slc
.msg
.ObjectList
;
32 import org
.argeo
.slc
.process
.SlcExecution
;
33 import org
.springframework
.oxm
.Marshaller
;
34 import org
.springframework
.oxm
.Unmarshaller
;
35 import org
.springframework
.xml
.transform
.StringResult
;
37 public class SlcExecutionManager
{
38 private final static Log log
= LogFactory
.getLog(SlcExecutionManager
.class);
40 private Unmarshaller unmarshaller
;
41 private Marshaller marshaller
;
42 private AttachmentsStorage attachmentsStorage
;
44 public SlcExecutionManager(Unmarshaller unmarshaller
,
45 Marshaller marshaller
, AttachmentsStorage attachmentsStorage
) {
47 this.unmarshaller
= unmarshaller
;
48 this.marshaller
= marshaller
;
49 this.attachmentsStorage
= attachmentsStorage
;
52 void storeRealizedFlows(SlcExecution slcExecution
) {
54 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
55 .toString(), slcExecution
);
56 InputStream in
= null;
58 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
59 StringResult result
= new StringResult();
60 marshaller
.marshal(ol
, result
);
61 in
= new ByteArrayInputStream(result
.toString().getBytes());
62 attachmentsStorage
.storeAttachment(attachment
, in
);
64 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
65 } catch (Exception e
) {
66 log
.error("Could not store realized flows as attachment #"
67 + attachment
.getUuid(), e
);
69 IOUtils
.closeQuietly(in
);
73 void retrieveRealizedFlows(SlcExecution slcExecution
) {
74 Attachment attachment
= realizedFlowsAttachment(slcExecution
75 .getRealizedFlowsXml(), slcExecution
);
77 ByteArrayOutputStream out
= null;
78 ByteArrayInputStream in
= null;
80 // TODO: optimize with piped streams
81 out
= new ByteArrayOutputStream();
82 attachmentsStorage
.retrieveAttachment(attachment
, out
);
84 byte[] arr
= out
.toByteArray();
85 in
= new ByteArrayInputStream(arr
);
86 StreamSource source
= new StreamSource(in
);
87 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
88 ol
.fill(slcExecution
.getRealizedFlows());
89 } catch (Exception e
) {
90 log
.error("Could not retrieve realized flows from attachment #"
91 + attachment
.getUuid(), e
);
93 IOUtils
.closeQuietly(in
);
94 IOUtils
.closeQuietly(out
);
98 /** Unify labeling in the package */
99 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
100 SlcExecution slcExecution
) {
101 return new SimpleAttachment(attachmentUuid
,
102 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),