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