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
.controllers
;
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
.springframework
.oxm
.Marshaller
;
35 import org
.springframework
.oxm
.Unmarshaller
;
36 import org
.springframework
.xml
.transform
.StringResult
;
38 public class SlcExecutionManager
{
39 private final static Log log
= LogFactory
.getLog(SlcExecutionManager
.class);
41 private Unmarshaller unmarshaller
;
42 private Marshaller marshaller
;
43 private AttachmentsStorage attachmentsStorage
;
45 public SlcExecutionManager(Unmarshaller unmarshaller
,
46 Marshaller marshaller
, AttachmentsStorage attachmentsStorage
) {
48 this.unmarshaller
= unmarshaller
;
49 this.marshaller
= marshaller
;
50 this.attachmentsStorage
= attachmentsStorage
;
53 void storeRealizedFlows(SlcExecution slcExecution
) {
55 Attachment attachment
= realizedFlowsAttachment(UUID
.randomUUID()
56 .toString(), slcExecution
);
57 InputStream in
= null;
59 ObjectList ol
= new ObjectList(slcExecution
.getRealizedFlows());
60 StringResult result
= new StringResult();
61 marshaller
.marshal(ol
, result
);
62 in
= new ByteArrayInputStream(result
.toString().getBytes());
63 attachmentsStorage
.storeAttachment(attachment
, in
);
65 slcExecution
.setRealizedFlowsXml(attachment
.getUuid());
66 } catch (Exception e
) {
67 log
.error("Could not store realized flows as attachment #"
68 + attachment
.getUuid(), e
);
70 IOUtils
.closeQuietly(in
);
74 void retrieveRealizedFlows(SlcExecution slcExecution
) {
75 Attachment attachment
= realizedFlowsAttachment(slcExecution
76 .getRealizedFlowsXml(), slcExecution
);
78 ByteArrayOutputStream out
= null;
79 ByteArrayInputStream in
= null;
81 // TODO: optimize with piped streams
82 out
= new ByteArrayOutputStream();
83 attachmentsStorage
.retrieveAttachment(attachment
, out
);
85 byte[] arr
= out
.toByteArray();
86 in
= new ByteArrayInputStream(arr
);
87 StreamSource source
= new StreamSource(in
);
88 ObjectList ol
= (ObjectList
) unmarshaller
.unmarshal(source
);
89 ol
.fill(slcExecution
.getRealizedFlows());
90 } catch (Exception e
) {
91 log
.error("Could not retrieve realized flows from attachment #"
92 + attachment
.getUuid(), e
);
94 IOUtils
.closeQuietly(in
);
95 IOUtils
.closeQuietly(out
);
99 /** Unify labeling in the package */
100 static Attachment
realizedFlowsAttachment(String attachmentUuid
,
101 SlcExecution slcExecution
) {
102 return new SimpleAttachment(attachmentUuid
,
103 "RealizedFlows of SlcExecution #" + slcExecution
.getUuid(),