]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Adapt web services to lazy loading
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 8771c0d997775900d042a1016351050a672ce29e..7c85fc2397ca9d921ccb148ba55951648521208e 100644 (file)
@@ -27,8 +27,8 @@ 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.logging.Log4jUtils;
-import org.argeo.slc.runtime.SlcExecutionContext;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
+import org.argeo.slc.runtime.SlcExecutionOutput;
+import org.springframework.beans.factory.BeanFactoryUtils;
 import org.springframework.beans.factory.ListableBeanFactory;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -46,16 +46,17 @@ public class AntSlcApplication {
        private final static Log log = LogFactory.getLog(AntSlcApplication.class);
 
        private Resource contextLocation;
-       private ApplicationContext parentContext;
+       private ConfigurableApplicationContext parentContext;
 
        private Resource rootDir;
        private Resource confDir;
        private File workDir;
 
-       public SlcExecutionContext execute(SlcExecution slcExecution,
-                       Properties properties, Map<String, Object> references) {
+       public void execute(SlcExecution slcExecution, Properties properties,
+                       Map<String, Object> references,
+                       SlcExecutionOutput<AntExecutionContext> executionOutput) {
 
-               // Properties and app logging initialization
+               // Properties and application logging initialization
                initSystemProperties(properties);
                Log4jUtils.initLog4j("classpath:" + DEFAULT_APP_LOG4J_PROPERTIES);
 
@@ -71,23 +72,25 @@ public class AntSlcApplication {
                ConfigurableApplicationContext ctx = createExecutionContext(slcExecution);
 
                // Ant coordinates
-               Resource script = findAntScript(slcExecution);
+               String scriptRelativePath = findAntScript(slcExecution);
                List<String> targets = findAntTargets(slcExecution);
 
                // Ant project initialization
                Project project = new Project();
                AntExecutionContext executionContext = new AntExecutionContext(project);
-               project.addReference(SlcAntConstants.REF_ROOT_CONTEXT, ctx);
-               project.addReference(SlcAntConstants.REF_SLC_EXECUTION, slcExecution);
+               project.addReference(AntConstants.REF_ROOT_CONTEXT, ctx);
+               project.addReference(AntConstants.REF_SLC_EXECUTION, slcExecution);
                initProject(project, properties, references);
-               parseProject(project, script);
+               parseProject(project, scriptRelativePath);
 
-               initStructure(project, script);
+               // Execute project
+               initStructure(project, scriptRelativePath);
                runProject(project, targets);
 
-               ctx.close();
+               if (executionOutput != null)
+                       executionOutput.postExecution(executionContext);
 
-               return executionContext;
+               ctx.close();
        }
 
        protected void initSystemProperties(Properties userProperties) {
@@ -100,20 +103,20 @@ public class AntSlcApplication {
                        }
                }
 
-               if (System.getProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
-                       System.setProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY,
+               if (System.getProperty(AntConstants.DEFAULT_TEST_RUN_PROPERTY) == null) {
+                       System.setProperty(AntConstants.DEFAULT_TEST_RUN_PROPERTY,
                                        "defaultTestRun");
                }
 
                try {
                        if (rootDir != null)
-                               System.setProperty(SlcAntConstants.ROOT_DIR_PROPERTY, rootDir
+                               System.setProperty(AntConstants.ROOT_DIR_PROPERTY, rootDir
                                                .getURL().toString());
                        if (confDir != null)
-                               System.setProperty(SlcAntConstants.CONF_DIR_PROPERTY, confDir
+                               System.setProperty(AntConstants.CONF_DIR_PROPERTY, confDir
                                                .getURL().toString());
                        if (workDir != null)
-                               System.setProperty(SlcAntConstants.WORK_DIR_PROPERTY, workDir
+                               System.setProperty(AntConstants.WORK_DIR_PROPERTY, workDir
                                                .getCanonicalPath());
 
                        // Additional properties in slc.properties file. Already set sytem
@@ -142,7 +145,7 @@ public class AntSlcApplication {
                                }
                        }
                } catch (Exception e) {
-                       throw new SlcAntException("Cannot init system properties.", e);
+                       throw new SlcException("Cannot init system properties.", e);
                }
        }
 
@@ -153,19 +156,21 @@ public class AntSlcApplication {
                        // Find runtime definition
                        Resource runtimeRes = null;
                        String runtimeStr = slcExecution.getAttributes().get(
-                                       SlcAntConstants.EXECATTR_RUNTIME);
-                       if (runtimeStr != null) {
-                               ResourceLoader rl = new DefaultResourceLoader(getClass()
-                                               .getClassLoader());
-                               try {
-                                       runtimeRes = rl.getResource(runtimeStr);
-                               } catch (Exception e) {
-                                       // silent
-                               }
-                               if (runtimeRes == null || !runtimeRes.exists()) {
+                                       AntConstants.EXECATTR_RUNTIME);
+                       if (runtimeStr == null)
+                               runtimeStr = "default";
+
+                       ResourceLoader rl = new DefaultResourceLoader(getClass()
+                                       .getClassLoader());
+                       try {// tries absolute reference
+                               runtimeRes = rl.getResource(runtimeStr);
+                       } catch (Exception e) {
+                               // silent
+                       }
+                       if (runtimeRes == null || !runtimeRes.exists()) {
+                               if (confDir != null)
                                        runtimeRes = confDir.createRelative("runtime/" + runtimeStr
                                                        + ".xml");
-                               }
                        }
 
                        // Find runtime independent application context definition
@@ -197,27 +202,23 @@ public class AntSlcApplication {
                        ctx.refresh();
                        return ctx;
                } catch (Exception e) {
-                       throw new SlcAntException(
+                       throw new SlcException(
                                        "Cannot create SLC execution application context.", e);
                }
        }
 
-       protected Resource findAntScript(SlcExecution slcExecution) {
+       protected String findAntScript(SlcExecution slcExecution) {
                String scriptStr = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_FILE);
+                               AntConstants.EXECATTR_ANT_FILE);
                if (scriptStr == null)
-                       throw new SlcAntException("No Ant script provided");
+                       throw new SlcException("No Ant script provided");
 
-               try {
-                       return rootDir.createRelative(scriptStr);
-               } catch (Exception e) {
-                       throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
-               }
+               return scriptStr;
        }
 
        protected List<String> findAntTargets(SlcExecution slcExecution) {
                String targetList = slcExecution.getAttributes().get(
-                               SlcAntConstants.EXECATTR_ANT_TARGETS);
+                               AntConstants.EXECATTR_ANT_TARGETS);
                List<String> targets = new Vector<String>();
                if (targetList != null) {
                        StringTokenizer stTargets = new StringTokenizer(targetList, ",");
@@ -246,16 +247,17 @@ public class AntSlcApplication {
                project.addBuildListener(new CommonsLoggingListener());
 
                ListableBeanFactory context = (ListableBeanFactory) project
-                               .getReference(SlcAntConstants.REF_ROOT_CONTEXT);
+                               .getReference(AntConstants.REF_ROOT_CONTEXT);
                // Register build listeners
-               Map<String, BuildListener> listeners = context.getBeansOfType(
-                               BuildListener.class, false, true);
+               Map<String, BuildListener> listeners = BeanFactoryUtils
+                               .beansOfTypeIncludingAncestors(context, BuildListener.class,
+                                               false, false);
                for (BuildListener listener : listeners.values()) {
                        project.addBuildListener(listener);
                }
 
                // Register log4j appenders from context
-               MDC.put(SlcAntConstants.MDC_ANT_PROJECT, project);
+               MDC.put(AntConstants.MDC_ANT_PROJECT, project);
                Map<String, Appender> appenders = context.getBeansOfType(
                                Appender.class, false, true);
                for (Appender appender : appenders.values()) {
@@ -269,7 +271,7 @@ public class AntSlcApplication {
        /** Loads the SLC specific Ant tasks. */
        protected void addCustomTaskAndTypes(Project project) {
                Properties taskdefs = getDefs(project,
-                               SlcAntConstants.SLC_TASKDEFS_RESOURCE_PATH);
+                               AntConstants.SLC_TASKDEFS_RESOURCE_PATH);
                for (Object o : taskdefs.keySet()) {
                        String name = o.toString();
                        try {
@@ -280,7 +282,7 @@ public class AntSlcApplication {
                        }
                }
                Properties typedefs = getDefs(project,
-                               SlcAntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
+                               AntConstants.SLC_TYPEDEFS_RESOURCE_PATH);
                for (Object o : typedefs.keySet()) {
                        String name = o.toString();
                        try {
@@ -299,57 +301,50 @@ public class AntSlcApplication {
                        defs.load(in);
                        in.close();
                } catch (IOException e) {
-                       throw new SlcAntException("Cannot load task definitions", e);
+                       throw new SlcException("Cannot load task definitions", e);
                }
                return defs;
        }
 
-       protected void initStructure(Project project, Resource script) {
+       protected void initStructure(Project project, String scriptRelativePath) {
                // Init structure registry
-               try {
-                       StructureRegistry<TreeSPath> registry = new TreeSRegistry();
-                       project.addReference(SlcAntConstants.REF_STRUCTURE_REGISTRY,
-                                       registry);
-
-                       String scriptPath = script.getURL().getPath();
-                       if (rootDir != null) {
-                               scriptPath = scriptPath.substring(rootDir.getURL().getPath()
-                                               .length());
-                               log.debug("rootDirPath=" + rootDir.getURL().getPath());
-                       }
-                       log.debug("scriptPath=" + scriptPath);
-
-                       StringTokenizer st = new StringTokenizer(scriptPath, "/");
-                       TreeSPath currPath = null;
-                       while (st.hasMoreTokens()) {
-                               String name = st.nextToken();
-                               if (currPath == null) {
-                                       currPath = TreeSPath.createRootPath(name);
-                               } else {
+               StructureRegistry<TreeSPath> registry = new TreeSRegistry();
+               project.addReference(AntConstants.REF_STRUCTURE_REGISTRY, registry);
+
+               // Lowest levels
+               StringTokenizer st = new StringTokenizer(scriptRelativePath, "/");
+               TreeSPath currPath = null;
+               while (st.hasMoreTokens()) {
+                       String name = st.nextToken();
+                       if (currPath == null) {
+                               currPath = TreeSPath.createRootPath(name);
+                       } else {
+                               if (st.hasMoreTokens())// don't register project file
                                        currPath = currPath.createChild(name);
-                               }
-                               registry.register(currPath, new SimpleSElement(name));
                        }
-                       TreeSPath projectPath = currPath
-                                       .createChild(project.getName() != null
-                                                       && !project.getName().equals("") ? project
-                                                       .getName() : "project");
-                       String projectDesc = project.getDescription() != null
-                                       && !project.getDescription().equals("") ? project
-                                       .getDescription() : projectPath.getName();
-                       registry.register(projectPath, new SimpleSElement(projectDesc));
-                       project.addReference(SlcAntConstants.REF_PROJECT_PATH, currPath);
-
-                       if (log.isDebugEnabled())
-                               log.debug("Project path: " + projectPath);
-               } catch (IOException e) {
-                       throw new SlcAntException("Cannot inititalize execution structure",
-                                       e);
+                       registry.register(currPath, new SimpleSElement(name));
                }
+
+               // Project level
+               String projectName = project.getName() != null
+                               && !project.getName().equals("") ? project.getName()
+                               : "project";
+               TreeSPath projectPath = currPath.createChild(projectName);
+
+               String projectDesc = project.getDescription() != null
+                               && !project.getDescription().equals("") ? project
+                               .getDescription() : projectPath.getName();
+
+               registry.register(projectPath, new SimpleSElement(projectDesc));
+               project.addReference(AntConstants.REF_PROJECT_PATH, projectPath);
+
+               if (log.isDebugEnabled())
+                       log.debug("Project path: " + projectPath);
        }
 
-       protected void parseProject(Project project, Resource script) {
+       protected void parseProject(Project project, String scriptRelativePath) {
                try {
+                       Resource script = rootDir.createRelative(scriptRelativePath);
                        File baseDir = null;
                        try {
                                File scriptFile = script.getFile();
@@ -367,8 +362,8 @@ public class AntSlcApplication {
                                        projectHelper);
                        projectHelper.parse(project, script.getURL());
                } catch (Exception e) {
-                       throw new SlcAntException("Could not parse project for script "
-                                       + script, e);
+                       throw new SlcException("Could not parse project for script "
+                                       + scriptRelativePath, e);
                }
 
        }
@@ -384,7 +379,7 @@ public class AntSlcApplication {
                        }
                } catch (Throwable e) {
                        exception = e;
-                       throw new SlcAntException("SLC Ant execution failed", exception);
+                       throw new SlcException("SLC Ant execution failed", exception);
                } finally {
                        p.fireBuildFinished(exception);
                }
@@ -406,7 +401,7 @@ public class AntSlcApplication {
                this.workDir = workDir;
        }
 
-       public void setParentContext(ApplicationContext runtimeContext) {
+       public void setParentContext(ConfigurableApplicationContext runtimeContext) {
                this.parentContext = runtimeContext;
        }