]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
@update:81; Ability to copy the content of resources locally, thus enabling launchyin...
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 9 Jun 2009 13:00:16 +0000 (13:00 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 9 Jun 2009 13:00:16 +0000 (13:00 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2519 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionResources.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/FileExecutionResources.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/MapExecutionContext.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/OsFileFactoryBean.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ResourceSpecAttribute.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/defaults.xml [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/spring.xml
runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/templates.xml

index f10de57dd82c73c6aad0b2ef18a389bbcf177044..53f5af79af837377fe9f409863813af7c75d7b91 100644 (file)
@@ -4,4 +4,6 @@ import org.springframework.core.io.Resource;
 
 public interface ExecutionResources {
        public Resource getWritableResource(String relativePath);
+
+       public String getAsOsPath(Resource resource, Boolean overwrite);
 }
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");
 
index 5d26c52b42fd3d0bbda91618805173d8e9bff9ef..f38342662852747f4f3aeba405e39bb813907010 100644 (file)
@@ -12,8 +12,6 @@ import org.argeo.slc.SlcException;
 import org.argeo.slc.execution.ExecutionContext;
 import org.argeo.slc.execution.ExecutionFlow;
 import org.argeo.slc.execution.ExecutionSpecAttribute;
-import org.argeo.slc.process.SlcExecution;
-import org.springframework.beans.factory.ObjectFactory;
 
 public class MapExecutionContext implements ExecutionContext {
        private final static Log log = LogFactory.getLog(MapExecutionContext.class);
@@ -23,10 +21,16 @@ public class MapExecutionContext implements ExecutionContext {
        // TODO: make it thread safe?
        private final Map<String, Object> variables = new HashMap<String, Object>();
 
-       private final String uuid = UUID.randomUUID().toString();
+       private final String uuid;
 
        private final Date creationDate = new Date();
 
+       public MapExecutionContext() {
+               uuid = UUID.randomUUID().toString();
+               if (log.isDebugEnabled())
+                       log.debug("Execution context #" + uuid + " instantiated.");
+       }
+
        public void addVariables(
                        Map<? extends String, ? extends Object> variablesToAdd) {
                variables.putAll(variablesToAdd);
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/OsFileFactoryBean.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/OsFileFactoryBean.java
new file mode 100644 (file)
index 0000000..b566e57
--- /dev/null
@@ -0,0 +1,48 @@
+package org.argeo.slc.core.execution;
+
+import java.io.File;
+
+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 OsFileFactoryBean implements FactoryBean {
+       private ExecutionResources executionResources;
+       private Resource resource;
+       private Boolean overwrite = false;
+
+       /** Return an existing file on the fiel system. */
+       public Object getObject() throws Exception {
+               Assert.notNull(executionResources, "executionResources is null");
+               Assert.notNull(resource, "relativePath is null");
+               return executionResources.getAsOsPath(resource, overwrite);
+       }
+
+       public Class<? extends Object> getObjectType() {
+               return File.class;
+       }
+
+       public boolean isSingleton() {
+               return true;
+       }
+
+       /** The execution resources object. */
+       public void setExecutionResources(ExecutionResources executionResources) {
+               this.executionResources = executionResources;
+       }
+
+       /** The resource to access. */
+       public void setResource(Resource resource) {
+               this.resource = resource;
+       }
+
+       /**
+        * Whether to overwrite the resource if it already exists. Default is
+        * <code>false</code>.
+        */
+       public void setOverwrite(Boolean overwrite) {
+               this.overwrite = overwrite;
+       }
+
+}
index 572665dd52211b195200404518d9340988974346..8eecc0b47580a1884aa860a887cf93d9c10605eb 100644 (file)
@@ -2,10 +2,12 @@ package org.argeo.slc.core.execution;
 
 import java.io.IOException;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
 import org.springframework.core.io.Resource;
 
-// TODO: offer the ability to read the resource
+/** @deprecated */
 public class ResourceSpecAttribute extends AbstractSpecAttribute {
        public final static String TYPE_PATH = "path";
        public final static String TYPE_URL = "url";
@@ -14,6 +16,15 @@ public class ResourceSpecAttribute extends AbstractSpecAttribute {
        private Resource resource;
        private String type = TYPE_PATH;
 
+       private final static Log log = LogFactory
+                       .getLog(ResourceSpecAttribute.class);
+
+       public ResourceSpecAttribute() {
+               log
+                               .warn(getClass()
+                                               + " is deprecated and will soon be removed. Please use slcDefault.executionResources instead.");
+       }
+
        public Object getValue() {
                return convertResource(resource);
        }
index 988b6b85330dd9a8ecbf65181e33b1ee2600ca09..2bb835b24accc14999f675f74fbec86cbf05382e 100644 (file)
@@ -1,13 +1,6 @@
 package org.argeo.slc.core.execution.tasks;
 
-import java.io.BufferedReader;
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -21,17 +14,13 @@ import org.apache.commons.exec.Executor;
 import org.apache.commons.exec.LogOutputStream;
 import org.apache.commons.exec.PumpStreamHandler;
 import org.apache.commons.exec.ShutdownHookProcessDestroyer;
-import org.apache.commons.exec.launcher.CommandLauncher;
-import org.apache.commons.exec.launcher.CommandLauncherFactory;
-import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.NotImplementedException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
 
+/** Execute and OS system call. */
 public class SystemCall implements Runnable {
-       // TODO: specify environment variables
-
        private final Log log = LogFactory.getLog(getClass());
 
        private String execDir;
@@ -40,7 +29,6 @@ public class SystemCall implements Runnable {
        private List<String> command = null;
 
        private Boolean synchronous = true;
-       // private Boolean captureStdIn = false;
 
        private String stdErrLogLevel = "ERROR";
        private String stdOutLogLevel = "INFO";
@@ -67,8 +55,6 @@ public class SystemCall implements Runnable {
                                dir = new File(execDir).getCanonicalFile();
                        }
 
-                       // Process process = null;
-
                        // Check if an OS specific command overrides
                        String osName = System.getProperty("os.name");
                        List<String> commandToUse = null;
@@ -117,7 +103,6 @@ public class SystemCall implements Runnable {
                                                        + getUsedDir(dir));
 
                                commandLine = CommandLine.parse(cmdToUse);
-                               // process = Runtime.getRuntime().exec(cmdToUse, null, dir);
                        } else if (commandToUse != null) {
                                if (log.isTraceEnabled())
                                        log.trace("Execute '" + commandToUse + "' in "
@@ -128,10 +113,6 @@ public class SystemCall implements Runnable {
                                commandLine = new CommandLine(commandToUse.get(0));
                                for (int i = 1; i < commandToUse.size(); i++)
                                        commandLine.addArgument(commandToUse.get(i));
-                               // ProcessBuilder processBuilder = new
-                               // ProcessBuilder(commandToUse);
-                               // processBuilder.directory(dir);
-                               // process = processBuilder.start();
                        } else {
                                // all cases covered previously
                                throw new NotImplementedException();
@@ -145,7 +126,9 @@ public class SystemCall implements Runnable {
                        ExecuteResultHandler executeResultHandler = new ExecuteResultHandler() {
 
                                public void onProcessComplete(int exitValue) {
-                                       log.info("Process " + commandLine + " properly completed.");
+                                       if (log.isDebugEnabled())
+                                               log.debug("Process " + commandLine
+                                                               + " properly completed.");
                                }
 
                                public void onProcessFailed(ExecuteException e) {
@@ -165,35 +148,6 @@ public class SystemCall implements Runnable {
                        else
                                executor.execute(commandLine, environmentVariablesToUse,
                                                executeResultHandler);
-
-                       // Manage standard streams
-                       // StreamReaderThread stdOutThread = new StreamReaderThread(process
-                       // .getInputStream()) {
-                       // protected void callback(String line) {
-                       // stdOutCallback(line);
-                       // }
-                       // };
-                       // stdOutThread.start();
-                       // StreamReaderThread stdErrThread = new StreamReaderThread(process
-                       // .getErrorStream()) {
-                       // protected void callback(String line) {
-                       // stdErrCallback(line);
-                       // }
-                       // };
-                       // stdErrThread.start();
-                       // if (captureStdIn)
-                       // new StdInThread(process.getOutputStream()).start();
-                       //
-                       // // Wait for the end of the process
-                       // if (synchronous) {
-                       // Integer exitCode = process.waitFor();
-                       // if (exitCode != 0) {
-                       // Thread.sleep(5000);// leave the process a chance to log
-                       // log.warn("Process return exit code " + exitCode);
-                       // }
-                       // } else {
-                       // // asynchronous: return
-                       // }
                } catch (Exception e) {
                        throw new SlcException("Could not execute command " + cmd, e);
                }
@@ -211,14 +165,6 @@ public class SystemCall implements Runnable {
                        return dir.getPath();
        }
 
-       // protected void stdOutCallback(String line) {
-       // log(stdOutLogLevel, line);
-       // }
-       //
-       // protected void stdErrCallback(String line) {
-       // log(stdErrLogLevel, line);
-       // }
-       //
        protected void log(String logLevel, String line) {
                if ("ERROR".equals(logLevel))
                        log.error(line);
@@ -254,10 +200,6 @@ public class SystemCall implements Runnable {
                this.synchronous = synchronous;
        }
 
-       // public void setCaptureStdIn(Boolean captureStdIn) {
-       // this.captureStdIn = captureStdIn;
-       // }
-
        public void setCommand(List<String> command) {
                this.command = command;
        }
@@ -270,69 +212,12 @@ public class SystemCall implements Runnable {
                this.osCmds = osCmds;
        }
 
-       // protected abstract class StreamReaderThread extends Thread {
-       // private final InputStream stream;
-       //
-       // public StreamReaderThread(InputStream stream) {
-       // this.stream = stream;
-       // }
-       //
-       // @Override
-       // public void run() {
-       // BufferedReader in = null;
-       // try {
-       // in = new BufferedReader(new InputStreamReader(stream));
-       // String line = null;
-       // while ((line = in.readLine()) != null) {
-       // stdOutCallback(line);
-       // }
-       // } catch (IOException e) {
-       // if (log.isTraceEnabled()) {
-       // log.trace("Could not read stream", e);
-       // // catch silently
-       // // because the other methods
-       // // to check whether the stream
-       // // is closed would probably
-       // // be to costly
-       // }
-       // } finally {
-       // if (synchronous)
-       // IOUtils.closeQuietly(in);
-       // }
-       // }
-       //
-       // protected abstract void callback(String line);
-       // }
-       //
-       // protected class StdInThread extends Thread {
-       // private final OutputStream stream;
-       //
-       // public StdInThread(OutputStream stream) {
-       // this.stream = stream;
-       // }
-       //
-       // @Override
-       // public void run() {
-       // BufferedReader in = null;
-       // Writer out = null;
-       // try {
-       // out = new OutputStreamWriter(stream);
-       // in = new BufferedReader(new InputStreamReader(System.in));
-       // String line = null;
-       // while ((line = in.readLine()) != null) {
-       // out.write(line);
-       // out.write("\n");
-       // out.flush();
-       // }
-       // } catch (IOException e) {
-       // throw new SlcException("Could not write to stdin stream", e);
-       // } finally {
-       // if (synchronous) {
-       // IOUtils.closeQuietly(in);
-       // IOUtils.closeQuietly(out);
-       // }
-       // }
-       // }
-       //
-       // }
+       public void setEnvironmentVariables(Map<String, String> environmentVariables) {
+               this.environmentVariables = environmentVariables;
+       }
+
+       public void setWatchdogTimeout(Long watchdogTimeout) {
+               this.watchdogTimeout = watchdogTimeout;
+       }
+
 }
diff --git a/runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/defaults.xml b/runtime/org.argeo.slc.support.simple/src/main/resources/org/argeo/slc/core/execution/defaults.xml
new file mode 100644 (file)
index 0000000..09e7a5c
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
+
+       <bean id="slcDefault.executionResources" parent="slcTemplate.fileResources"
+               lazy-init="true">
+               <property name="executionContext" ref="executionContext" />
+       </bean>
+
+       <bean id="slcDefault.writableResource" parent="slcTemplate.writableResource"
+               abstract="true">
+               <property name="executionResources" ref="slcDefault.executionResources" />
+       </bean>
+
+       <bean id="slcDefault.osFile" parent="slcTemplate.osFile" abstract="true">
+               <property name="executionResources" ref="slcDefault.executionResources" />
+       </bean>
+</beans>
\ No newline at end of file
index 663ea3cb5625dd68049e41ae721f8f55a6622fc6..3cfac026b5806f98429dd9c4e30661d9b5a6196a 100644 (file)
@@ -9,6 +9,7 @@
 
        <import resource="specs.xml" />
        <import resource="templates.xml" />
+       <import resource="defaults.xml" />
        <import resource="tasks/core.xml" />
 
        <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
index 5f022268d96dc1047ffb5e6db42c748658333778..4566adf62922eff98884cd09d6f3606dbfc2d5c9 100644 (file)
@@ -7,8 +7,14 @@
        <bean id="slcTemplate.fileResources" class="org.argeo.slc.core.execution.FileExecutionResources"
                abstract="true" />
 
-       <bean id="slcTemplate.resourcesFactoryBean"
+       <bean id="slcTemplate.resourcesFactoryBean" parent="slcTemplate.writableResource"
+               abstract="true" />
+
+       <bean id="slcTemplate.writableResource"
                class="org.argeo.slc.core.execution.ExecutionResourcesFactoryBean"
                abstract="true" />
 
+       <bean id="slcTemplate.osFile" class="org.argeo.slc.core.execution.OsFileFactoryBean"
+               abstract="true" />
+
 </beans>
\ No newline at end of file