]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.agent/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
Improve logging
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / ant / AntSlcApplication.java
index 3638340e16cb8fa3eaea8fa6b863e3597adc7b8c..aac37a3b1fbe8b73ea771b8ead8edf56e9b508ff 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;
@@ -10,55 +9,65 @@ import java.util.Properties;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
+import org.apache.commons.io.IOUtils;
 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.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.SlcRuntime;
-import org.springframework.beans.factory.BeanDefinitionStoreException;
+import org.argeo.slc.logging.Log4jUtils;
+import org.argeo.slc.runtime.SlcExecutionContext;
+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.Resource;
+import org.springframework.util.SystemPropertyUtils;
 
 public class AntSlcApplication {
-       private final static Log log = LogFactory.getLog(AntSlcApplication.class);
+       private final static String DEFAULT_APP_LOG4J_PROPERTIES = "org/argeo/slc/ant/defaultAppLog4j.properties";
 
-       private SlcRuntime slcRuntime;
+       private final static Log log = LogFactory.getLog(AntSlcApplication.class);
 
        private Resource contextLocation;
+       private ApplicationContext parentContext;
 
        private Resource rootDir;
        private Resource confDir;
-       private Resource workDir;
+       private File workDir;
 
-       public void execute(SlcExecution slcExecution, Properties properties,
-                       Map<String, Object> references) {
+       public SlcExecutionContext execute(SlcExecution slcExecution,
+                       Properties properties, Map<String, Object> references) {
+
+               // Properties and app logging initialization
+               initSystemProperties(properties);
+               Log4jUtils.initLog4j("classpath:" + DEFAULT_APP_LOG4J_PROPERTIES);
+
+               log.info("\n###\n### Start SLC execution " + slcExecution.getUuid()
+                               + "\n###\n");
                if (log.isDebugEnabled()) {
-                       log.debug("### Start SLC execution " + slcExecution.getUuid()
-                                       + " ###");
                        log.debug("rootDir=" + rootDir);
                        log.debug("confDir=" + confDir);
                        log.debug("workDir=" + workDir);
                }
 
+               // Spring initialization
+               ConfigurableApplicationContext ctx = createExecutionContext();
+
                // Ant coordinates
                Resource script = findAntScript(slcExecution);
                List<String> targets = findAntTargets(slcExecution);
 
-               ConfigurableApplicationContext ctx = createExecutionContext();
-
+               // 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);
                initProject(project, properties, references);
@@ -66,6 +75,10 @@ public class AntSlcApplication {
 
                initStructure(project, script);
                runProject(project, targets);
+
+               ctx.close();
+
+               return executionContext;
        }
 
        protected Resource findAntScript(SlcExecution slcExecution) {
@@ -75,25 +88,7 @@ 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;
-
-                       throw new SlcAntException("Cannot find Ant script " + scriptStr);
+                       return rootDir.createRelative(scriptStr);
                } catch (Exception e) {
                        throw new SlcAntException("Cannot find Ant script " + scriptStr, e);
                }
@@ -112,6 +107,62 @@ public class AntSlcApplication {
                return targets;
        }
 
+       protected void initSystemProperties(Properties userProperties) {
+               // Set user properties as system properties so that Spring can access
+               // them
+               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) {
+                       System.setProperty(SlcAntConstants.DEFAULT_TEST_RUN_PROPERTY,
+                                       "defaultTestRun");
+               }
+
+               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
+                                               .getCanonicalPath());
+
+                       // Additional properties in slc.properties file. Already set sytem
+                       // properties (such as the various directories) can be resolved in
+                       // placeholders.
+                       if (confDir != null) {
+                               Resource slcPropertiesRes = confDir
+                                               .createRelative("slc.properties");
+                               if (slcPropertiesRes.exists()) {
+                                       Properties slcProperties = new Properties();
+                                       InputStream in = slcPropertiesRes.getInputStream();
+                                       try {
+                                               slcProperties.load(in);
+                                       } finally {
+                                               IOUtils.closeQuietly(in);
+                                       }
+
+                                       for (String key : slcProperties.stringPropertyNames()) {
+                                               if (!System.getProperties().containsKey(key)) {
+                                                       String value = SystemPropertyUtils
+                                                                       .resolvePlaceholders(slcProperties
+                                                                                       .getProperty(key));
+                                                       System.setProperty(key, value);
+                                               }
+                                       }
+                               }
+                       }
+               } catch (Exception e) {
+                       throw new SlcAntException("Cannot init system properties.", e);
+               }
+       }
+
        protected ConfigurableApplicationContext createExecutionContext() {
                try {
                        if (confDir != null && contextLocation == null) {
@@ -120,12 +171,19 @@ public class AntSlcApplication {
                        }
 
                        GenericApplicationContext ctx = new GenericApplicationContext(
-                                       slcRuntime.getRuntimeContext());
+                                       parentContext);
                        if (contextLocation != null && contextLocation.exists()) {
                                XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
                                                ctx);
                                xmlReader.loadBeanDefinitions(contextLocation);
                        }
+
+                       // Add property place holder
+                       PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
+                       ppc.setIgnoreUnresolvablePlaceholders(true);
+                       ctx.addBeanFactoryPostProcessor(ppc);
+
+                       ctx.refresh();
                        return ctx;
                } catch (Exception e) {
                        throw new SlcAntException(
@@ -206,7 +264,6 @@ public class AntSlcApplication {
                        }
                        log.debug("scriptPath=" + scriptPath);
 
-                       List<String> dirNames = new Vector<String>();
                        StringTokenizer st = new StringTokenizer(scriptPath, "/");
                        TreeSPath currPath = null;
                        while (st.hasMoreTokens()) {
@@ -234,30 +291,6 @@ public class AntSlcApplication {
                        throw new SlcAntException("Cannot inititalize execution structure",
                                        e);
                }
-
-               /*
-                * File rootDir = new File(project
-                * .getUserProperty(SlcAntConstants.ROOT_DIR_PROPERTY))
-                * .getAbsoluteFile(); File baseDir =
-                * project.getBaseDir().getAbsoluteFile(); List<File> dirs = new Vector<File>();
-                * File currentDir = baseDir; do { dirs.add(currentDir); currentDir =
-                * currentDir.getParentFile(); if (log.isTraceEnabled()) log.trace("List " +
-                * currentDir); } while (!currentDir.equals(rootDir.getParentFile())); //
-                * first path is root dir (because of previous algorithm) TreeSPath
-                * currPath = TreeSPath.createRootPath(rootDir.getName()); for (int i =
-                * dirs.size() - 1; i >= 0; i--) { File dir = dirs.get(i); // retrieves
-                * description for this path final String description; if (i == 0) {//
-                * project itself description = project.getDescription() != null &&
-                * !project.getDescription().equals("") ? project .getDescription() :
-                * project.getName(); } else { description = dir.getName(); if
-                * (log.isTraceEnabled()) log.trace("Dir desc " + i + "/" + dirs.size() + ": " +
-                * description); } SimpleSElement element = new
-                * SimpleSElement(description); // creates and register path if
-                * (!dir.equals(rootDir)) {// already set currPath =
-                * currPath.createChild(dir.getName()); } registry.register(currPath,
-                * element); } project.addReference(SlcAntConstants.REF_PROJECT_PATH,
-                * currPath);
-                */
        }
 
        protected void parseProject(Project project, Resource script) {
@@ -302,10 +335,6 @@ public class AntSlcApplication {
                }
        }
 
-       public void setSlcRuntime(SlcRuntime slcRuntime) {
-               this.slcRuntime = slcRuntime;
-       }
-
        public void setContextLocation(Resource contextLocation) {
                this.contextLocation = contextLocation;
        }
@@ -318,8 +347,12 @@ public class AntSlcApplication {
                this.confDir = confDir;
        }
 
-       public void setWorkDir(Resource workDir) {
+       public void setWorkDir(File workDir) {
                this.workDir = workDir;
        }
 
+       public void setParentContext(ApplicationContext parentContext) {
+               this.parentContext = parentContext;
+       }
+
 }