]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
Introduce a factory bean to use execution resources
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
index e35ff0014c418bd1eb16e75b0bef814bb41d3f0d..a023fc7f579ba99d30f96a37150752425dc4c443 100644 (file)
 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.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 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");
+               String osgiInstanceAreaDefault = System
+                               .getProperty("osgi.instance.area.default");
+               String tempDir = System.getProperty("java.io.tmpdir");
+
                if (osgiInstanceArea != null) {
-                       if (osgiInstanceArea.startsWith("file:"))
-                               osgiInstanceArea = osgiInstanceArea.substring("file:".length());
+                       // within OSGi with -data specified
+                       osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
                        baseDir = new File(osgiInstanceArea + File.separator
-                                       + "executionResources");
+                                       + 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(DEFAULT_EXECUTION_RESOURCES_TMP_PATH);
                }
+       }
 
-               if (baseDir == null) {
-                       String tempDir = System.getProperty("java.io.tmpdir");
-                       baseDir = new File(tempDir + File.separator
-                                       + "slcExecutionResources");
-               }
+       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()) {
+                       // Creates if necessary
+                       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
-                               + executionContext.getUuid());
-               if (!executionDir.exists())
-                       executionDir.mkdirs();
-               return new File(executionDir.getPath() + File.separator + relativePath);
+               Assert.notNull(executionContext, "execution context is null");
+
+               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) {
+               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) {
@@ -48,4 +108,24 @@ public class FileExecutionResources implements ExecutionResources {
                this.executionContext = executionContext;
        }
 
+       public void setPrefixDatePattern(String prefixDatePattern) {
+               this.prefixDatePattern = prefixDatePattern;
+       }
+
+       public File getBaseDir() {
+               return baseDir;
+       }
+
+       public ExecutionContext getExecutionContext() {
+               return executionContext;
+       }
+
+       public String getPrefixDatePattern() {
+               return prefixDatePattern;
+       }
+
+       public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
+               this.withExecutionSubdirectory = withExecutionSubdirectory;
+       }
+
 }