]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/SlcExecutionManager.java
Move NameVersion
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / SlcExecutionManager.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.web.mvc.controllers;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.InputStream;
22 import java.util.UUID;
23
24 import javax.xml.transform.stream.StreamSource;
25
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;
37
38 public class SlcExecutionManager {
39 private final static Log log = LogFactory.getLog(SlcExecutionManager.class);
40
41 private Unmarshaller unmarshaller;
42 private Marshaller marshaller;
43 private AttachmentsStorage attachmentsStorage;
44
45 public SlcExecutionManager(Unmarshaller unmarshaller,
46 Marshaller marshaller, AttachmentsStorage attachmentsStorage) {
47
48 this.unmarshaller = unmarshaller;
49 this.marshaller = marshaller;
50 this.attachmentsStorage = attachmentsStorage;
51 }
52
53 void storeRealizedFlows(SlcExecution slcExecution) {
54
55 Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()
56 .toString(), slcExecution);
57 InputStream in = null;
58 try {
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);
64
65 slcExecution.setRealizedFlowsXml(attachment.getUuid());
66 } catch (Exception e) {
67 log.error("Could not store realized flows as attachment #"
68 + attachment.getUuid(), e);
69 } finally {
70 IOUtils.closeQuietly(in);
71 }
72 }
73
74 void retrieveRealizedFlows(SlcExecution slcExecution) {
75 Attachment attachment = realizedFlowsAttachment(slcExecution
76 .getRealizedFlowsXml(), slcExecution);
77
78 ByteArrayOutputStream out = null;
79 ByteArrayInputStream in = null;
80 try {
81 // TODO: optimize with piped streams
82 out = new ByteArrayOutputStream();
83 attachmentsStorage.retrieveAttachment(attachment, out);
84
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);
93 } finally {
94 IOUtils.closeQuietly(in);
95 IOUtils.closeQuietly(out);
96 }
97 }
98
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(),
104 "text/xml");
105 }
106 }