]> 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
@update:81; Ability to copy the content of resources locally, thus enabling launchyin...
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / FileExecutionResources.java
index a023fc7f579ba99d30f96a37150752425dc4c443..e032e6a0c3f69c0efc9426c21b98e7eabf7e4cc9 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;
@@ -74,6 +80,43 @@ public class FileExecutionResources implements ExecutionResources {
                return resource;
        }
 
+       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...");
+               }
+
+               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("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) {
                Assert.notNull(executionContext, "execution context is null");