587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.core.ant;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.IOException;
|
628
|
21 |
import java.net.MalformedURLException;
|
587
|
22 |
import java.net.URL;
|
|
23 |
import java.util.ArrayList;
|
628
|
24 |
import java.util.Enumeration;
|
|
25 |
import java.util.Hashtable;
|
|
26 |
import java.util.Iterator;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.Map;
|
587
|
29 |
import java.util.jar.JarEntry;
|
|
30 |
import java.util.jar.JarFile;
|
628
|
31 |
|
|
32 |
import org.apache.tools.ant.BuildException;
|
|
33 |
import org.apache.tools.ant.BuildListener;
|
|
34 |
import org.apache.tools.ant.BuildLogger;
|
|
35 |
import org.apache.tools.ant.Project;
|
|
36 |
import org.apache.tools.ant.ProjectHelper;
|
|
37 |
import org.apache.tools.ant.helper.DefaultExecutor;
|
|
38 |
|
|
39 |
import com.nokia.helium.core.MultiCauseBuildException;
|
|
40 |
import com.nokia.helium.core.ant.types.HlmDefList;
|
587
|
41 |
|
|
42 |
/**
|
|
43 |
* This class implements a flexible Ant Executor which allows dynamic discovery
|
|
44 |
* and automatic loading of new features. It also supports pre/post actions to
|
|
45 |
* be executed.
|
628
|
46 |
*
|
587
|
47 |
*/
|
|
48 |
public class HeliumExecutor extends DefaultExecutor {
|
628
|
49 |
|
|
50 |
private static final String HELP_TARGET = "help";
|
|
51 |
|
|
52 |
private List<HlmDefList> hlmDefCache = new ArrayList<HlmDefList>();
|
|
53 |
private MultiCauseBuildException failure;
|
587
|
54 |
|
|
55 |
/**
|
628
|
56 |
* Execute the specified Targets for the specified Project.
|
587
|
57 |
*
|
|
58 |
* @param project
|
628
|
59 |
* the Ant Project.
|
587
|
60 |
* @param targetNames
|
628
|
61 |
* String[] of Target names.
|
|
62 |
* @throws BuildException
|
|
63 |
* on error
|
587
|
64 |
*/
|
|
65 |
public void executeTargets(Project project, String[] targetNames) {
|
628
|
66 |
if (targetNames.length > 1 && targetNames[0].equals(HELP_TARGET)) {
|
|
67 |
displayHelp(project, targetNames);
|
|
68 |
} else {
|
|
69 |
executeRegularTargets(project, targetNames);
|
587
|
70 |
}
|
628
|
71 |
}
|
587
|
72 |
|
628
|
73 |
/**
|
|
74 |
* Execute the given targets.
|
|
75 |
*
|
|
76 |
* @param project
|
|
77 |
* is the ant project
|
|
78 |
* @param targetNames
|
|
79 |
* array of target names to be executed.
|
|
80 |
*/
|
|
81 |
public void executeRegularTargets(Project project, String[] targetNames) {
|
|
82 |
project.log("Running executeTargets", Project.MSG_DEBUG);
|
|
83 |
loadModules(project);
|
|
84 |
handlePreBuildActions(project, targetNames);
|
587
|
85 |
try {
|
628
|
86 |
super.executeTargets(project, targetNames);
|
|
87 |
} catch (BuildException be) {
|
|
88 |
recordFailure(be);
|
|
89 |
} finally {
|
|
90 |
handlePostBuildActions(project, targetNames);
|
|
91 |
// Propagating any raised issues.
|
|
92 |
handleException(project);
|
587
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
628
|
97 |
* Method loads all the available helium modules from the system classpath.
|
587
|
98 |
*
|
628
|
99 |
* @param project
|
|
100 |
* is the ant project.
|
587
|
101 |
*/
|
628
|
102 |
@SuppressWarnings("unchecked")
|
|
103 |
private void loadModules(Project project) {
|
|
104 |
try {
|
|
105 |
List<URL> modules = getAvailableModules();
|
|
106 |
project.log("Total no of modules available : " + modules.size(), Project.MSG_DEBUG);
|
|
107 |
for (URL module : modules) {
|
|
108 |
loadModule(project, module);
|
|
109 |
}
|
|
110 |
Map<String, Object> references = (Hashtable<String, Object>)project.getReferences();
|
|
111 |
for (String key : references.keySet()) {
|
|
112 |
Object refObj = references.get(key);
|
|
113 |
if (refObj instanceof HlmDefList) {
|
|
114 |
hlmDefCache.add((HlmDefList)refObj);
|
|
115 |
project.log("Total pre build actions : "
|
|
116 |
+ ((HlmDefList)refObj).getPreBuildActions().size(), Project.MSG_DEBUG);
|
|
117 |
project.log("Total post build actions : "
|
|
118 |
+ ((HlmDefList)refObj).getPostBuildActions().size(), Project.MSG_DEBUG);
|
|
119 |
project.log("Total exception handlers : "
|
|
120 |
+ ((HlmDefList)refObj).getExceptionHandlers().size(), Project.MSG_DEBUG);
|
|
121 |
}
|
|
122 |
}
|
|
123 |
} catch (BuildException be) {
|
|
124 |
recordFailure(be);
|
587
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
628
|
129 |
* Returns a list of available helium modules from the system classpath.
|
587
|
130 |
*
|
628
|
131 |
* @return a list of helium module files.
|
587
|
132 |
*/
|
628
|
133 |
private List<URL> getAvailableModules() {
|
|
134 |
List<URL> moduleList = new ArrayList<URL>();
|
|
135 |
String classpathString = System.getProperty("java.class.path");
|
|
136 |
String[] modules = classpathString.split(File.pathSeparator);
|
|
137 |
File module = null;
|
|
138 |
URL url = null;
|
|
139 |
for (String moduleName : modules) {
|
|
140 |
module = new File(moduleName);
|
|
141 |
if (module != null && module.isFile()
|
|
142 |
&& module.getName().endsWith(".jar")) {
|
|
143 |
try {
|
|
144 |
String hlmAntlibXmlFile = findHeliumAntlibXml(new JarFile(
|
|
145 |
module));
|
|
146 |
if (hlmAntlibXmlFile != null) {
|
|
147 |
url = new URL("jar:" + module.toURI().toString() + "!/"
|
|
148 |
+ hlmAntlibXmlFile);
|
|
149 |
moduleList.add(url);
|
|
150 |
}
|
|
151 |
} catch (MalformedURLException me) {
|
|
152 |
throw new BuildException(
|
|
153 |
"Error occured while getting helium module "
|
|
154 |
+ module + " : ", me);
|
|
155 |
} catch (IOException ioe) {
|
|
156 |
throw new BuildException("Error reading file " + module
|
|
157 |
+ ": " + ioe.getMessage(), ioe);
|
|
158 |
}
|
|
159 |
}
|
587
|
160 |
}
|
628
|
161 |
return moduleList;
|
587
|
162 |
}
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Search for helium.antlib.xml under the module Jar.
|
|
166 |
*
|
628
|
167 |
* @param jarFile
|
|
168 |
* is the jar to be searched in.
|
|
169 |
* @return the helium.antlib.xml
|
587
|
170 |
*/
|
628
|
171 |
private String findHeliumAntlibXml(JarFile jarFile) {
|
|
172 |
String hlmAntlibXmlFile = null;
|
|
173 |
for (Enumeration<JarEntry> jarEntries = jarFile.entries(); jarEntries
|
|
174 |
.hasMoreElements();) {
|
|
175 |
JarEntry je = jarEntries.nextElement();
|
587
|
176 |
if (je.getName().endsWith("/helium.antlib.xml")) {
|
628
|
177 |
hlmAntlibXmlFile = je.getName();
|
|
178 |
break;
|
587
|
179 |
}
|
|
180 |
}
|
628
|
181 |
return hlmAntlibXmlFile;
|
587
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
628
|
185 |
* Method loads the specified module .
|
587
|
186 |
*
|
628
|
187 |
* @param project
|
|
188 |
* is the ant project.
|
|
189 |
* @param module
|
|
190 |
* the helium module to be loaded.
|
587
|
191 |
*/
|
628
|
192 |
private void loadModule(Project project, URL module) {
|
|
193 |
project.log("Loading module : " + module.toString(), Project.MSG_DEBUG);
|
|
194 |
ProjectHelper helper = (ProjectHelper) project
|
|
195 |
.getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
|
|
196 |
helper.parse(project, module);
|
587
|
197 |
}
|
|
198 |
|
628
|
199 |
/**
|
|
200 |
* Method handles all the pre build events.
|
|
201 |
*
|
|
202 |
* @param project
|
|
203 |
* is the ant project.
|
|
204 |
* @param targets
|
|
205 |
* an array of target names.
|
|
206 |
*/
|
|
207 |
private void handlePreBuildActions(Project project, String[] targets) {
|
|
208 |
for (HlmDefList hlmDefList : hlmDefCache) {
|
|
209 |
for (PreBuildAction event : hlmDefList.getPreBuildActions()) {
|
|
210 |
try {
|
|
211 |
event.executeOnPreBuild(project, targets);
|
|
212 |
} catch (BuildException be) {
|
|
213 |
// Saving current issue
|
|
214 |
// We are Ignoring the errors as no need to fail the build.
|
|
215 |
recordFailure(be);
|
|
216 |
}
|
587
|
217 |
}
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
628
|
221 |
/**
|
|
222 |
* Method handles all the post build events.
|
|
223 |
*
|
|
224 |
* @param project
|
|
225 |
* is the ant project.
|
|
226 |
* @param targets
|
|
227 |
* an array of target names.
|
|
228 |
*/
|
|
229 |
private void handlePostBuildActions(Project project, String[] targets) {
|
|
230 |
for (HlmDefList hlmDefList : hlmDefCache) {
|
|
231 |
for (PostBuildAction event : hlmDefList.getPostBuildActions()) {
|
|
232 |
try {
|
|
233 |
event.executeOnPostBuild(project, targets);
|
|
234 |
} catch (BuildException be) {
|
|
235 |
// Treating possible new issues...
|
|
236 |
recordFailure(be);
|
|
237 |
}
|
587
|
238 |
}
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
628
|
242 |
/**
|
|
243 |
* Records a build failure.
|
|
244 |
*
|
|
245 |
* @param be
|
|
246 |
* a build failure.
|
|
247 |
*/
|
|
248 |
private void recordFailure(BuildException be) {
|
|
249 |
if (failure == null) {
|
|
250 |
failure = new MultiCauseBuildException();
|
587
|
251 |
}
|
628
|
252 |
failure.add(be);
|
587
|
253 |
}
|
|
254 |
|
628
|
255 |
/**
|
|
256 |
* Method handles the recored build failures if any.
|
|
257 |
*
|
|
258 |
* @param project
|
|
259 |
* is the ant project.
|
|
260 |
*/
|
|
261 |
private void handleException(Project project) {
|
|
262 |
if (failure != null) {
|
|
263 |
for (HlmDefList hlmDefList : hlmDefCache) {
|
|
264 |
for (HlmExceptionHandler handler : hlmDefList
|
|
265 |
.getExceptionHandlers()) {
|
|
266 |
try {
|
|
267 |
handler.handleException(project, failure);
|
|
268 |
} catch (BuildException be) {
|
|
269 |
// Treating possible new issues...
|
|
270 |
recordFailure(be);
|
|
271 |
}
|
|
272 |
}
|
|
273 |
}
|
|
274 |
throw failure;
|
|
275 |
}
|
|
276 |
}
|
|
277 |
|
|
278 |
@SuppressWarnings("unchecked")
|
|
279 |
private void displayHelp(Project project, String[] targetNames) {
|
|
280 |
if (targetNames.length > 1) {
|
|
281 |
project.setProperty("help.item", targetNames[1]);
|
|
282 |
}
|
|
283 |
// Set Emacs mode to true for all listeners, so that help text does
|
|
284 |
// not have [echo] at the start of each line
|
|
285 |
Iterator<BuildListener> iter = project.getBuildListeners().iterator();
|
|
286 |
while (iter.hasNext()) {
|
|
287 |
BuildListener listener = iter.next();
|
|
288 |
if (listener instanceof BuildLogger) {
|
|
289 |
BuildLogger logger = (BuildLogger) listener;
|
|
290 |
logger.setEmacsMode(true);
|
587
|
291 |
}
|
|
292 |
}
|
628
|
293 |
// Run the 'help' target
|
|
294 |
project.executeTarget(HELP_TARGET);
|
587
|
295 |
}
|
628
|
296 |
}
|