+++ /dev/null
-/*\r
- * Copyright (C) 2007-2012 Mathieu Baudier\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.argeo.slc.web.mvc.controllers;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.InputStream;\r
-import java.util.UUID;\r
-\r
-import javax.xml.transform.stream.StreamSource;\r
-\r
-import org.apache.commons.io.IOUtils;\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
-import org.argeo.slc.core.attachment.Attachment;\r
-import org.argeo.slc.core.attachment.AttachmentsStorage;\r
-import org.argeo.slc.core.attachment.SimpleAttachment;\r
-import org.argeo.slc.msg.ObjectList;\r
-import org.argeo.slc.process.SlcExecution;\r
-import org.springframework.oxm.Marshaller;\r
-import org.springframework.oxm.Unmarshaller;\r
-import org.springframework.xml.transform.StringResult;\r
-\r
-public class SlcExecutionManager {\r
- private final static Log log = LogFactory.getLog(SlcExecutionManager.class);\r
-\r
- private Unmarshaller unmarshaller;\r
- private Marshaller marshaller;\r
- private AttachmentsStorage attachmentsStorage;\r
-\r
- public SlcExecutionManager(Unmarshaller unmarshaller,\r
- Marshaller marshaller, AttachmentsStorage attachmentsStorage) {\r
-\r
- this.unmarshaller = unmarshaller;\r
- this.marshaller = marshaller;\r
- this.attachmentsStorage = attachmentsStorage;\r
- }\r
-\r
- void storeRealizedFlows(SlcExecution slcExecution) {\r
-\r
- Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()\r
- .toString(), slcExecution);\r
- InputStream in = null;\r
- try {\r
- ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
- StringResult result = new StringResult();\r
- marshaller.marshal(ol, result);\r
- in = new ByteArrayInputStream(result.toString().getBytes());\r
- attachmentsStorage.storeAttachment(attachment, in);\r
-\r
- slcExecution.setRealizedFlowsXml(attachment.getUuid());\r
- } catch (Exception e) {\r
- log.error("Could not store realized flows as attachment #"\r
- + attachment.getUuid(), e);\r
- } finally {\r
- IOUtils.closeQuietly(in);\r
- }\r
- }\r
-\r
- void retrieveRealizedFlows(SlcExecution slcExecution) {\r
- Attachment attachment = realizedFlowsAttachment(slcExecution\r
- .getRealizedFlowsXml(), slcExecution);\r
-\r
- ByteArrayOutputStream out = null;\r
- ByteArrayInputStream in = null;\r
- try {\r
- // TODO: optimize with piped streams\r
- out = new ByteArrayOutputStream();\r
- attachmentsStorage.retrieveAttachment(attachment, out);\r
-\r
- byte[] arr = out.toByteArray();\r
- in = new ByteArrayInputStream(arr);\r
- StreamSource source = new StreamSource(in);\r
- ObjectList ol = (ObjectList) unmarshaller.unmarshal(source);\r
- ol.fill(slcExecution.getRealizedFlows());\r
- } catch (Exception e) {\r
- log.error("Could not retrieve realized flows from attachment #"\r
- + attachment.getUuid(), e);\r
- } finally {\r
- IOUtils.closeQuietly(in);\r
- IOUtils.closeQuietly(out);\r
- }\r
- }\r
-\r
- /** Unify labeling in the package */\r
- static Attachment realizedFlowsAttachment(String attachmentUuid,\r
- SlcExecution slcExecution) {\r
- return new SimpleAttachment(attachmentUuid,\r
- "RealizedFlows of SlcExecution #" + slcExecution.getUuid(),\r
- "text/xml");\r
- }\r
-}\r