]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Attachments management
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 7 May 2009 15:07:48 +0000 (15:07 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 7 May 2009 15:07:48 +0000 (15:07 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2419 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

12 files changed:
demo/site/org.argeo.slc.demo.basic/conf/attachments.xml [new file with mode: 0644]
demo/site/org.argeo.slc.demo.basic/conf/basic.xml
runtime/org.argeo.slc.support.castor/src/main/resources/org/argeo/slc/castor/msg.xml
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/attachment/SimpleAttachment.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/tasks/Echo.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/test/tree/ResultAttributes.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/spring.xml
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/templates.xml [new file with mode: 0644]

diff --git a/demo/site/org.argeo.slc.demo.basic/conf/attachments.xml b/demo/site/org.argeo.slc.demo.basic/conf/attachments.xml
new file mode 100644 (file)
index 0000000..b6e6934
--- /dev/null
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"\r
+       xmlns:aop="http://www.springframework.org/schema/aop"\r
+       xsi:schemaLocation="\r
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\r
+       http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd\r
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">\r
+\r
+       <bean id="attachments" parent="slcTemplate.simpleFlow">\r
+               <property name="executables">\r
+                       <list>\r
+                               <bean parent="task.echo">\r
+                                       <property name="message" value="DATA" />\r
+                                       <property name="writeTo" ref="attachment.writeTo" />\r
+                               </bean>\r
+\r
+                               <bean parent="task.uploadAttachments">\r
+                                       <property name="attachmentUploader" ref="attachmentUploader" />\r
+                                       <property name="attachment">\r
+                                               <bean parent="taskArg.attachment">\r
+                                                       <property name="name" value="myAttachment" />\r
+                                                       <property name="contentType" value="text/xml" />\r
+                                               </bean>\r
+                                       </property>\r
+                                       <property name="resource" ref="attachment.writeTo" />\r
+                                       <property name="attachTo">\r
+                                               <list>\r
+                                                       <ref bean="attachments.testResult" />\r
+                                               </list>\r
+                                       </property>\r
+                               </bean>\r
+\r
+                               <bean parent="task.closeTestResult" scope="execution">\r
+                                       <property name="testResult" ref="attachments.testResult" />\r
+                               </bean>\r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="attachments.testResult" parent="slcDefault.test.basicTreeTestResult"\r
+               scope="execution">\r
+               <property name="listeners" ref="resultListeners" />\r
+               <aop:scoped-proxy />\r
+       </bean>\r
+\r
+       <bean id="attachment.writeTo" factory-bean="attachment.resourcesManager"\r
+               factory-method="getWritableResource" scope="execution">\r
+               <constructor-arg value="subdir/writeTo" />\r
+               <aop:scoped-proxy />\r
+       </bean>\r
+\r
+       <bean id="attachment.resourcesManager" parent="slcTemplate.fileResources">\r
+               <property name="executionContext" ref="executionContext" />\r
+       </bean>\r
+\r
+</beans>
\ No newline at end of file
index 686fc5f63d06661b183724ced3eeabc0be00dbc7..5c1eed7fd88a86e6d970e8454fb6353a1470e03b 100644 (file)
                                        <property name="testData" ref="ref1" />\r
                                </bean>\r
 \r
+                               <!-- Attachments -->\r
+                               <bean parent="task.echo">\r
+                                       <property name="message" value="DATA" />\r
+                                       <property name="writeTo" ref="basic.writeTo" />\r
+                               </bean>\r
+\r
                                <bean parent="task.uploadAttachments">\r
                                        <property name="attachmentUploader" ref="attachmentUploader" />\r
                                        <property name="attachment">\r
                                                <bean parent="taskArg.attachment">\r
                                                        <property name="name" value="myAttachment" />\r
-                                                       <property name="contentType" value="text/xml" />\r
-                                                       <property name="uuid" value="1" />\r
+                                                       <property name="contentType" value="text/plain" />\r
                                                </bean>\r
                                        </property>\r
-                                       <property name="resource" value="osgibundle:/conf/main.xml" />\r
+                                       <property name="resource" ref="basic.writeTo" />\r
                                        <property name="attachTo">\r
                                                <list>\r
                                                        <ref bean="basic.testResult" />\r
                <property name="testResult" ref="basic.testResult" />\r
        </bean>\r
 \r
+       <bean id="basic.writeTo" factory-bean="attachment.resourcesManager"\r
+               factory-method="getWritableResource" scope="execution">\r
+               <constructor-arg value="subdir/writeTo" />\r
+               <aop:scoped-proxy />\r
+       </bean>\r
+\r
+       <bean id="basic.resourcesManager" parent="slcTemplate.fileResources">\r
+               <property name="executionContext" ref="executionContext" />\r
+       </bean>\r
+\r
+\r
 \r
 </beans>
\ No newline at end of file
index e523bbdf215273244d81cf59ec1f3f1df0bc0330..0ba1fdeed971e43b19a86821acdb0e2b52d8e847 100644 (file)
                                </class>
                        </bind-xml>
                </field>
+               <field name="attachments" collection="arraylist"
+                       type="org.argeo.slc.core.attachment.SimpleAttachment">
+                       <bind-xml auto-naming="deriveByClass" location="attachments" />
+               </field>
        </class>
 
 
index 1f1089bb263aedb58ffed903eaaf467d0d1f1341..02e1c8d91185864d45b42800421414a97cc53e63 100644 (file)
@@ -11,9 +11,23 @@ import org.apache.commons.io.IOUtils;
 import org.argeo.slc.SlcException;
 
 public class FileAttachmentsStorage implements AttachmentsStorage {
-       private File attachmentsDirectory = new File(System
-                       .getProperty("java.io.tmpdir")
-                       + File.separator + "slcAttachments");
+       private File attachmentsDirectory;
+
+       public FileAttachmentsStorage() {
+               String osgiInstanceArea = System.getProperty("osgi.instance.area");
+               if (osgiInstanceArea != null) {
+                       if (osgiInstanceArea.startsWith("file:"))
+                               osgiInstanceArea = osgiInstanceArea.substring("file:".length());
+                       attachmentsDirectory = new File(osgiInstanceArea + File.separator
+                                       + "executionResources");
+               }
+
+               if (attachmentsDirectory == null) {
+                       String tempDir = System.getProperty("java.io.tmpdir");
+                       attachmentsDirectory = new File(tempDir + File.separator
+                                       + "slcAttachments");
+               }
+       }
 
        public void retrieveAttachment(Attachment attachment,
                        OutputStream outputStream) {
index f5ee9df678a321d0392ada7ed3702c4e04f753d7..23dad53f16f61d7b7f027ec4aaf6447b93e79717 100644 (file)
@@ -1,7 +1,9 @@
 package org.argeo.slc.core.attachment;
 
+import java.util.UUID;
+
 public class SimpleAttachment implements Attachment {
-       private String uuid;
+       private String uuid = UUID.randomUUID().toString();
        private String name;
        private String contentType;
 
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java
new file mode 100644 (file)
index 0000000..f10de57
--- /dev/null
@@ -0,0 +1,7 @@
+package org.argeo.slc.core.execution;
+
+import org.springframework.core.io.Resource;
+
+public interface ExecutionResources {
+       public Resource getWritableResource(String relativePath);
+}
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
new file mode 100644 (file)
index 0000000..e35ff00
--- /dev/null
@@ -0,0 +1,51 @@
+package org.argeo.slc.core.execution;
+
+import java.io.File;
+
+import org.argeo.slc.execution.ExecutionContext;
+import org.springframework.core.io.FileSystemResource;
+import org.springframework.core.io.Resource;
+
+public class FileExecutionResources implements ExecutionResources {
+       private File baseDir;
+       private ExecutionContext executionContext;
+
+       public FileExecutionResources() {
+               String osgiInstanceArea = System.getProperty("osgi.instance.area");
+               if (osgiInstanceArea != null) {
+                       if (osgiInstanceArea.startsWith("file:"))
+                               osgiInstanceArea = osgiInstanceArea.substring("file:".length());
+                       baseDir = new File(osgiInstanceArea + File.separator
+                                       + "executionResources");
+               }
+
+               if (baseDir == null) {
+                       String tempDir = System.getProperty("java.io.tmpdir");
+                       baseDir = new File(tempDir + File.separator
+                                       + "slcExecutionResources");
+               }
+       }
+
+       public Resource getWritableResource(String relativePath) {
+               File file = getFile(relativePath);
+               file.getParentFile().mkdirs();
+               return new FileSystemResource(file);
+       }
+
+       public File getFile(String relativePath) {
+               File executionDir = new File(baseDir.getPath() + File.separator
+                               + executionContext.getUuid());
+               if (!executionDir.exists())
+                       executionDir.mkdirs();
+               return new File(executionDir.getPath() + File.separator + relativePath);
+       }
+
+       public void setBaseDir(File baseDir) {
+               this.baseDir = baseDir;
+       }
+
+       public void setExecutionContext(ExecutionContext executionContext) {
+               this.executionContext = executionContext;
+       }
+
+}
index 5c932899084796970a7a216ce59bad92f4069532..07ec879aa2b15c6192118d2a5f8758664896493f 100644 (file)
@@ -1,16 +1,34 @@
 package org.argeo.slc.core.execution.tasks;
 
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
+import org.springframework.core.io.Resource;
 
 public class Echo implements Runnable {
        private final static Log defaultLog = LogFactory.getLog(Echo.class);
+       private Resource writeTo = null;
 
        private Log log;
        private String message;
 
        public void run() {
                log().info(message);
+
+               if (writeTo != null) {
+                       try {
+                               File file = writeTo.getFile();
+                               if (log().isDebugEnabled())
+                                       log().debug("Write to " + file);
+                               FileUtils.writeStringToFile(file, message);
+                       } catch (IOException e) {
+                               throw new SlcException("Could not write to " + writeTo, e);
+                       }
+               }
        }
 
        protected Log log() {
@@ -25,4 +43,8 @@ public class Echo implements Runnable {
                this.message = message;
        }
 
+       public void setWriteTo(Resource writeTo) {
+               this.writeTo = writeTo;
+       }
+
 }
index 3a2c842e36930a8241e8c24bc16a41f3b0cf2734..ec09ce51f2ec851eb7c3a9750cd3a2b8104348ce 100644 (file)
@@ -1,13 +1,18 @@
 package org.argeo.slc.core.test.tree;
 
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
 
+import org.argeo.slc.core.attachment.SimpleAttachment;
+
 public class ResultAttributes {
        private String uuid = null;
        private Date closeDate = null;
        private Map<String, String> attributes = new Hashtable<String, String>();
+       private List<SimpleAttachment> attachments = new ArrayList<SimpleAttachment>();
 
        public ResultAttributes() {
                super();
@@ -18,6 +23,7 @@ public class ResultAttributes {
                this.uuid = ttr.getUuid();
                this.attributes = ttr.getAttributes();
                this.closeDate = ttr.getCloseDate();
+               this.attachments = ttr.getAttachments();
        }
 
        public String getUuid() {
@@ -44,4 +50,12 @@ public class ResultAttributes {
                this.closeDate = closeDate;
        }
 
+       public List<SimpleAttachment> getAttachments() {
+               return attachments;
+       }
+
+       public void setAttachments(List<SimpleAttachment> attachments) {
+               this.attachments = attachments;
+       }
+
 }
index c7fa530ace9a8a6b4ff9856cf74cf84fd1ee8c55..9ab13e9dc24e8282866e8d826d87bfdf52e481d7 100644 (file)
@@ -13,6 +13,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;\r
 import org.argeo.slc.core.attachment.Attachment;\r
 import org.argeo.slc.core.attachment.AttachmentsEnabled;\r
+import org.argeo.slc.core.attachment.SimpleAttachment;\r
 import org.argeo.slc.core.structure.tree.TreeSPath;\r
 import org.argeo.slc.structure.StructureAware;\r
 import org.argeo.slc.structure.StructureElement;\r
@@ -46,7 +47,7 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
 \r
        private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();\r
        private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();\r
-       private List<Attachment> attachments = new ArrayList<Attachment>();\r
+       private List<SimpleAttachment> attachments = new ArrayList<SimpleAttachment>();\r
 \r
        private Map<String, String> attributes = new TreeMap<String, String>();\r
 \r
@@ -219,16 +220,16 @@ public class TreeTestResult implements TestResult, StructureAware<TreeSPath>,
                this.warnIfAlreadyClosed = warnIfAlreadyClosed;\r
        }\r
 \r
-       public List<Attachment> getAttachments() {\r
+       public List<SimpleAttachment> getAttachments() {\r
                return attachments;\r
        }\r
 \r
-       public void setAttachments(List<Attachment> attachments) {\r
+       public void setAttachments(List<SimpleAttachment> attachments) {\r
                this.attachments = attachments;\r
        }\r
 \r
        public void addAttachment(Attachment attachment) {\r
-               attachments.add(attachment);\r
+               attachments.add((SimpleAttachment) attachment);\r
                synchronized (listeners) {\r
                        for (TestResultListener<TreeTestResult> listener : listeners) {\r
                                if (listener instanceof TreeTestResultListener)\r
index ef7d1499eb3035ebb88bd601b1b4d77365d1ab83..663ea3cb5625dd68049e41ae721f8f55a6622fc6 100644 (file)
@@ -8,6 +8,7 @@
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
        <import resource="specs.xml" />
+       <import resource="templates.xml" />
        <import resource="tasks/core.xml" />
 
        <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
diff --git a/runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/templates.xml b/runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/templates.xml
new file mode 100644 (file)
index 0000000..12ee7d4
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+       <bean id="slcTemplate.fileResources" class="org.argeo.slc.core.execution.FileExecutionResources"
+               abstract="true" />
+
+</beans>
\ No newline at end of file