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