]> 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
Remove eclipse project definitions
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
index 316633a80decb1f50f548b22b6423fc04d2e1a74..361cede76ee3e17979433316a748e0495e056ef6 100644 (file)
@@ -1,10 +1,16 @@
 package org.argeo.slc.core.execution;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.text.SimpleDateFormat;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.SlcException;
 import org.argeo.slc.execution.ExecutionContext;
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.core.io.Resource;
@@ -14,18 +20,25 @@ 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) {
                        // within OSGi with -data specified
@@ -38,8 +51,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);
                }
        }
 
@@ -53,34 +65,72 @@ public class FileExecutionResources implements ExecutionResources {
        public Resource getWritableResource(String relativePath) {
                File file = getFile(relativePath);
                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) {
-               Assert.notNull(executionContext, "execution context is null");
-
-               String path = baseDir.getPath() + File.separator
-                               + sdf().format(executionContext.getCreationDate())
-                               + executionContext.getUuid();
-               File executionDir = new File(path);
+       public String getAsOsPath(Resource resource, Boolean overwrite) {
+               File file = null;
+               try {
+                       file = resource.getFile();
+                       return file.getCanonicalPath();
+               } catch (IOException e) {
+                       if (log.isTraceEnabled())
+                               log
+                                               .trace("Resource "
+                                                               + resource
+                                                               + " is not available on the file system. Retrieving it...");
+               }
 
-               // Creates if necessary
-               if (!executionDir.exists()) {
+               InputStream in = null;
+               OutputStream out = null;
+               try {
+                       String path = resource.getURL().getPath();
+                       file = getFile(path);
+                       if (file.exists() && !overwrite)
+                               return file.getCanonicalPath();
+
+                       file.getParentFile().mkdirs();
+                       in = resource.getInputStream();
+                       out = new FileOutputStream(file);
+                       IOUtils.copy(in, out);
                        if (log.isDebugEnabled())
-                               log.debug("Creating execution directory " + executionDir);
-                       executionDir.mkdirs();
+                               log.debug("Retrieved " + resource + " to OS file " + file);
+                       return file.getCanonicalPath();
+               } catch (IOException e) {
+                       throw new SlcException("Could not make resource " + resource
+                                       + " an OS file.", e);
+               } finally {
+                       IOUtils.closeQuietly(in);
+                       IOUtils.closeQuietly(out);
                }
+       }
+
+       public File getFile(String relativePath) {
 
-               return new File(executionDir.getPath() + File.separator
-                               + relativePath.replace('/', File.separatorChar));
+               if (withExecutionSubdirectory) {
+                       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));
+               } else {
+                       return new File(baseDir.getPath() + File.separator
+                                       + relativePath.replace('/', File.separatorChar));
+               }
        }
 
        protected String removeFilePrefix(String url) {
@@ -115,4 +165,10 @@ public class FileExecutionResources implements ExecutionResources {
        public String getPrefixDatePattern() {
                return prefixDatePattern;
        }
+
+       /** Default is true. */
+       public void setWithExecutionSubdirectory(Boolean withExecutionSubdirectory) {
+               this.withExecutionSubdirectory = withExecutionSubdirectory;
+       }
+
 }