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