]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Make sure Spring context will be properly closed
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 76c8331178a6f5a02dd7141d0482ec5cd939af71..b6c98ebbc3e44f6c571783fd190c05a0c8ce0872 100644 (file)
@@ -28,10 +28,10 @@ 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.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;
-import org.springframework.context.ApplicationContext;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.support.GenericApplicationContext;
 import org.springframework.core.io.DefaultResourceLoader;
@@ -67,29 +67,32 @@ public class AntSlcApplication {
                        log.debug("workDir=" + workDir);
                }
 
-               // Spring initialization
-               ConfigurableApplicationContext ctx = createExecutionContext(slcExecution);
-
                // Ant coordinates
                String scriptRelativePath = findAntScript(slcExecution);
                List<String> targets = findAntTargets(slcExecution);
 
+               // Spring initialization
+               ConfigurableApplicationContext ctx = createExecutionContext(slcExecution);
+
                // Ant project initialization
                Project project = new Project();
                AntExecutionContext executionContext = new AntExecutionContext(project);
                project.addReference(AntConstants.REF_ROOT_CONTEXT, ctx);
                project.addReference(AntConstants.REF_SLC_EXECUTION, slcExecution);
-               initProject(project, properties, references);
-               parseProject(project, scriptRelativePath);
 
-               // Execute project
-               initStructure(project, scriptRelativePath);
-               runProject(project, targets);
+               try {
+                       initProject(project, properties, references);
+                       parseProject(project, scriptRelativePath);
 
-               if (executionOutput != null)
-                       executionOutput.postExecution(executionContext);
+                       // Execute project
+                       initStructure(project, scriptRelativePath);
+                       runProject(project, targets);
 
-               ctx.close();
+                       if (executionOutput != null)
+                               executionOutput.postExecution(executionContext);
+               } finally {
+                       ctx.close();
+               }
        }
 
        protected void initSystemProperties(Properties userProperties) {
@@ -248,8 +251,9 @@ public class AntSlcApplication {
                ListableBeanFactory context = (ListableBeanFactory) project
                                .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);
                }