]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Remove unused SlcAntException
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 8771c0d997775900d042a1016351050a672ce29e..8b8d2e4e5e3f564e85f646851f167603c2d6d3fa 100644 (file)
@@ -27,8 +27,7 @@ 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.ListableBeanFactory;
 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -52,10 +51,11 @@ public class AntSlcApplication {
        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,7 +71,7 @@ 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
@@ -80,14 +80,16 @@ public class AntSlcApplication {
                project.addReference(SlcAntConstants.REF_ROOT_CONTEXT, ctx);
                project.addReference(SlcAntConstants.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) {
@@ -142,7 +144,7 @@ public class AntSlcApplication {
                                }
                        }
                } catch (Exception e) {
-                       throw new SlcAntException("Cannot init system properties.", e);
+                       throw new SlcException("Cannot init system properties.", e);
                }
        }
 
@@ -154,18 +156,20 @@ public class AntSlcApplication {
                        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()) {
+                       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,22 +201,18 @@ 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);
                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) {
@@ -299,57 +299,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(SlcAntConstants.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(SlcAntConstants.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 +360,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 +377,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);
                }