]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcProjectHelper.java
Introduce unit tests framework
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / ant / SlcProjectHelper.java
1 package org.argeo.slc.ant;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Properties;
9 import java.util.Vector;
10
11 import org.springframework.context.support.AbstractApplicationContext;
12 import org.springframework.context.support.FileSystemXmlApplicationContext;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.apache.tools.ant.BuildException;
17 import org.apache.tools.ant.Project;
18 import org.apache.tools.ant.helper.ProjectHelperImpl;
19
20 import org.argeo.slc.core.structure.DefaultSRegistry;
21 import org.argeo.slc.core.structure.SimpleSElement;
22 import org.argeo.slc.core.structure.StructureRegistry;
23 import org.argeo.slc.core.structure.tree.TreeSPath;
24
25 /**
26 * Custom implementation of an Ant <code>ProjectHelper</code> binding a Spring
27 * application context and a structure registry with the Ant project.
28 */
29 public class SlcProjectHelper extends ProjectHelperImpl {
30 private static Log log;
31
32 /** The Ant reference to the Spring application context used. */
33 public static String REF_ROOT_CONTEXT = "slcApplicationContext";
34 /** The Ant reference to the SLC structure registry used. */
35 public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";
36 /** The Ant reference to the <code>TreePath</code> of the current project */
37 private static String REF_PROJECT_PATH = "slcProjectPath";
38 /**
39 * Resource path to the property file listing the SLC specific Ant tasks:
40 * /org/argeo/slc/ant/taskdefs.properties
41 */
42 private static String SLC_TASKDEFS_RESOURCE_PATH = "/org/argeo/slc/ant/taskdefs.properties";
43
44 @Override
45 public void parse(Project project, Object source) throws BuildException {
46
47 // initialize config
48 SlcAntConfig slcAntConfig = new SlcAntConfig();
49 slcAntConfig.initProject(project);
50
51 if (log == null) {
52 // log4j is initialized only now
53 log = LogFactory.getLog(SlcProjectHelper.class);
54 }
55 log.debug("SLC properties are set, starting initialization..");
56
57 // init Spring application context
58 initSpringContext(project);
59
60 // init structure registry
61 DefaultSRegistry registry = new DefaultSRegistry();
62 project.addReference(REF_STRUCTURE_REGISTRY, registry);
63
64 // call the underlying implementation to do the actual work
65 super.parse(project, source);
66
67 // create structure root
68 registerProjectAndParents(project, slcAntConfig);
69
70 addSlcTasks(project);
71
72 }
73
74 /** Creates the tree-based structure for this project. */
75 private void registerProjectAndParents(Project project,
76 SlcAntConfig slcAntConfig) {
77 StructureRegistry registry = (StructureRegistry) project
78 .getReference(REF_STRUCTURE_REGISTRY);
79 File rootDir = new File(project
80 .getUserProperty(SlcAntConfig.ROOT_DIR_PROPERTY))
81 .getAbsoluteFile();
82 File baseDir = project.getBaseDir().getAbsoluteFile();
83
84 List<File> dirs = new Vector<File>();
85 File currentDir = baseDir;
86 do {
87 dirs.add(currentDir);
88 currentDir = currentDir.getParentFile();
89 if (log.isTraceEnabled())
90 log.trace("List " + currentDir);
91 } while (!currentDir.equals(rootDir.getParentFile()));
92
93 // first path is root dir (because of previous algorithm)
94 TreeSPath currPath = TreeSPath.createRootPath(rootDir.getName());
95 for (int i = dirs.size() - 1; i >= 0; i--) {
96 File dir = dirs.get(i);
97
98 // retrieves description for this path
99 final String description;
100 if (i == 0) {// project itself
101 description = project.getDescription() != null ? project
102 .getDescription() : "";
103 } else {
104 description = slcAntConfig.getDescriptionForDir(dir);
105 }
106 SimpleSElement element = new SimpleSElement(description);
107
108 // creates and register path
109 if (!dir.equals(rootDir)) {// already set
110 currPath = currPath.createChild(dir.getName());
111 }
112 registry.register(currPath, element);
113 }
114 project.addReference(REF_PROJECT_PATH, currPath);
115 }
116
117 /** Gets the path of a project (root). */
118 public static TreeSPath getProjectPath(Project project) {
119 return (TreeSPath) project.getReference(REF_PROJECT_PATH);
120 }
121
122 /** Initializes the Spring application context. */
123 private void initSpringContext(Project project) {
124 System.getProperties().putAll((Map<?, ?>) project.getProperties());
125 String acPath = project
126 .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);
127 AbstractApplicationContext context = new FileSystemXmlApplicationContext(
128 acPath);
129 context.registerShutdownHook();
130 project.addReference(REF_ROOT_CONTEXT, context);
131 }
132
133 /** Loads the SLC specific Ant tasks. */
134 private void addSlcTasks(Project project) {
135 Properties taskdefs = new Properties();
136 try {
137 InputStream in = project.getClass().getResourceAsStream(
138 SLC_TASKDEFS_RESOURCE_PATH);
139 taskdefs.load(in);
140 in.close();
141 } catch (IOException e) {
142 throw new SlcAntException("Cannot load task definitions", e);
143 }
144
145 for (Object o : taskdefs.keySet()) {
146 String name = o.toString();
147 try {
148 project.addTaskDefinition(name, Class.forName(taskdefs
149 .getProperty(name)));
150 } catch (ClassNotFoundException e) {
151 log.error("Unknown class for task " + name, e);
152 }
153 }
154 }
155 }