Fix issue with Windows path and add unit test
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 8 May 2009 08:47:46 +0000 (08:47 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 8 May 2009 08:47:46 +0000 (08:47 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2430 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesTest.java [new file with mode: 0644]

index 171e0b2a16a023399854ad3410759ed64391c42b..316633a80decb1f50f548b22b6423fc04d2e1a74 100644 (file)
@@ -3,53 +3,93 @@ package org.argeo.slc.core.execution;
 import java.io.File;
 import java.text.SimpleDateFormat;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.execution.ExecutionContext;
-import org.springframework.beans.factory.InitializingBean;
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.core.io.Resource;
+import org.springframework.util.Assert;
+
+public class FileExecutionResources implements ExecutionResources {
+       private final static Log log = LogFactory
+                       .getLog(FileExecutionResources.class);
+       protected final static String DEFAULT_EXECUTION_RESOURCES_DIRNAME = "executionResources";
 
-public class FileExecutionResources implements ExecutionResources,
-               InitializingBean {
        private File baseDir;
        private ExecutionContext executionContext;
        private String prefixDatePattern = "yyyyMMdd_HHmmss_";
        private SimpleDateFormat sdf = null;
 
-       public void afterPropertiesSet() throws Exception {
-               if (sdf == null)
-                       sdf = new SimpleDateFormat(prefixDatePattern);
+       public FileExecutionResources() {
+               // Default base directory
+               String osgiInstanceArea = System.getProperty("osgi.instance.area");
+               String osgiInstanceAreaDefault = System
+                               .getProperty("osgi.instance.area.default");
+               String tempDir = System.getProperty("java.io.tmpdir");
 
-               if (baseDir == null) {
-                       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");
-                       }
+               if (osgiInstanceArea != null) {
+                       // within OSGi with -data specified
+                       osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
+                       baseDir = new File(osgiInstanceArea + File.separator
+                                       + DEFAULT_EXECUTION_RESOURCES_DIRNAME);
+               } else if (osgiInstanceAreaDefault != null) {
+                       // within OSGi without -data specified
+                       osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
+                       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);
                }
        }
 
+       protected SimpleDateFormat sdf() {
+               // Lazy init in case prefix has been externally set
+               if (sdf == null)
+                       sdf = new SimpleDateFormat(prefixDatePattern);
+               return sdf;
+       }
+
        public Resource getWritableResource(String relativePath) {
                File file = getFile(relativePath);
-               file.getParentFile().mkdirs();
-               return new FileSystemResource(file);
+               File parentDir = file.getParentFile();
+               if (!parentDir.exists()) {
+                       if (log.isTraceEnabled())
+                               log.trace("Creating parent directory " + parentDir);
+                       parentDir.mkdirs();
+               }
+               Resource resource = new FileSystemResource(file);
+               if (log.isTraceEnabled())
+                       log.trace("Returns writable resource " + resource);
+               return resource;
        }
 
        public File getFile(String relativePath) {
-               File executionDir = new File(baseDir.getPath() + File.separator
-                               + sdf.format(executionContext.getCreationDate())
-                               + executionContext.getUuid());
-               if (!executionDir.exists())
+               Assert.notNull(executionContext, "execution context is null");
+
+               String path = baseDir.getPath() + File.separator
+                               + sdf().format(executionContext.getCreationDate())
+                               + executionContext.getUuid();
+               File executionDir = new File(path);
+
+               // Creates if necessary
+               if (!executionDir.exists()) {
+                       if (log.isDebugEnabled())
+                               log.debug("Creating execution directory " + executionDir);
                        executionDir.mkdirs();
-               return new File(executionDir.getPath() + File.separator + relativePath);
+               }
+
+               return new File(executionDir.getPath() + File.separator
+                               + relativePath.replace('/', File.separatorChar));
+       }
+
+       protected String removeFilePrefix(String url) {
+               if (url.startsWith("file:"))
+                       return url.substring("file:".length());
+               else if (url.startsWith("reference:file:"))
+                       return url.substring("reference:file:".length());
+               else
+                       return url;
        }
 
        public void setBaseDir(File baseDir) {
@@ -64,8 +104,15 @@ public class FileExecutionResources implements ExecutionResources,
                this.prefixDatePattern = prefixDatePattern;
        }
 
-       public void setSdf(SimpleDateFormat sdf) {
-               this.sdf = sdf;
+       public File getBaseDir() {
+               return baseDir;
+       }
+
+       public ExecutionContext getExecutionContext() {
+               return executionContext;
        }
 
+       public String getPrefixDatePattern() {
+               return prefixDatePattern;
+       }
 }
diff --git a/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesTest.java b/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/FileExecutionResourcesTest.java
new file mode 100644 (file)
index 0000000..d1e4ec5
--- /dev/null
@@ -0,0 +1,39 @@
+package org.argeo.slc.core.execution;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.FileUtils;
+import org.argeo.slc.execution.ExecutionContext;
+import org.springframework.core.io.Resource;
+
+public class FileExecutionResourcesTest extends TestCase {
+       public void testGetWritableFile() throws Exception {
+               FileExecutionResources executionResources = new FileExecutionResources();
+               ExecutionContext executionContext = new MapExecutionContext();
+               executionResources.setExecutionContext(executionContext);
+
+               String expected = "TEST";
+               String reached = "";
+               try {
+                       // Resource
+                       Resource resource = executionResources
+                                       .getWritableResource("subdir/textRes.txt");
+                       FileUtils.writeStringToFile(resource.getFile(), expected);
+                       reached = FileUtils.readFileToString(resource.getFile());
+                       assertEquals(expected, reached);
+
+                       // File
+                       File file = executionResources.getFile("subdir/textFile.txt");
+                       FileUtils.writeStringToFile(file, expected);
+                       reached = FileUtils.readFileToString(file);
+                       assertEquals(expected, reached);
+               } finally {
+                       if (executionResources.getBaseDir() != null
+                                       && executionResources.getBaseDir().exists())
+                               FileUtils.deleteDirectory(executionResources.getBaseDir());
+               }
+
+       }
+}