]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcProjectHelper.java
Introduce end to end testing with logging of results
[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.Properties;
8 import java.util.Vector;
9
10 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
11 import org.springframework.context.ApplicationContext;
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 a <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 = LogFactory.getLog(SlcProjectHelper.class);
31
32 public static String REF_ROOT_CONTEXT = "slcApplicationContext";
33 public static String REF_STRUCTURE_REGISTRY = "slcStructureRegistry";
34 public static String REF_PROJECT_PATH = "slcProjectPath";
35
36 private String slcRootFileName = "slcRoot.properties";
37 private String slcLocalFileName = "slcLocal.properties";
38
39 @Override
40 public void parse(Project project, Object source) throws BuildException {
41 log.debug("Entered SLC project helper");
42
43 // look for root file
44 File projectBaseDir = project.getBaseDir();
45 File slcRootFile = findSlcRootFile(projectBaseDir);
46 if (slcRootFile == null) {
47 throw new SlcAntException("Cannot find SLC root file");
48 }
49 SlcAntConfig.initProject(project, slcRootFile);
50
51 // init Spring application context
52 initSpringContext(project);
53
54 // init structure registry
55 DefaultSRegistry registry = new DefaultSRegistry();
56 project.addReference(REF_STRUCTURE_REGISTRY, registry);
57
58 // call the underlying implementation to do the actual work
59 super.parse(project, source);
60
61 // create structure root
62 registerProjectAndParents(project);
63
64 addSlcTasks(project);
65
66 }
67
68 private void registerProjectAndParents(Project project) {
69 StructureRegistry registry = (StructureRegistry) project
70 .getReference(REF_STRUCTURE_REGISTRY);
71 File rootDir = new File(project
72 .getUserProperty(SlcAntConfig.ROOT_DIR_PROPERTY))
73 .getAbsoluteFile();
74 File baseDir = project.getBaseDir().getAbsoluteFile();
75
76 List<File> dirs = new Vector<File>();
77 File currentDir = baseDir;
78 do {
79 dirs.add(currentDir);
80 currentDir = currentDir.getParentFile();
81 log.trace("List " + currentDir);
82 } while (!currentDir.equals(rootDir.getParentFile()));
83
84 TreeSPath currPath = null;
85 for (int i = dirs.size() - 1; i >= 0; i--) {
86 File dir = dirs.get(i);
87
88 String description = dir.getName();
89 File slcLocal = new File(dir.getPath() + File.separator
90 + slcLocalFileName);
91 if (slcLocal.exists()) {
92 Properties properties = SlcAntConfig.loadFile(slcLocal
93 .getAbsolutePath());
94 description = properties
95 .getProperty(SlcAntConfig.DIR_DESCRIPTION_PROPERTY);
96 } else {
97 if (i == 0) {// project it self
98 description = project.getDescription() != null ? project
99 .getDescription() : "";
100 }
101 }
102 SimpleSElement element = new SimpleSElement(description);
103
104 if (dir.equals(rootDir)) {
105 currPath = TreeSPath.createRootPath(rootDir.getName());
106 } else {
107 currPath = currPath.createChild(dir.getName());
108 }
109 registry.register(currPath, element);
110 }
111 project.addReference(REF_PROJECT_PATH, currPath);
112 }
113
114 /** Get the path of a project (root). */
115 public static TreeSPath getProjectPath(Project project) {
116 return (TreeSPath) project.getReference(REF_PROJECT_PATH);
117 }
118
119 private File findSlcRootFile(File dir) {
120 for (File file : dir.listFiles()) {
121 if (!file.isDirectory() && file.getName().equals(slcRootFileName)) {
122 return file;
123 }
124 }
125
126 File parentDir = dir.getParentFile();
127 if (parentDir == null) {
128 return null;// stop condition: not found
129 } else {
130 return findSlcRootFile(parentDir);
131 }
132 }
133
134 private void initSpringContext(Project project) {
135 System.getProperties().putAll(project.getProperties());
136 String acPath = project
137 .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);
138 ApplicationContext context = new FileSystemXmlApplicationContext(acPath);
139 project.addReference(REF_ROOT_CONTEXT, context);
140 }
141
142 private void addSlcTasks(Project project) {
143 Properties taskdefs = new Properties();
144 try {
145 InputStream in = project.getClass().getResourceAsStream(
146 "/org/argeo/slc/ant/taskdefs.properties");
147 taskdefs.load(in);
148 in.close();
149 } catch (IOException e) {
150 throw new SlcAntException("Cannot load task definitions", e);
151 }
152
153 for (Object o : taskdefs.keySet()) {
154 String name = o.toString();
155 try {
156 project.addTaskDefinition(name, Class.forName(taskdefs
157 .getProperty(name)));
158 } catch (ClassNotFoundException e) {
159 log.error("Unknown class for task " + name, e);
160 }
161 }
162 }
163 }