X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.slc%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fant%2FSlcProjectHelper.java;h=0b2698d2615eb59cc5f2a116ad65d1dacd6e15b0;hb=b5c4e0c9c2fcf788a56d6ce72989fe15182e057d;hp=2eb703e90689a0bb06e82483b0e1e86f9d6ac6f7;hpb=6e6998e19852f8209f955c0d2c773feca161d4d0;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 2eb703e90..0b2698d26 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,58 +1,174 @@
package org.argeo.slc.ant;
-import org.springframework.context.ApplicationContext;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Vector;
+
+import org.springframework.context.support.AbstractApplicationContext;
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.UnsupportedException;
import org.argeo.slc.core.structure.DefaultSRegistry;
-import org.argeo.slc.core.structure.tree.TreeSElement;
+import org.argeo.slc.core.structure.SimpleSElement;
+import org.argeo.slc.core.structure.StructureRegistry;
import org.argeo.slc.core.structure.tree.TreeSPath;
+/**
+ * Custom implementation of an Ant 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";
- //public static String PROP_REGISTRY_MODE = "org.argeo.slc.slcRegistryMode";
+ private static Log log;
+
+ /** The Ant reference to the Spring application context used. */
public static String REF_ROOT_CONTEXT = "slcApplicationContext";
+ /** The Ant reference to the SLC structure registry used. */
public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";
+ /** The Ant reference to the TreePath
of the current project */
+ private static String REF_PROJECT_PATH = "slcProjectPath";
+ /**
+ * Resource path to the property file listing the SLC specific Ant tasks:
+ * /org/argeo/slc/ant/taskdefs.properties
+ */
+ private static String SLC_TASKDEFS_RESOURCE_PATH = "/org/argeo/slc/ant/taskdefs.properties";
@Override
public void parse(Project project, Object source) throws BuildException {
- stdOut("Entered SLC project helper");
+ if (!(source instanceof File)) {
+ throw new UnsupportedException("Ant file", source);
+ }
+ File sourceFile = (File)source;
+
+ // initialize config
+ SlcAntConfig slcAntConfig = new SlcAntConfig();
+
+ System.out.println("Base dir prop2: " + project.getProperty("basedir"));
+ // In order to avoid base dire override when running in Maven
+ project.setProperty("basedir", sourceFile.getParentFile().getAbsolutePath());
+ if (!slcAntConfig.initProject(project)) {
+ // not SLC compatible, do normal Ant
+ super.parse(project, source);
+ return;
+ }
+
+ if (log == null) {
+ // log4j is initialized only now
+ log = LogFactory.getLog(SlcProjectHelper.class);
+ }
+ log.debug("SLC properties are set, starting initialization..");
+ log.debug("Base dir1: " + project.getBaseDir().getAbsoluteFile());
+ log.debug("Base dir prop1: " + project.getProperty("basedir"));
// init Spring application context
- String acPath = System.getProperty(PROP_APPLICATION_CONTEXT,
- "applicationContext.xml");
- ApplicationContext context = new FileSystemXmlApplicationContext(acPath);
- project.addReference(REF_ROOT_CONTEXT, context);
+ initSpringContext(project);
+ log.debug("Base dir2: " + project.getBaseDir().getAbsoluteFile());
- // init structure register if it does not exist
+ // init structure registry
DefaultSRegistry registry = new DefaultSRegistry();
- project.addReference(REF_STRUCTURE_REGISTRY, registry);
+ project.addReference(REF_STRUCTURE_REGISTRY, registry);
- // call the underlying implementation to do the actual work
- super.parse(project, source);
+ log.debug("Base dir prop2: " + project.getProperty("basedir"));
+ // in order to prevent pb w/ basedir setting:
+ source = ((File) source).getAbsoluteFile();
+ // call the underlying implementation to do the actual work
+ super.parse(project, source);
+
+ log.debug("Base dir3: " + project.getBaseDir().getAbsoluteFile());
+ // create structure root
+ registerProjectAndParents(project, slcAntConfig);
+
+ addSlcTasks(project);
- String projectDescription = project.getDescription() != null ? project
- .getDescription()
- : "Root";
- TreeSElement element = TreeSElement.createRootElelment(
- getProjectPathName(project), projectDescription);
- registry.register(element);
}
- private static void stdOut(Object o) {
- System.out.println(o);
+ /** Creates the tree-based structure for this project. */
+ private void registerProjectAndParents(Project project,
+ SlcAntConfig slcAntConfig) {
+ 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();
+ 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() : "[no desc]";
+ } else {
+ description = slcAntConfig.getDescriptionForDir(dir);
+ }
+ 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(REF_PROJECT_PATH, currPath);
}
+ /** Gets the path of a project (root). */
public static TreeSPath getProjectPath(Project project) {
- return TreeSPath.createChild(null, getProjectPathName(project));
+ return (TreeSPath) project.getReference(REF_PROJECT_PATH);
}
- private static String getProjectPathName(Project project) {
- String projectName = project.getName() != null ? project.getName()
- : "project";
- return projectName;
+ /** Initializes the Spring application context. */
+ private void initSpringContext(Project project) {
+ System.getProperties().putAll((Map, ?>) project.getProperties());
+ String acPath = project
+ .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);
+ AbstractApplicationContext context = new FileSystemXmlApplicationContext(
+ acPath);
+ context.registerShutdownHook();
+ project.addReference(REF_ROOT_CONTEXT, context);
+ }
+
+ /** Loads the SLC specific Ant tasks. */
+ private void addSlcTasks(Project project) {
+ Properties taskdefs = new Properties();
+ try {
+ InputStream in = project.getClass().getResourceAsStream(
+ SLC_TASKDEFS_RESOURCE_PATH);
+ 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);
+ }
+ }
}
}