]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Simplify new runtime
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2008 13:25:33 +0000 (13:25 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 20 Jun 2008 13:25:33 +0000 (13:25 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1257 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/spring/OverrideArg.java
org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/DefaultSlcRuntime.java
org.argeo.slc.agent/src/test/java/org/argeo/slc/ant/SlcAntTest.java

index 1a69c493bf7509eac155dfbc3d4e4e10ab15f8da..b312ad84a09b1de4973f57fc11aba63d7a5b5992 100644 (file)
@@ -1,7 +1,6 @@
 package org.argeo.slc.ant;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.List;
@@ -12,77 +11,32 @@ import java.util.Vector;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.ProjectHelper;
 import org.apache.tools.ant.helper.ProjectHelper2;
 import org.apache.tools.ant.listener.CommonsLoggingListener;
-import org.argeo.slc.core.SlcException;
 import org.argeo.slc.core.process.SlcExecution;
-import org.argeo.slc.core.structure.DefaultSRegistry;
 import org.argeo.slc.core.structure.SimpleSElement;
 import org.argeo.slc.core.structure.StructureRegistry;
 import org.argeo.slc.core.structure.tree.TreeSPath;
 import org.argeo.slc.core.structure.tree.TreeSRegistry;
 import org.argeo.slc.runtime.SlcExecutionContext;
-import org.argeo.slc.runtime.SlcRuntime;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
-import org.springframework.core.io.FileSystemResource;
 import org.springframework.core.io.Resource;
 
 public class AntSlcApplication {
        private final static Log log = LogFactory.getLog(AntSlcApplication.class);
 
-       private SlcRuntime slcRuntime;
-
        private Resource contextLocation;
-       private ConfigurableApplicationContext context;
+       private ApplicationContext parentContext;
 
        private Resource rootDir;
        private Resource confDir;
        private File workDir;
 
-       public void init() {
-               try {
-                       try {
-                               if (rootDir != null)
-                                       System.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY,
-                                                       rootDir.getURL().toString());
-                               if (confDir != null)
-                                       System.setProperty(SlcAntConstants.CONF_DIR_PROPERTY,
-                                                       confDir.getURL().toString());
-                       } catch (IOException e) {
-                               throw new SlcAntException("Cannot interpret dir as URL.", e);
-                       }
-                       if (workDir != null)
-                               System.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, workDir
-                                               .toString());
-
-                       if (confDir != null && contextLocation == null) {
-                               contextLocation = confDir
-                                               .createRelative("applicationContext.xml");
-                       }
-
-                       GenericApplicationContext ctx = new GenericApplicationContext(
-                                       slcRuntime.getRuntimeContext());
-                       if (contextLocation != null && contextLocation.exists()) {
-                               XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
-                                               ctx);
-                               xmlReader.loadBeanDefinitions(contextLocation);
-                       }
-                       ctx.refresh();
-                       context = ctx;
-               } catch (Exception e) {
-                       throw new SlcAntException(
-                                       "Cannot create SLC app application context.", e);
-               }
-
-       }
-
        public SlcExecutionContext execute(SlcExecution slcExecution,
                        Properties properties, Map<String, Object> references) {
                log.info("### Start SLC execution " + slcExecution.getUuid() + " ###");
@@ -120,35 +74,10 @@ public class AntSlcApplication {
                        throw new SlcAntException("No Ant script provided");
 
                try {
-                       if (log.isTraceEnabled())
-                               log.trace("scriptStr=" + scriptStr);
-                       Resource script = null;
-
-                       if (rootDir != null) {
-                               script = rootDir.createRelative(scriptStr);
-                               if (log.isTraceEnabled())
-                                       log.trace("script(relative)=" + script);
-                               if (script.exists())
-                                       return script;
-                       }
-
-                       script = slcRuntime.getRuntimeContext().getResource(scriptStr);
-                       if (log.isTraceEnabled())
-                               log.trace("script(absolute)=" + script);
-                       if (script.exists())
-                               return script;
-
-                       script = new FileSystemResource(scriptStr);
-                       if (log.isTraceEnabled())
-                               log.trace("script(fs)=" + script);
-                       if (script.exists())
-                               return script;
-
+                       return rootDir.createRelative(scriptStr);
                } catch (Exception e) {
                        throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
                }
-
-               throw new SlcAntException("Cannot find Ant script " + scriptStr);
        }
 
        protected List<String> findAntTargets(SlcExecution slcExecution) {
@@ -168,9 +97,12 @@ public class AntSlcApplication {
                        Properties userProperties) {
                // Set user properties as system properties so that Spring can access
                // them
-               for (Object key : userProperties.keySet()) {
-                       System.setProperty(key.toString(), userProperties.getProperty(key
-                                       .toString()));
+
+               if (userProperties != null) {
+                       for (Object key : userProperties.keySet()) {
+                               System.setProperty(key.toString(), userProperties
+                                               .getProperty(key.toString()));
+                       }
                }
 
                if (System.getProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
@@ -179,8 +111,28 @@ public class AntSlcApplication {
                }
 
                try {
+                       if (rootDir != null)
+                               System.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY, rootDir
+                                               .getURL().toString());
+                       if (confDir != null)
+                               System.setProperty(SlcAntConstants.CONF_DIR_PROPERTY, confDir
+                                               .getURL().toString());
+                       if (workDir != null)
+                               System.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, workDir
+                                               .toString());
+
+                       if (confDir != null && contextLocation == null) {
+                               contextLocation = confDir
+                                               .createRelative("applicationContext.xml");
+                       }
+
                        GenericApplicationContext ctx = new GenericApplicationContext(
-                                       context);
+                                       parentContext);
+                       if (contextLocation != null && contextLocation.exists()) {
+                               XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
+                                               ctx);
+                               xmlReader.loadBeanDefinitions(contextLocation);
+                       }
                        ctx.refresh();
                        return ctx;
                } catch (Exception e) {
@@ -333,10 +285,6 @@ public class AntSlcApplication {
                }
        }
 
-       public void setSlcRuntime(SlcRuntime slcRuntime) {
-               this.slcRuntime = slcRuntime;
-       }
-
        public void setContextLocation(Resource contextLocation) {
                this.contextLocation = contextLocation;
        }
@@ -353,4 +301,8 @@ public class AntSlcApplication {
                this.workDir = workDir;
        }
 
+       public void setParentContext(ApplicationContext parentContext) {
+               this.parentContext = parentContext;
+       }
+
 }
index dcede31b346b88cd315f219e4ad3702b8ee17bed..f39b79baa4231f096607a5f57160f2872f4f8514 100644 (file)
@@ -12,8 +12,6 @@ import org.argeo.slc.core.process.SlcExecution;
 import org.argeo.slc.core.process.SlcExecutionNotifier;\r
 import org.argeo.slc.core.process.SlcExecutionStep;\r
 import org.argeo.slc.ws.process.WebServiceSlcExecutionNotifier;\r
-import org.springframework.context.ApplicationContext;\r
-import org.springframework.context.support.AbstractApplicationContext;\r
 \r
 public class SlcExecutionBuildListener extends AppenderSkeleton implements\r
                ProjectRelatedBuildListener {\r
index 4a2db0b0c4077aaa500ed4ccb8333486d13c8abc..6bd66ef7c512df24b984d697a08e5363639f5eda 100644 (file)
@@ -5,8 +5,6 @@ import java.util.Vector;
 \r
 import org.apache.tools.ant.BuildException;\r
 \r
-import org.argeo.slc.core.SlcException;\r
-\r
 /** Ant type allowing to override bean properties. */\r
 public class OverrideArg extends SpringArg<Object> {\r
        private String name;\r
index 7bb77e578d65ef067475cdc85f3be5a1cb3592bc..74b383eeff0173a0ee0fe44ce0e959824cf271eb 100644 (file)
@@ -1,16 +1,15 @@
 package org.argeo.slc.cli;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.util.Map;
 import java.util.Properties;
-import java.util.StringTokenizer;
 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.AntSlcApplication;
@@ -18,17 +17,12 @@ import org.argeo.slc.ant.SlcAntConstants;
 import org.argeo.slc.ant.SlcAntException;
 import org.argeo.slc.core.SlcException;
 import org.argeo.slc.core.process.SlcExecution;
-import org.argeo.slc.runtime.SimpleSlcRuntime;
 import org.argeo.slc.runtime.SlcExecutionContext;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.BeanFactory;
-import org.springframework.beans.factory.BeanFactoryAware;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.support.GenericApplicationContext;
+import org.argeo.slc.spring.SpringUtils;
+import org.springframework.core.io.DefaultResourceLoader;
 import org.springframework.core.io.Resource;
 
-public class DefaultSlcRuntime extends SimpleSlcRuntime implements
-               BeanFactoryAware {
+public class DefaultSlcRuntime {
        private final static Log log = LogFactory.getLog(DefaultSlcRuntime.class);
 
        public final static String SLC_ROOT_FILE_NAME = "slcRoot.properties";
@@ -36,12 +30,20 @@ public class DefaultSlcRuntime extends SimpleSlcRuntime implements
        public SlcExecutionContext executeScript(Resource script,
                        Properties properties, Map<String, Object> references) {
 
-               if (runtimeContext == null) {
-                       GenericApplicationContext ctx = new GenericApplicationContext();
-                       ctx.refresh();
-                       runtimeContext = ctx;
-               }
+               Resource slcRootFile = findSlcRootFile(script);
+               String scriptRelativePath = SpringUtils.extractRelativePath(SpringUtils
+                               .getParent(slcRootFile), script);
+
+               SlcExecution slcExecution = createSlcExecution();
+               slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
+               slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
+                               scriptRelativePath);
 
+               AntSlcApplication application = getApplication(slcRootFile);
+               return application.execute(slcExecution, properties, references);
+       }
+
+       protected SlcExecution createSlcExecution() {
                SlcExecution slcExecution = new SlcExecution();
                slcExecution.setUuid(UUID.randomUUID().toString());
                try {
@@ -53,52 +55,36 @@ public class DefaultSlcRuntime extends SimpleSlcRuntime implements
                slcExecution.setType(SlcAntConstants.EXECTYPE_SLC_ANT);
 
                slcExecution.setUser(System.getProperty("user.name"));
-               slcExecution.setStatus(SlcExecution.STATUS_RUNNING);
-               try {
-                       slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,
-                                       script.getURL().toString());
-               } catch (IOException e) {
-                       throw new SlcException("Cannot interpret script " + script
-                                       + " as URL.", e);
-               }
-
-               AntSlcApplication application = new AntSlcApplication();
-               prepareApplication(slcExecution, application, script);
-               return application.execute(slcExecution, properties, references);
+               return slcExecution;
        }
 
-       public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
-               runtimeContext = (ApplicationContext) beanFactory;
-       }
-
-       protected void prepareApplication(SlcExecution slcExecution,
-                       AntSlcApplication application, Resource script) {
+       protected AntSlcApplication getApplication(Resource slcRootFile) {
+               AntSlcApplication application = new AntSlcApplication();
+               InputStream inRootFile = null;
                try {
-                       final String fileUrlPrefix = "";
-
-                       Resource slcRootFile = findSlcRootFile(script);
-
                        // Remove basedir property in order to avoid conflict with Maven
                        // if (all.containsKey("basedir"))
                        // all.remove("basedir");
 
-                       InputStream inRootFile = slcRootFile.getInputStream();
+                       inRootFile = slcRootFile.getInputStream();
                        Properties rootProps = loadFile(inRootFile);
 
                        Resource confDir = null;
                        File workDir = null;
                        // Root dir
-                       final Resource rootDir = getParent(slcRootFile);
+                       final Resource rootDir = SpringUtils.getParent(slcRootFile);
 
                        // Conf dir
                        String confDirStr = rootProps
                                        .getProperty(SlcAntConstants.CONF_DIR_PROPERTY);
                        if (confDirStr != null)
-                               confDir = runtimeContext.getResource(confDirStr);
+                               confDir = new DefaultResourceLoader(application.getClass()
+                                               .getClassLoader()).getResource(confDirStr);
 
                        if (confDir == null || !confDir.exists()) {
                                // confDir = rootDir.createRelative("../conf");
-                               confDir = getParent(rootDir).createRelative("conf/");
+                               confDir = SpringUtils.getParent(rootDir)
+                                               .createRelative("conf/");
                        }
 
                        // Work dir
@@ -116,24 +102,24 @@ public class DefaultSlcRuntime extends SimpleSlcRuntime implements
                                } catch (IOException e) {
                                        workDir = new File(System.getProperty("java.io.tmpdir")
                                                        + File.separator + "slcExecutions" + File.separator
-                                                       + slcExecution.getUuid()).getCanonicalFile();
+                                                       + slcRootFile.getURL().getPath());
                                        log.debug("Root dir is not a file: " + e.getMessage()
                                                        + ", creating work dir in temp: " + workDir);
                                }
+                               workDir.mkdirs();
                        }
 
                        application.setConfDir(confDir);
                        application.setRootDir(rootDir);
                        application.setWorkDir(workDir);
 
-                       application.setSlcRuntime(this);
-
-                       application.init();
+                       return application;
                } catch (IOException e) {
                        throw new SlcException(
-                                       "Could not prepare SLC application for SLC execution "
-                                                       + slcExecution.getUuid() + " and script " + script,
-                                       e);
+                                       "Could not prepare SLC application for root file "
+                                                       + slcRootFile, e);
+               } finally {
+                       IOUtils.closeQuietly(inRootFile);
                }
 
                // Properties from the conf dir files
@@ -172,7 +158,7 @@ public class DefaultSlcRuntime extends SimpleSlcRuntime implements
                                if (currPath.equals("/") || currPath.equals("")) {
                                        return null;
                                } else {
-                                       return findSlcRootFile(getParent(currDir));
+                                       return findSlcRootFile(SpringUtils.getParent(currDir));
                                }
                                // int indx = currPath.lastIndexOf('/',currPath.length()-1);
 
@@ -208,22 +194,6 @@ public class DefaultSlcRuntime extends SimpleSlcRuntime implements
                return p;
        }
 
-       private Resource getParent(Resource res) {
-               try {
-                       if (res.getURL().getPath().equals("/"))
-                               return null;
-
-                       String urlStr = res.getURL().toString();
-                       if (urlStr.charAt(urlStr.length() - 1) == '/')
-                               urlStr = urlStr.substring(0, urlStr.length() - 2);
-
-                       String parentUrlStr = urlStr.substring(0, urlStr.lastIndexOf('/'));
-                       return runtimeContext.getResource(parentUrlStr + '/');
-               } catch (IOException e) {
-                       throw new SlcException("Cannot get parent for resource " + res, e);
-               }
-       }
-
        // private Resource getParentOfFile(Resource file) {
        // try {
        // return file.createRelative(".");
index 4cbdb86cb6515338bc59b12f20dfb91a066bd8cf..17f8a60c50f5ef5b4aed09e6fa6efe56adbc04a6 100644 (file)
@@ -1,39 +1,24 @@
 package org.argeo.slc.ant;\r
 \r
 import java.io.File;\r
-import java.net.URL;\r
-\r
-import org.apache.commons.logging.Log;\r
-import org.apache.commons.logging.LogFactory;\r
 \r
 import org.argeo.slc.core.process.SlcExecution;\r
-import org.argeo.slc.runtime.SimpleSlcRuntime;\r
 import org.argeo.slc.unit.AbstractSpringTestCase;\r
 import org.springframework.core.io.FileSystemResource;\r
 \r
 public class SlcAntTest extends AbstractSpringTestCase {\r
-       private Log log = LogFactory.getLog(getClass());\r
+//     private Log log = LogFactory.getLog(getClass());\r
 \r
        public void testSimpleRun() {\r
-               // AntRegistryUtil.runAll(getClass().getResource(\r
-               // "/org/argeo/slc/ant/build.xml"), "test", null);\r
-\r
-               URL url = getClass().getResource("/org/argeo/slc/ant/build.xml");\r
-               log.info("Run Ant file from URL: " + url);\r
-\r
-               // AntRunner antRunner = new AntRunner(getContext(), url, "test");\r
-               // antRunner.run();\r
-\r
                AntSlcApplication slcApp = new AntSlcApplication();\r
-               slcApp.setSlcRuntime(new SimpleSlcRuntime(getContext()));\r
                slcApp.setRootDir(new FileSystemResource(new File("src/test/resources")\r
                                .getAbsolutePath()\r
                                + File.separator));\r
-               slcApp.init();\r
+               slcApp.setParentContext(getContext());\r
 \r
                SlcExecution slcExecution = new SlcExecution();\r
                slcExecution.getAttributes().put(SlcAntConstants.EXECATTR_ANT_FILE,\r
-                               url.toString());\r
+                               "/org/argeo/slc/ant/build.xml");\r
 \r
                slcApp.execute(slcExecution, null, null);\r
        }\r