]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/ant/SlcProjectHelper.java
0b2698d2615eb59cc5f2a116ad65d1dacd6e15b0
[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.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 ProjectHelperImpl {
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 System.out.println("Base dir prop2: " + project.getProperty("basedir"));
56 // In order to avoid base dire override when running in Maven
57 project.setProperty("basedir", sourceFile.getParentFile().getAbsolutePath());
58 if (!slcAntConfig.initProject(project)) {
59 // not SLC compatible, do normal Ant
60 super.parse(project, source);
61 return;
62 }
63
64 if (log == null) {
65 // log4j is initialized only now
66 log = LogFactory.getLog(SlcProjectHelper.class);
67 }
68 log.debug("SLC properties are set, starting initialization..");
69 log.debug("Base dir1: " + project.getBaseDir().getAbsoluteFile());
70 log.debug("Base dir prop1: " + project.getProperty("basedir"));
71
72 // init Spring application context
73 initSpringContext(project);
74 log.debug("Base dir2: " + project.getBaseDir().getAbsoluteFile());
75
76 // init structure registry
77 DefaultSRegistry registry = new DefaultSRegistry();
78 project.addReference(REF_STRUCTURE_REGISTRY, registry);
79
80 log.debug("Base dir prop2: " + project.getProperty("basedir"));
81 // in order to prevent pb w/ basedir setting:
82 source = ((File) source).getAbsoluteFile();
83 // call the underlying implementation to do the actual work
84 super.parse(project, source);
85
86 log.debug("Base dir3: " + project.getBaseDir().getAbsoluteFile());
87 // create structure root
88 registerProjectAndParents(project, slcAntConfig);
89
90 addSlcTasks(project);
91
92 }
93
94 /** Creates the tree-based structure for this project. */
95 private void registerProjectAndParents(Project project,
96 SlcAntConfig slcAntConfig) {
97 StructureRegistry registry = (StructureRegistry) project
98 .getReference(REF_STRUCTURE_REGISTRY);
99 File rootDir = new File(project
100 .getUserProperty(SlcAntConfig.ROOT_DIR_PROPERTY))
101 .getAbsoluteFile();
102 File baseDir = project.getBaseDir().getAbsoluteFile();
103 List<File> dirs = new Vector<File>();
104 File currentDir = baseDir;
105 do {
106 dirs.add(currentDir);
107 currentDir = currentDir.getParentFile();
108 if (log.isTraceEnabled())
109 log.trace("List " + currentDir);
110 } while (!currentDir.equals(rootDir.getParentFile()));
111
112 // first path is root dir (because of previous algorithm)
113 TreeSPath currPath = TreeSPath.createRootPath(rootDir.getName());
114 for (int i = dirs.size() - 1; i >= 0; i--) {
115 File dir = dirs.get(i);
116
117 // retrieves description for this path
118 final String description;
119 if (i == 0) {// project itself
120 description = project.getDescription() != null ? project
121 .getDescription() : "[no desc]";
122 } else {
123 description = slcAntConfig.getDescriptionForDir(dir);
124 }
125 SimpleSElement element = new SimpleSElement(description);
126
127 // creates and register path
128 if (!dir.equals(rootDir)) {// already set
129 currPath = currPath.createChild(dir.getName());
130 }
131 registry.register(currPath, element);
132 }
133 project.addReference(REF_PROJECT_PATH, currPath);
134 }
135
136 /** Gets the path of a project (root). */
137 public static TreeSPath getProjectPath(Project project) {
138 return (TreeSPath) project.getReference(REF_PROJECT_PATH);
139 }
140
141 /** Initializes the Spring application context. */
142 private void initSpringContext(Project project) {
143 System.getProperties().putAll((Map<?, ?>) project.getProperties());
144 String acPath = project
145 .getUserProperty(SlcAntConfig.APPLICATION_CONTEXT_PROPERTY);
146 AbstractApplicationContext context = new FileSystemXmlApplicationContext(
147 acPath);
148 context.registerShutdownHook();
149 project.addReference(REF_ROOT_CONTEXT, context);
150 }
151
152 /** Loads the SLC specific Ant tasks. */
153 private void addSlcTasks(Project project) {
154 Properties taskdefs = new Properties();
155 try {
156 InputStream in = project.getClass().getResourceAsStream(
157 SLC_TASKDEFS_RESOURCE_PATH);
158 taskdefs.load(in);
159 in.close();
160 } catch (IOException e) {
161 throw new SlcAntException("Cannot load task definitions", e);
162 }
163
164 for (Object o : taskdefs.keySet()) {
165 String name = o.toString();
166 try {
167 project.addTaskDefinition(name, Class.forName(taskdefs
168 .getProperty(name)));
169 } catch (ClassNotFoundException e) {
170 log.error("Unknown class for task " + name, e);
171 }
172 }
173 }
174 }