Introduce a factory bean to use execution resources
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 11 May 2009 13:41:01 +0000 (13:41 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 11 May 2009 13:41:01 +0000 (13:41 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2433 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResourcesFactoryBean.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/templates.xml
runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesSpringTest.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/executionResources.xml [new file with mode: 0644]

diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResourcesFactoryBean.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResourcesFactoryBean.java
new file mode 100644 (file)
index 0000000..68ff63e
--- /dev/null
@@ -0,0 +1,34 @@
+package org.argeo.slc.core.execution;
+
+import org.springframework.beans.factory.FactoryBean;
+import org.springframework.core.io.Resource;
+import org.springframework.util.Assert;
+
+/** Workaround when execution placedholders needs to be passed.*/
+public class ExecutionResourcesFactoryBean implements FactoryBean {
+       private ExecutionResources executionResources;
+       private String relativePath;
+
+       public Object getObject() throws Exception {
+               Assert.notNull(executionResources, "executionResources is null");
+               Assert.notNull(relativePath, "relativePath is null");
+               return executionResources.getWritableResource(relativePath);
+       }
+
+       public Class<? extends Object> getObjectType() {
+               return Resource.class;
+       }
+
+       public boolean isSingleton() {
+               return true;
+       }
+
+       public void setExecutionResources(ExecutionResources executionResources) {
+               this.executionResources = executionResources;
+       }
+
+       public void setRelativePath(String relativePath) {
+               this.relativePath = relativePath;
+       }
+
+}
index e6530832ad7638ea7d41babadfe64363df49a4b9..a023fc7f579ba99d30f96a37150752425dc4c443 100644 (file)
@@ -14,12 +14,20 @@ public class FileExecutionResources implements ExecutionResources {
        private final static Log log = LogFactory
                        .getLog(FileExecutionResources.class);
        protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
+       public final static String DEFAULT_EXECUTION_RESOURCES_TMP_PATH = System
+                       .getProperty("java.io.tmpdir")
+                       + File.separator
+                       + "slc"
+                       + File.separator
+                       + DEFAULT_EXECUTION_RESOURCES_DIRNAME;
 
        private File baseDir;
        private ExecutionContext executionContext;
        private String prefixDatePattern = "yyyyMMdd_HHmmss_";
        private SimpleDateFormat sdf = null;
 
+       private Boolean withExecutionSubdirectory = true;
+
        public FileExecutionResources() {
                // Default base directory
                String osgiInstanceArea = System.getProperty("osgi.instance.area");
@@ -38,8 +46,7 @@ public class FileExecutionResources implements ExecutionResources {
                        baseDir = new File(osgiInstanceAreaDefault + File.separator
                                        + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
                } else {// outside OSGi
-                       baseDir = new File(tempDir + File.separator + "slc"
-                                       + File.separator + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
+                       baseDir = new File(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
                }
        }
 
@@ -61,6 +68,7 @@ public class FileExecutionResources implements ExecutionResources {
                        parentDir.mkdirs();
                }
                Resource resource = new FileSystemResource(file);
+
                if (log.isTraceEnabled())
                        log.trace("Returns writable resource " + resource);
                return resource;
@@ -69,13 +77,18 @@ public class FileExecutionResources implements ExecutionResources {
        public File getFile(String relativePath) {
                Assert.notNull(executionContext, "execution context is null");
 
-               String path = baseDir.getPath() + File.separator
-                               + sdf().format(executionContext.getCreationDate())
-                               + executionContext.getUuid();
-               File executionDir = new File(path);
-
-               return new File(executionDir.getPath() + File.separator
-                               + relativePath.replace('/', File.separatorChar));
+               if (withExecutionSubdirectory) {
+                       String path = baseDir.getPath() + File.separator
+                                       + sdf().format(executionContext.getCreationDate())
+                                       + executionContext.getUuid();
+                       File executionDir = new File(path);
+
+                       return new File(executionDir.getPath() + File.separator
+                                       + relativePath.replace('/', File.separatorChar));
+               } else {
+                       return new File(baseDir.getPath() + File.separator
+                                       + relativePath.replace('/', File.separatorChar));
+               }
        }
 
        protected String removeFilePrefix(String url) {
@@ -110,4 +123,9 @@ public class FileExecutionResources implements ExecutionResources {
        public String getPrefixDatePattern() {
                return prefixDatePattern;
        }
+
+       public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
+               this.withExecutionSubdirectory = withExecutionSubdirectory;
+       }
+
 }
index 12ee7d47863988b3689e468a22f0aac7b5c380af..5f022268d96dc1047ffb5e6db42c748658333778 100644 (file)
@@ -7,4 +7,8 @@
        <bean id="slcTemplate.fileResources" class="org.argeo.slc.core.execution.FileExecutionResources"
                abstract="true" />
 
+       <bean id="slcTemplate.resourcesFactoryBean"
+               class="org.argeo.slc.core.execution.ExecutionResourcesFactoryBean"
+               abstract="true" />
+
 </beans>
\ No newline at end of file
diff --git a/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesSpringTest.java b/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesSpringTest.java
new file mode 100644 (file)
index 0000000..0d1223a
--- /dev/null
@@ -0,0 +1,53 @@
+package org.argeo.slc.core.execution;
+
+import java.io.File;
+
+public class FileExecutionResourcesSpringTest extends
+               AbstractExecutionFlowTestCase {
+       private String basePath = FileExecutionResources.DEFAULT_EXECUTION_RESOURCES_TMP_PATH;
+
+       public void testSimple() throws Exception {
+               File file = getFile("subdir/writeTo");
+               try {
+                       assertFalse(file.exists());
+                       configureAndExecuteSlcFlow("executionResources.xml",
+                                       "executionResources.simple");
+                       assertTrue(file.exists());
+               } finally {
+                       file.deleteOnExit();
+               }
+       }
+
+       public void testPlaceholderPass() throws Exception {
+               File file = getFile("subdir/60");
+               try {
+                       assertFalse(file.exists());
+                       configureAndExecuteSlcFlow("executionResources.xml",
+                                       "executionResources.placeholderPass");
+                       assertTrue(file.exists());
+               } finally {
+                       file.deleteOnExit();
+               }
+       }
+
+       /**
+        * Test that it generate the wrong file because of issue when using
+        * execution placeholder in contructor-arg
+        */
+       public void testPlaceholderFail() throws Exception {
+               File file = getFile("subdir/@{var}");
+               try {
+                       assertFalse(file.exists());
+                       configureAndExecuteSlcFlow("executionResources.xml",
+                                       "executionResources.placeholderFail");
+                       assertTrue(file.exists());
+               } finally {
+                       file.deleteOnExit();
+               }
+       }
+
+       protected File getFile(String relativePath) {
+               return new File(basePath + File.separator
+                               + relativePath.replace('/', File.separatorChar));
+       }
+}
diff --git a/runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/executionResources.xml b/runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/executionResources.xml
new file mode 100644 (file)
index 0000000..8b0f440
--- /dev/null
@@ -0,0 +1,81 @@
+<?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
+       <import resource="imports.xml" />\r
+\r
+       <bean id="executionResources.spec" parent="slcTemplate.simpleSpec">\r
+               <property name="attributes">\r
+                       <map>\r
+                               <entry key="var">\r
+                                       <bean parent="specAttr.primitive" p:isParameter="false"\r
+                                               p:type="integer" p:value="60" />\r
+                               </entry>\r
+                       </map>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="executionResources.placeholderPass" parent="slcTemplate.simpleFlow">\r
+               <constructor-arg ref="executionResources.spec" />\r
+               <property name="executables">\r
+                       <list>\r
+                               <bean parent="task.echo">\r
+                                       <property name="message" value="DATA" />\r
+                                       <property name="writeTo">\r
+                                               <bean parent="slcTemplate.resourcesFactoryBean" scope="execution">\r
+                                                       <property name="executionResources" ref="executionResources" />\r
+                                                       <property name="relativePath" value="subdir/@{var}" />\r
+                                                       <aop:scoped-proxy />\r
+                                               </bean>\r
+                                       </property>\r
+                               </bean>\r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="executionResources.placeholderFail" parent="slcTemplate.simpleFlow">\r
+               <constructor-arg ref="executionResources.spec" />\r
+               <property name="executables">\r
+                       <list>\r
+                               <bean parent="task.echo">\r
+                                       <property name="message" value="DATA" />\r
+                                       <property name="writeTo">\r
+                                               <bean factory-bean="executionResources" factory-method="getWritableResource"\r
+                                                       scope="execution">\r
+                                                       <constructor-arg value="subdir/@{var}" />\r
+                                                       <aop:scoped-proxy />\r
+                                               </bean>\r
+                                       </property>\r
+                               </bean>\r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="executionResources.simple" parent="slcTemplate.simpleFlow">\r
+               <property name="executables">\r
+                       <list>\r
+                               <bean parent="task.echo">\r
+                                       <property name="message" value="DATA" />\r
+                                       <property name="writeTo">\r
+                                               <bean factory-bean="executionResources" factory-method="getWritableResource"\r
+                                                       scope="execution">\r
+                                                       <constructor-arg value="subdir/writeTo" />\r
+                                                       <aop:scoped-proxy />\r
+                                               </bean>\r
+                                       </property>\r
+                               </bean>\r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="executionResources" parent="slcTemplate.fileResources">\r
+               <property name="executionContext" ref="executionContext" />\r
+               <property name="withExecutionSubdirectory" value="false" />\r
+       </bean>\r
+\r
+</beans>
\ No newline at end of file