]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/SlcExecutionManager.java
Introduce H2 database support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / process / 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.process;
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.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;
39
40 public class SlcExecutionManager {
41 private final static Log log = LogFactory.getLog(SlcExecutionManager.class);
42
43 private SlcAgentFactory agentFactory;
44 private Unmarshaller unmarshaller;
45 private Marshaller marshaller;
46 private SlcExecutionService slcExecutionService;
47 private AttachmentsStorage attachmentsStorage;
48
49 public SlcExecutionManager(SlcAgentFactory agentFactory,
50 Unmarshaller unmarshaller, Marshaller marshaller,
51 SlcExecutionService slcExecutionService,
52 AttachmentsStorage attachmentsStorage) {
53
54 this.agentFactory = agentFactory;
55 this.unmarshaller = unmarshaller;
56 this.marshaller = marshaller;
57 this.slcExecutionService = slcExecutionService;
58 this.attachmentsStorage = attachmentsStorage;
59 }
60
61 public void storeRealizedFlows(SlcExecution slcExecution) {
62
63 Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()
64 .toString(), slcExecution);
65 InputStream in = null;
66 try {
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);
72
73 slcExecution.setRealizedFlowsXml(attachment.getUuid());
74 } catch (Exception e) {
75 log.error("Could not store realized flows as attachment #"
76 + attachment.getUuid(), e);
77 } finally {
78 IOUtils.closeQuietly(in);
79 }
80 }
81
82 public void retrieveRealizedFlows(SlcExecution slcExecution) {
83 Attachment attachment = realizedFlowsAttachment(slcExecution
84 .getRealizedFlowsXml(), slcExecution);
85
86 ByteArrayOutputStream out = null;
87 ByteArrayInputStream in = null;
88 try {
89 // TODO: optimize with piped streams
90 out = new ByteArrayOutputStream();
91 attachmentsStorage.retrieveAttachment(attachment, out);
92
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);
101 } finally {
102 IOUtils.closeQuietly(in);
103 IOUtils.closeQuietly(out);
104 }
105 }
106
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(),
112 "text/xml");
113 }
114 }