]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcProjectHelper.java
Introduce end to end testing with logging of results
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / ant / SlcProjectHelper.java
index f0afc92efd7788bb79a9faa807377b9234a8d49b..f17e0d5a3bbf5ead5ca98a69a7a4f6047fb55cac 100644 (file)
 package org.argeo.slc.ant;\r
 \r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.List;\r
+import java.util.Properties;\r
+import java.util.Vector;\r
+\r
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;\r
 import org.springframework.context.ApplicationContext;\r
 import org.springframework.context.support.FileSystemXmlApplicationContext;\r
 \r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
 import org.apache.tools.ant.BuildException;\r
 import org.apache.tools.ant.Project;\r
 import org.apache.tools.ant.helper.ProjectHelperImpl;\r
 \r
-import org.argeo.slc.core.structure.StructurePath;\r
-import org.argeo.slc.core.structure.tree.TreeSElement;\r
+import org.argeo.slc.core.structure.DefaultSRegistry;\r
+import org.argeo.slc.core.structure.SimpleSElement;\r
+import org.argeo.slc.core.structure.StructureRegistry;\r
 import org.argeo.slc.core.structure.tree.TreeSPath;\r
-import org.argeo.slc.core.structure.tree.TreeSRegistry;\r
 \r
+/**\r
+ * Custom implementation of a <code>ProjectHelper</code> binding a Spring\r
+ * application context and a structure registry with the Ant project.\r
+ */\r
 public class SlcProjectHelper extends ProjectHelperImpl {\r
-       public static String PROP_APPLICATION_CONTEXT = "org.argeo.slc.slcRootContext";\r
+       private static Log log = LogFactory.getLog(SlcProjectHelper.class);\r
+\r
        public static String REF_ROOT_CONTEXT = "slcApplicationContext";\r
        public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";\r
+       public static String REF_PROJECT_PATH = "slcProjectPath";\r
+\r
+       private String slcRootFileName = "slcRoot.properties";\r
+       private String slcLocalFileName = "slcLocal.properties";\r
 \r
        @Override\r
        public void parse(Project project, Object source) throws BuildException {\r
-               stdOut("Entered SLC project helper");\r
+               log.debug("Entered SLC project helper");\r
 \r
-               // init Spring application context\r
-               String acPath = System.getProperty(PROP_APPLICATION_CONTEXT,\r
-                               "applicationContext.xml");\r
-               ApplicationContext context = new FileSystemXmlApplicationContext(acPath);\r
-               project.addReference(REF_ROOT_CONTEXT, context);\r
+               // look for root file\r
+               File projectBaseDir = project.getBaseDir();\r
+               File slcRootFile = findSlcRootFile(projectBaseDir);\r
+               if (slcRootFile == null) {\r
+                       throw new SlcAntException("Cannot find SLC root file");\r
+               }\r
+               SlcAntConfig.initProject(project, slcRootFile);\r
 \r
-               // init structure register\r
-               TreeSRegistry registry = new TreeSRegistry();\r
+               // init Spring application context\r
+               initSpringContext(project);\r
+               \r
+               // init structure registry\r
+               DefaultSRegistry registry = new DefaultSRegistry();\r
                project.addReference(REF_STRUCTURE_REGISTRY, registry);\r
 \r
                // call the underlying implementation to do the actual work\r
                super.parse(project, source);\r
 \r
-               String projectDescription = project.getDescription() != null ? project\r
-                               .getDescription() : "Root";\r
-               TreeSElement element = TreeSElement.createRootElelment(\r
-                               getProjectPathName(project), projectDescription);\r
-               registry.register(element);\r
+               // create structure root\r
+               registerProjectAndParents(project);\r
+\r
+               addSlcTasks(project);\r
+\r
        }\r
 \r
-       private static void stdOut(Object o) {\r
-               System.out.println(o);\r
+       private void registerProjectAndParents(Project project) {\r
+               StructureRegistry registry = (StructureRegistry) project\r
+                               .getReference(REF_STRUCTURE_REGISTRY);\r
+               File rootDir = new File(project\r
+                               .getUserProperty(SlcAntConfig.ROOT_DIR_PROPERTY))\r
+                               .getAbsoluteFile();\r
+               File baseDir = project.getBaseDir().getAbsoluteFile();\r
+\r
+               List<File> dirs = new Vector<File>();\r
+               File currentDir = baseDir;\r
+               do {\r
+                       dirs.add(currentDir);\r
+                       currentDir = currentDir.getParentFile();\r
+                       log.trace("List " + currentDir);\r
+               } while (!currentDir.equals(rootDir.getParentFile()));\r
+\r
+               TreeSPath currPath = null;\r
+               for (int i = dirs.size() - 1; i >= 0; i--) {\r
+                       File dir = dirs.get(i);\r
+\r
+                       String description = dir.getName();\r
+                       File slcLocal = new File(dir.getPath() + File.separator\r
+                                       + slcLocalFileName);\r
+                       if (slcLocal.exists()) {\r
+                               Properties properties = SlcAntConfig.loadFile(slcLocal\r
+                                               .getAbsolutePath());\r
+                               description = properties\r
+                                               .getProperty(SlcAntConfig.DIR_DESCRIPTION_PROPERTY);\r
+                       } else {\r
+                               if (i == 0) {// project it self\r
+                                       description = project.getDescription() != null ? project\r
+                                                       .getDescription() : "";\r
+                               }\r
+                       }\r
+                       SimpleSElement element = new SimpleSElement(description);\r
+\r
+                       if (dir.equals(rootDir)) {\r
+                               currPath = TreeSPath.createRootPath(rootDir.getName());\r
+                       } else {\r
+                               currPath = currPath.createChild(dir.getName());\r
+                       }\r
+                       registry.register(currPath, element);\r
+               }\r
+               project.addReference(REF_PROJECT_PATH, currPath);\r
+       }\r
+\r
+       /** Get the path of a project (root). */\r
+       public static TreeSPath getProjectPath(Project project) {\r
+               return (TreeSPath) project.getReference(REF_PROJECT_PATH);\r
+       }\r
+\r
+       private File findSlcRootFile(File dir) {\r
+               for (File file : dir.listFiles()) {\r
+                       if (!file.isDirectory() && file.getName().equals(slcRootFileName)) {\r
+                               return file;\r
+                       }\r
+               }\r
+\r
+               File parentDir = dir.getParentFile();\r
+               if (parentDir == null) {\r
+                       return null;// stop condition: not found\r
+               } else {\r
+                       return findSlcRootFile(parentDir);\r
+               }\r
        }\r
 \r
-       static TreeSPath getProjectPath(Project project) {\r
-               return TreeSPath.createChild(null, getProjectPathName(project));\r
+       private void initSpringContext(Project project) {\r
+               System.getProperties().putAll(project.getProperties());\r
+               String acPath = project\r
+                               .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);\r
+               ApplicationContext context = new FileSystemXmlApplicationContext(acPath);\r
+               project.addReference(REF_ROOT_CONTEXT, context);\r
        }\r
 \r
-       private static String getProjectPathName(Project project) {\r
-               String projectName = project.getName() != null ? project.getName()\r
-                               : "project";\r
-               return projectName;\r
+       private void addSlcTasks(Project project) {\r
+               Properties taskdefs = new Properties();\r
+               try {\r
+                       InputStream in = project.getClass().getResourceAsStream(\r
+                                       "/org/argeo/slc/ant/taskdefs.properties");\r
+                       taskdefs.load(in);\r
+                       in.close();\r
+               } catch (IOException e) {\r
+                       throw new SlcAntException("Cannot load task definitions", e);\r
+               }\r
+\r
+               for (Object o : taskdefs.keySet()) {\r
+                       String name = o.toString();\r
+                       try {\r
+                               project.addTaskDefinition(name, Class.forName(taskdefs\r
+                                               .getProperty(name)));\r
+                       } catch (ClassNotFoundException e) {\r
+                               log.error("Unknown class for task " + name, e);\r
+                       }\r
+               }\r
        }\r
 }\r