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