587
|
1 |
/*
|
628
|
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 |
|
587
|
18 |
package com.nokia.ant.taskdefs;
|
|
19 |
|
628
|
20 |
import java.io.BufferedReader;
|
|
21 |
import java.io.BufferedWriter;
|
|
22 |
import java.io.ByteArrayInputStream;
|
|
23 |
import java.io.File;
|
|
24 |
import java.io.FileNotFoundException;
|
|
25 |
import java.io.FileOutputStream;
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.io.InputStreamReader;
|
|
28 |
import java.io.OutputStreamWriter;
|
|
29 |
import java.io.UnsupportedEncodingException;
|
|
30 |
import java.io.Writer;
|
|
31 |
import java.net.HttpURLConnection;
|
|
32 |
import java.net.URL;
|
|
33 |
import java.util.ArrayList;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.Enumeration;
|
|
36 |
import java.util.HashSet;
|
|
37 |
import java.util.Hashtable;
|
|
38 |
import java.util.Iterator;
|
|
39 |
import java.util.List;
|
|
40 |
import java.util.Map;
|
|
41 |
import java.util.jar.Attributes;
|
|
42 |
import java.util.jar.JarFile;
|
|
43 |
import java.util.jar.Manifest;
|
|
44 |
import java.util.zip.ZipEntry;
|
587
|
45 |
|
628
|
46 |
import org.apache.tools.ant.DirectoryScanner;
|
587
|
47 |
import org.apache.tools.ant.Project;
|
|
48 |
import org.apache.tools.ant.Task;
|
|
49 |
import org.apache.tools.ant.types.FileSet;
|
628
|
50 |
import org.dom4j.Document;
|
|
51 |
import org.dom4j.DocumentException;
|
|
52 |
import org.dom4j.Element;
|
587
|
53 |
import org.dom4j.io.SAXReader;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Outputs a directed graph of Ant library dependencies, reads information from dependency jars
|
|
57 |
*/
|
628
|
58 |
public class AntDependencyTask extends Task {
|
587
|
59 |
private ArrayList<FileSet> antFileSetList = new ArrayList<FileSet>();
|
|
60 |
private String outputFile;
|
628
|
61 |
|
|
62 |
public AntDependencyTask() {
|
587
|
63 |
setTaskName("AntDependencyTask");
|
|
64 |
}
|
628
|
65 |
|
587
|
66 |
/**
|
|
67 |
* Add a set of files to copy.
|
628
|
68 |
*
|
587
|
69 |
* @param set a set of files to AntDependencyTask.
|
|
70 |
* @ant.required
|
|
71 |
*/
|
|
72 |
public void addFileset(FileSet set) {
|
|
73 |
antFileSetList.add(set);
|
|
74 |
}
|
628
|
75 |
|
587
|
76 |
/**
|
|
77 |
* Location of graph file to output to
|
628
|
78 |
*
|
587
|
79 |
* @ant.required
|
|
80 |
*/
|
628
|
81 |
public void setOutputFile(String path) {
|
587
|
82 |
outputFile = path;
|
|
83 |
}
|
628
|
84 |
|
|
85 |
public String classToJar(Class aclass) {
|
587
|
86 |
String name = aclass.getName().replace(".", "/") + ".class";
|
628
|
87 |
|
|
88 |
for (Iterator iterator = antFileSetList.iterator(); iterator.hasNext();) {
|
587
|
89 |
FileSet fs = (FileSet) iterator.next();
|
|
90 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
|
91 |
String[] srcFiles = ds.getIncludedFiles();
|
|
92 |
String basedir = ds.getBasedir().getPath();
|
628
|
93 |
// log(basedir);
|
|
94 |
|
|
95 |
for (int i = 0; i < srcFiles.length; i++) {
|
587
|
96 |
String fileName = basedir + File.separator + srcFiles[i];
|
628
|
97 |
// log(fileName);
|
587
|
98 |
try {
|
|
99 |
JarFile jar = new JarFile(fileName);
|
628
|
100 |
|
|
101 |
// for (Enumeration e = jar.entries(); e.hasMoreElements() ;)
|
|
102 |
// {log(e.nextElement().toString()); }
|
|
103 |
|
|
104 |
if (jar.getJarEntry(name) != null) {
|
587
|
105 |
return fileName;
|
628
|
106 |
}
|
587
|
107 |
}
|
628
|
108 |
catch (IOException e) {
|
587
|
109 |
// We are Ignoring the errors as no need to fail the build.
|
|
110 |
log(e.getMessage(), Project.MSG_DEBUG);
|
|
111 |
}
|
|
112 |
}
|
|
113 |
}
|
|
114 |
log(name + " not found", Project.MSG_DEBUG);
|
|
115 |
return null;
|
|
116 |
}
|
628
|
117 |
|
|
118 |
public String getJarAttr(JarFile jar, String nameOfAttr) {
|
587
|
119 |
try {
|
|
120 |
String attr = jar.getManifest().getMainAttributes().getValue(nameOfAttr);
|
628
|
121 |
if (attr != null) {
|
587
|
122 |
return attr;
|
628
|
123 |
}
|
|
124 |
|
587
|
125 |
Manifest manifest = jar.getManifest();
|
|
126 |
Map map = manifest.getEntries();
|
628
|
127 |
|
|
128 |
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
|
|
129 |
String entryName = (String) it.next();
|
|
130 |
Attributes attrs = (Attributes) map.get(entryName);
|
|
131 |
|
|
132 |
for (Iterator it2 = attrs.keySet().iterator(); it2.hasNext();) {
|
|
133 |
Attributes.Name attrName = (Attributes.Name) it2.next();
|
|
134 |
if (attrName.toString() == nameOfAttr) {
|
|
135 |
return attrs.getValue(attrName).replace("\"", "");
|
|
136 |
}
|
587
|
137 |
}
|
|
138 |
}
|
628
|
139 |
}
|
|
140 |
catch (IOException e) {
|
587
|
141 |
// We are Ignoring the errors as no need to fail the build.
|
|
142 |
log("Not able to get the JAR file attribute information. " + e.getMessage(), Project.MSG_DEBUG);
|
|
143 |
}
|
|
144 |
return null;
|
|
145 |
}
|
628
|
146 |
|
|
147 |
public HashSet<String> getJarInfo() {
|
587
|
148 |
HashSet<String> classlist = new HashSet<String>();
|
628
|
149 |
|
|
150 |
for (Iterator iterator = antFileSetList.iterator(); iterator.hasNext();) {
|
587
|
151 |
FileSet fs = (FileSet) iterator.next();
|
|
152 |
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
|
153 |
String[] srcFiles = ds.getIncludedFiles();
|
|
154 |
String basedir = ds.getBasedir().getPath();
|
628
|
155 |
// log(basedir);
|
|
156 |
|
|
157 |
for (int i = 0; i < srcFiles.length; i++) {
|
587
|
158 |
String fileName = basedir + File.separator + srcFiles[i];
|
628
|
159 |
// log(fileName);
|
587
|
160 |
try {
|
|
161 |
JarFile jar = new JarFile(fileName);
|
628
|
162 |
|
587
|
163 |
String vendor = getJarAttr(jar, "Implementation-Vendor");
|
|
164 |
String version = getJarAttr(jar, "Implementation-Version");
|
628
|
165 |
if (version == null) {
|
587
|
166 |
version = getJarAttr(jar, "Specification-Version");
|
628
|
167 |
}
|
587
|
168 |
String name = convertJarName(fileName);
|
628
|
169 |
|
|
170 |
// findLicense(srcFiles[i], jar);
|
|
171 |
|
587
|
172 |
String nameandversion = name;
|
628
|
173 |
|
|
174 |
if (version != null) {
|
587
|
175 |
version = version.replace("$", "");
|
628
|
176 |
if (!digitInString(name)) {
|
587
|
177 |
nameandversion = name + " " + version;
|
628
|
178 |
}
|
587
|
179 |
}
|
628
|
180 |
if (vendor == null) {
|
587
|
181 |
vendor = "";
|
628
|
182 |
}
|
|
183 |
classlist.add(name + " [style=filled,shape=record,label=\"" + nameandversion
|
|
184 |
+ "|" + vendor + "\"];");
|
587
|
185 |
}
|
628
|
186 |
catch (IOException e) {
|
587
|
187 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
188 |
e.printStackTrace();
|
587
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
return classlist;
|
|
194 |
}
|
628
|
195 |
|
|
196 |
public void findLicense(String name, JarFile jar) {
|
587
|
197 |
try {
|
|
198 |
ZipEntry entry = jar.getEntry("META-INF/LICENSE");
|
628
|
199 |
if (entry == null) {
|
587
|
200 |
entry = jar.getEntry("META-INF/LICENSE.txt");
|
628
|
201 |
}
|
|
202 |
if (entry != null) {
|
|
203 |
/**/
|
587
|
204 |
log("File in " + name + " in jar file ", Project.MSG_DEBUG);
|
|
205 |
byte[] data = new byte[1024];
|
|
206 |
jar.getInputStream(entry).read(data);
|
628
|
207 |
for (String line : new String(data).split("\n")) {
|
|
208 |
if (line.contains("License") || line.contains("LICENSE ")
|
|
209 |
|| line.contains("Copyright")) {
|
587
|
210 |
log("Replace License information with * " + line.replace("*", "").trim(), Project.MSG_INFO);
|
|
211 |
break;
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
628
|
215 |
else {
|
|
216 |
// http://mirrors.ibiblio.org/pub/mirrors/maven2/
|
587
|
217 |
String mavenUrl = "http://repo2.maven.org/maven2/";
|
|
218 |
Enumeration jarfiles = jar.entries();
|
|
219 |
boolean found = false;
|
588
|
220 |
while (!found && jarfiles.hasMoreElements()) {
|
587
|
221 |
ZipEntry file = (ZipEntry) jarfiles.nextElement();
|
628
|
222 |
if (file.isDirectory()) {
|
587
|
223 |
String filename = file.getName();
|
|
224 |
String[] split = file.getName().split("/");
|
|
225 |
String end = split[split.length - 1];
|
|
226 |
String specialfilename = filename + end;
|
628
|
227 |
|
587
|
228 |
URL url = new URL(mavenUrl + filename + end + "/maven-metadata.xml");
|
628
|
229 |
if (!end.equals("apache")) {
|
587
|
230 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
628
|
231 |
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
587
|
232 |
filename = filename.replace(end, name.replace(".jar", ""));
|
|
233 |
end = name.replace(".jar", "");
|
|
234 |
specialfilename = filename;
|
|
235 |
url = new URL(mavenUrl + filename + "maven-metadata.xml");
|
|
236 |
connection = (HttpURLConnection) url.openConnection();
|
|
237 |
}
|
628
|
238 |
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
|
239 |
|
587
|
240 |
SAXReader xmlReader = new SAXReader();
|
|
241 |
Document antDoc = xmlReader.read(url.openStream());
|
|
242 |
List versions = antDoc.selectNodes("//versioning/versions/version");
|
628
|
243 |
// if (version.equals(""))
|
|
244 |
// {
|
|
245 |
// version = antDoc.valueOf("/metadata/version");
|
|
246 |
// }
|
587
|
247 |
Collections.reverse(versions);
|
628
|
248 |
for (Object tmpversion : versions) {
|
|
249 |
String version = ((Element) tmpversion).getText();
|
|
250 |
URL url2 = new URL(mavenUrl + specialfilename + "/" + version
|
|
251 |
+ "/" + end + "-" + version + ".pom");
|
587
|
252 |
HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection();
|
628
|
253 |
if (connection2.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
587
|
254 |
BufferedReader din = new BufferedReader(new InputStreamReader(url2.openStream()));
|
|
255 |
StringBuffer sb = new StringBuffer();
|
|
256 |
String line = null;
|
|
257 |
while ((line = din.readLine()) != null) {
|
|
258 |
line = line.replace("xmlns=\"http://maven.apache.org/POM/4.0.0\"", "");
|
|
259 |
sb.append(line + "\n");
|
|
260 |
}
|
|
261 |
xmlReader = new SAXReader();
|
|
262 |
//
|
|
263 |
Document antDoc2 = xmlReader.read(new ByteArrayInputStream(new String(sb).getBytes()));
|
|
264 |
String license = antDoc2.valueOf("/project/licenses/license/name");
|
628
|
265 |
if (!license.equals("")) {
|
587
|
266 |
found = true;
|
|
267 |
break;
|
|
268 |
}
|
628
|
269 |
|
587
|
270 |
}
|
|
271 |
}
|
|
272 |
}
|
|
273 |
}
|
628
|
274 |
}
|
|
275 |
|
587
|
276 |
}
|
628
|
277 |
if (!found) {
|
587
|
278 |
log(name + " not found in " + jar, Project.MSG_INFO);
|
628
|
279 |
}
|
587
|
280 |
}
|
628
|
281 |
}
|
|
282 |
catch (IOException e) {
|
587
|
283 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
284 |
e.printStackTrace();
|
|
285 |
}
|
|
286 |
catch (DocumentException e) {
|
587
|
287 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
288 |
e.printStackTrace();
|
587
|
289 |
}
|
|
290 |
}
|
628
|
291 |
|
587
|
292 |
public boolean digitInString(String s) {
|
|
293 |
int j = s.length() - 1;
|
|
294 |
while (j >= 0 && Character.isDigit(s.charAt(j))) {
|
|
295 |
return true;
|
|
296 |
}
|
|
297 |
return false;
|
|
298 |
}
|
|
299 |
|
628
|
300 |
public String convertJarName(String jar) {
|
587
|
301 |
return new File(jar).getName().replace(".jar", "").replace("-", "_").replace(".", "_");
|
|
302 |
}
|
628
|
303 |
|
|
304 |
public final void execute() {
|
587
|
305 |
try {
|
|
306 |
Project project = getProject();
|
628
|
307 |
|
587
|
308 |
Hashtable taskdefs = project.getTaskDefinitions();
|
628
|
309 |
|
587
|
310 |
HashSet<String> classlist = new HashSet<String>();
|
628
|
311 |
|
587
|
312 |
Enumeration taskdefsenum = taskdefs.keys();
|
588
|
313 |
while (taskdefsenum.hasMoreElements()) {
|
587
|
314 |
String key = (String) taskdefsenum.nextElement();
|
|
315 |
Class value = (Class) taskdefs.get(key);
|
628
|
316 |
if (!key.contains("nokia") && !value.toString().contains("org.apache.tools.ant")) {
|
587
|
317 |
String name = value.getPackage().getName();
|
|
318 |
String vendor = value.getPackage().getImplementationVendor();
|
628
|
319 |
|
587
|
320 |
name = classToJar(value);
|
628
|
321 |
|
|
322 |
if (name != null) {
|
587
|
323 |
name = convertJarName(name);
|
628
|
324 |
|
587
|
325 |
classlist.add("helium_ant -> \"" + name + "\";");
|
628
|
326 |
|
|
327 |
if (vendor == null) {
|
587
|
328 |
vendor = "";
|
628
|
329 |
}
|
|
330 |
|
|
331 |
classlist.add(name + " [style=filled,shape=record,label=\"" + name + "|"
|
|
332 |
+ vendor + "\"];");
|
587
|
333 |
}
|
|
334 |
}
|
|
335 |
}
|
628
|
336 |
|
587
|
337 |
classlist.add("helium_ant -> nokia_ant;");
|
628
|
338 |
|
587
|
339 |
classlist.addAll(getJarInfo());
|
628
|
340 |
|
587
|
341 |
Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF8"));
|
628
|
342 |
|
|
343 |
for (String value : classlist) {
|
587
|
344 |
output.write(value + "\n");
|
628
|
345 |
}
|
|
346 |
|
587
|
347 |
output.close();
|
628
|
348 |
}
|
|
349 |
catch (FileNotFoundException e) {
|
587
|
350 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
351 |
log("FileNotFoundException occured while getting the ANT task dependency information. "
|
|
352 |
+ e.getMessage(), Project.MSG_DEBUG);
|
|
353 |
}
|
|
354 |
catch (UnsupportedEncodingException e) {
|
587
|
355 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
356 |
log("UnsupportedEncodingException occured while getting the ANT task dependency information. "
|
|
357 |
+ e.getMessage(), Project.MSG_DEBUG);
|
|
358 |
}
|
|
359 |
catch (IOException e) {
|
587
|
360 |
// We are Ignoring the errors as no need to fail the build.
|
628
|
361 |
log("IOException occured while getting the ANT task dependency information. "
|
|
362 |
+ e.getMessage(), Project.MSG_DEBUG);
|
587
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
} |