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.Properties;\r
+\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
* 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
- // public static String PROP_REGISTRY_MODE =\r
- // "org.argeo.slc.slcRegistryMode";\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
\r
+ private String slcRootFileName = "slcRoot.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
+ // 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 slcAntConfig = new SlcAntConfig(project, slcRootFile);\r
\r
// init Spring application context\r
- String acPath = System.getProperty(PROP_APPLICATION_CONTEXT,\r
- "applicationContext.xml");\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
// 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
- }\r
\r
- private static void stdOut(Object o) {\r
- System.out.println(o);\r
}\r
\r
- /** Get the path of a project (root).*/\r
+ /** Get the path of a project (root). */\r
public static TreeSPath getProjectPath(Project project) {\r
return TreeSPath.createChild(null, getProjectPathName(project));\r
}\r
: "project";\r
return projectName;\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
+ 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