]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
Adapt web services to lazy loading
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / DefaultSlcRuntime.java
index 74b383eeff0173a0ee0fe44ce0e959824cf271eb..3fc2a5671fced3e77bb54b9fb7c290e84d5a6607 100644 (file)
@@ -12,14 +12,15 @@ import java.util.UUID;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.ant.AntExecutionContext;
 import org.argeo.slc.ant.AntSlcApplication;
-import org.argeo.slc.ant.SlcAntConstants;
-import org.argeo.slc.ant.SlcAntException;
+import org.argeo.slc.ant.AntConstants;
 import org.argeo.slc.core.SlcException;
 import org.argeo.slc.core.process.SlcExecution;
-import org.argeo.slc.runtime.SlcExecutionContext;
+import org.argeo.slc.runtime.SlcExecutionOutput;
 import org.argeo.slc.spring.SpringUtils;
 import org.springframework.core.io.DefaultResourceLoader;
+import org.springframework.core.io.FileSystemResource;
 import org.springframework.core.io.Resource;
 
 public class DefaultSlcRuntime {
@@ -27,23 +28,69 @@ public class DefaultSlcRuntime {
 
        public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
 
-       public SlcExecutionContext executeScript(Resource script,
-                       Properties properties, Map<String, Object> references) {
+       /**
+        * Simplified execution with default runtime, default target, and no
+        * properties/reference arguments.
+        * 
+        * @param script
+        *            path to the script
+        * @param executionOutput
+        *            output
+        * 
+        * @see #executeScript(String, String, String, Properties, Map,
+        *      SlcExecutionOutput)
+        */
+       public void executeScript(String script,
+                       SlcExecutionOutput<AntExecutionContext> executionOutput) {
+               executeScript(null, script, null, null, null, executionOutput);
+       }
 
-               Resource slcRootFile = findSlcRootFile(script);
-               String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
-                               .getParent(slcRootFile), script);
+       /**
+        * Simplified execution with default runtime, and no properties/reference
+        * arguments.
+        * 
+        * @param script
+        *            path to the script
+        * @param targets
+        *            comma separated list of targets
+        * @param executionOutput
+        *            output
+        * @see #executeScript(String, String, String, Properties, Map,
+        *      SlcExecutionOutput)
+        */
+       public void executeScript(String script, String targets,
+                       SlcExecutionOutput<AntExecutionContext> executionOutput) {
+               executeScript(null, script, targets, null, null, executionOutput);
+       }
 
-               SlcExecution slcExecution = createSlcExecution();
-               slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
-               slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
-                               scriptRelativePath);
+       public void executeScript(String runtime, String script, String targets,
+                       Properties properties, Map<String, Object> references,
+                       SlcExecutionOutput<AntExecutionContext> executionOutput) {
+
+               Resource scriptRes = findScript(script);
+               Resource slcRootFile = findSlcRootFile(scriptRes);
+
+               SlcExecution slcExecution = createSlcExecution(runtime, slcRootFile,
+                               scriptRes, targets);
 
                AntSlcApplication application = getApplication(slcRootFile);
-               return application.execute(slcExecution, properties, references);
+               application.execute(slcExecution, properties, references,
+                               executionOutput);
+       }
+
+       protected Resource findScript(String scriptStr) {
+               Resource scriptRes;
+               if (new File(scriptStr).exists()) {
+                       scriptRes = new FileSystemResource(scriptStr);
+               } else {
+                       scriptRes = new DefaultResourceLoader(SlcMain.class
+                                       .getClassLoader()).getResource(scriptStr);
+               }
+               return scriptRes;
        }
 
-       protected SlcExecution createSlcExecution() {
+       protected SlcExecution createSlcExecution(String runtimeStr,
+                       Resource slcRootFile, Resource script, String targets) {
                SlcExecution slcExecution = new SlcExecution();
                slcExecution.setUuid(UUID.randomUUID().toString());
                try {
@@ -52,9 +99,23 @@ public class DefaultSlcRuntime {
                        slcExecution.setHost(SlcExecution.UNKOWN_HOST);
                }
 
-               slcExecution.setType(SlcAntConstants.EXECTYPE_SLC_ANT);
+               slcExecution.setType(AntConstants.EXECTYPE_SLC_ANT);
 
                slcExecution.setUser(System.getProperty("user.name"));
+
+               if (runtimeStr != null)
+                       slcExecution.getAttributes().put(AntConstants.EXECATTR_RUNTIME,
+                                       runtimeStr);
+               String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
+                               .getParent(slcRootFile), script);
+
+               slcExecution.getAttributes().put(AntConstants.EXECATTR_ANT_FILE,
+                               scriptRelativePath);
+               if (targets != null)
+                       slcExecution.getAttributes().put(
+                                       AntConstants.EXECATTR_ANT_TARGETS, targets);
+
+               slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
                return slcExecution;
        }
 
@@ -76,7 +137,7 @@ public class DefaultSlcRuntime {
 
                        // Conf dir
                        String confDirStr = rootProps
-                                       .getProperty(SlcAntConstants.CONF_DIR_PROPERTY);
+                                       .getProperty(AntConstants.CONF_DIR_PROPERTY);
                        if (confDirStr != null)
                                confDir = new DefaultResourceLoader(application.getClass()
                                                .getClassLoader()).getResource(confDirStr);
@@ -89,7 +150,7 @@ public class DefaultSlcRuntime {
 
                        // Work dir
                        String workDirStr = rootProps
-                                       .getProperty(SlcAntConstants.WORK_DIR_PROPERTY);
+                                       .getProperty(AntConstants.WORK_DIR_PROPERTY);
                        if (workDirStr != null) {
                                workDir = new File(workDirStr);
                        }
@@ -121,24 +182,6 @@ public class DefaultSlcRuntime {
                } finally {
                        IOUtils.closeQuietly(inRootFile);
                }
-
-               // Properties from the conf dir files
-               // Properties properties = new Properties();
-               // StringTokenizer st = new StringTokenizer(rootProps.getProperty(
-               // PROPERTY_FILE_NAMES_PROPERTY, "slc.properties"), ",");
-               // while (st.hasMoreTokens()) {
-               // String fileName = st.nextToken();
-               // properties.putAll(loadFile(confDir.getAbsolutePath()
-               // + File.separator + fileName));
-               // }
-               //
-               // for (Object o : properties.keySet()) {
-               // String key = o.toString();
-               // if (all.getProperty(key) == null) {// not already set
-               // all.setProperty(key, properties.getProperty(key));
-               // }
-               // }
-               //
        }
 
        /**
@@ -160,27 +203,11 @@ public class DefaultSlcRuntime {
                                } else {
                                        return findSlcRootFile(SpringUtils.getParent(currDir));
                                }
-                               // int indx = currPath.lastIndexOf('/',currPath.length()-1);
-
                        }
                } catch (IOException e) {
                        throw new SlcException("Problem when looking in SLC root file in "
                                        + currDir, e);
                }
-
-               // for (File file : dir.listFiles()) {
-               // if (!file.isDirectory()
-               // && file.getName().equals(SLC_ROOT_FILE_NAME)) {
-               // return file;
-               // }
-               // }
-               //
-               // File parentDir = dir.getParentFile();
-               // if (parentDir == null) {
-               // return null;// stop condition: not found
-               // } else {
-               // return findSlcRootFile(parentDir);
-               // }
        }
 
        /** Loads the content of a file as <code>Properties</code>. */
@@ -189,16 +216,8 @@ public class DefaultSlcRuntime {
                try {
                        p.load(in);
                } catch (IOException e) {
-                       throw new SlcAntException("Cannot read SLC root file", e);
+                       throw new SlcException("Cannot read SLC root file", e);
                }
                return p;
        }
-
-       // private Resource getParentOfFile(Resource file) {
-       // try {
-       // return file.createRelative(".");
-       // } catch (IOException e) {
-       // throw new SlcException("Cannot get parent for resource " + file, e);
-       // }
-       // }
 }