]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcProjectHelper.java
Improve build scripts: html files were not included.
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / ant / SlcProjectHelper.java
index ecb46c07bf0f73822b3e618f0c8e92b7816857d1..cefa61f69ab4e4ff462046393f2f5df0a60ed94f 100644 (file)
@@ -3,9 +3,12 @@ package org.argeo.slc.ant;
 import java.io.File;\r
 import java.io.IOException;\r
 import java.io.InputStream;\r
+import java.util.List;\r
+import java.util.Map;\r
 import java.util.Properties;\r
+import java.util.Vector;\r
 \r
-import org.springframework.context.ApplicationContext;\r
+import org.springframework.context.support.AbstractApplicationContext;\r
 import org.springframework.context.support.FileSystemXmlApplicationContext;\r
 \r
 import org.apache.commons.logging.Log;\r
@@ -15,88 +18,129 @@ import org.apache.tools.ant.Project;
 import org.apache.tools.ant.helper.ProjectHelperImpl;\r
 \r
 import org.argeo.slc.core.structure.DefaultSRegistry;\r
-import org.argeo.slc.core.structure.tree.TreeSElement;\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
 \r
 /**\r
- * Custom implementation of a <code>ProjectHelper</code> binding a Spring\r
+ * Custom implementation of an Ant <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
-       private static Log log = LogFactory.getLog(SlcProjectHelper.class);\r
+       private static Log log;\r
 \r
+       /** The Ant reference to the Spring application context used. */\r
        public static String REF_ROOT_CONTEXT = "slcApplicationContext";\r
+       /** The Ant reference to the SLC structure registry used. */\r
        public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";\r
-\r
-       private String slcRootFileName = "slcRoot.properties";\r
+       /** The Ant reference to the <code>TreePath</code> of the current project */\r
+       private static String REF_PROJECT_PATH = "slcProjectPath";\r
+       /**\r
+        * Resource path to the property file listing the SLC specific Ant tasks:\r
+        * /org/argeo/slc/ant/taskdefs.properties\r
+        */\r
+       private static String SLC_TASKDEFS_RESOURCE_PATH = "/org/argeo/slc/ant/taskdefs.properties";\r
 \r
        @Override\r
        public void parse(Project project, Object source) throws BuildException {\r
-               log.debug("Entered SLC project helper");\r
 \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
+               // initialize config\r
+               SlcAntConfig slcAntConfig = new SlcAntConfig();\r
+               \r
+               if(!slcAntConfig.initProject(project)){\r
+                       // not SLC compatible, do normal Ant\r
+                       super.parse(project, source);\r
+                       return;\r
+               }\r
+\r
+               if (log == null) {\r
+                       // log4j is initialized only now\r
+                       log = LogFactory.getLog(SlcProjectHelper.class);\r
                }\r
-               SlcAntConfig slcAntConfig = new SlcAntConfig(project, slcRootFile);\r
+               log.debug("SLC properties are set, starting initialization..");\r
 \r
                // init Spring application context\r
-               String acPath = project\r
-                               .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);\r
-               ApplicationContext context = new FileSystemXmlApplicationContext(acPath);\r
-               project.addReference(REF_ROOT_CONTEXT, context);\r
+               initSpringContext(project);\r
 \r
-               // init structure register if it does not exist\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
-               addSlcTasks(project);\r
-               \r
                // create structure root\r
-               String projectDescription = project.getDescription() != null ? project\r
-                               .getDescription() : "Root";\r
-               TreeSElement element = TreeSElement.createRootElelment(\r
-                               getProjectPathName(project), projectDescription);\r
-               registry.register(element);\r
+               registerProjectAndParents(project, slcAntConfig);\r
 \r
-       }\r
+               addSlcTasks(project);\r
 \r
-       /** Get the path of a project (root). */\r
-       public static TreeSPath getProjectPath(Project project) {\r
-               return TreeSPath.createChild(null, getProjectPathName(project));\r
        }\r
 \r
-       private static String getProjectPathName(Project project) {\r
-               String projectName = project.getName() != null ? project.getName()\r
-                               : "project";\r
-               return projectName;\r
-       }\r
+       /** Creates the tree-based structure for this project. */\r
+       private void registerProjectAndParents(Project project,\r
+                       SlcAntConfig slcAntConfig) {\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
+                       if (log.isTraceEnabled())\r
+                               log.trace("List " + currentDir);\r
+               } while (!currentDir.equals(rootDir.getParentFile()));\r
+\r
+               // first path is root dir (because of previous algorithm)\r
+               TreeSPath currPath = TreeSPath.createRootPath(rootDir.getName());\r
+               for (int i = dirs.size() - 1; i >= 0; i--) {\r
+                       File dir = dirs.get(i);\r
+\r
+                       // retrieves description for this path\r
+                       final String description;\r
+                       if (i == 0) {// project itself\r
+                               description = project.getDescription() != null ? project\r
+                                               .getDescription() : "";\r
+                       } else {\r
+                               description = slcAntConfig.getDescriptionForDir(dir);\r
+                       }\r
+                       SimpleSElement element = new SimpleSElement(description);\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
+                       // creates and register path\r
+                       if (!dir.equals(rootDir)) {// already set\r
+                               currPath = currPath.createChild(dir.getName());\r
                        }\r
+                       registry.register(currPath, element);\r
                }\r
+               project.addReference(REF_PROJECT_PATH, currPath);\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
+       /** Gets the path of a project (root). */\r
+       public static TreeSPath getProjectPath(Project project) {\r
+               return (TreeSPath) project.getReference(REF_PROJECT_PATH);\r
+       }\r
+\r
+       /** Initializes the Spring application context. */\r
+       private void initSpringContext(Project project) {\r
+               System.getProperties().putAll((Map<?, ?>) project.getProperties());\r
+               String acPath = project\r
+                               .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);\r
+               AbstractApplicationContext context = new FileSystemXmlApplicationContext(\r
+                               acPath);\r
+               context.registerShutdownHook();\r
+               project.addReference(REF_ROOT_CONTEXT, context);\r
        }\r
 \r
+       /** Loads the SLC specific Ant tasks. */\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
+                                       SLC_TASKDEFS_RESOURCE_PATH);\r
                        taskdefs.load(in);\r
                        in.close();\r
                } catch (IOException e) {\r
@@ -109,7 +153,7 @@ public class SlcProjectHelper extends ProjectHelperImpl {
                                project.addTaskDefinition(name, Class.forName(taskdefs\r
                                                .getProperty(name)));\r
                        } catch (ClassNotFoundException e) {\r
-                               log.error("Unknown class for task "+name, e);\r
+                               log.error("Unknown class for task " + name, e);\r
                        }\r
                }\r
        }\r