PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/nbbuild/antsrc/org/netbeans/nbbuild/extlibs/CreateLicenseSummary.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 346 lines | 285 code | 15 blank | 46 comment | 58 complexity | 6075f8a89f8875aeb22b1ffaecc1d4b1 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.nbbuild.extlibs;
  45. import java.io.BufferedReader;
  46. import java.io.File;
  47. import java.io.FileInputStream;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.InputStreamReader;
  52. import java.io.OutputStream;
  53. import java.io.OutputStreamWriter;
  54. import java.io.PrintWriter;
  55. import java.util.Arrays;
  56. import java.util.Enumeration;
  57. import java.util.HashMap;
  58. import java.util.LinkedHashMap;
  59. import java.util.List;
  60. import java.util.Map;
  61. import java.util.Set;
  62. import java.util.TreeMap;
  63. import java.util.TreeSet;
  64. import java.util.regex.Matcher;
  65. import java.util.regex.Pattern;
  66. import java.util.zip.CRC32;
  67. import java.util.zip.ZipEntry;
  68. import java.util.zip.ZipFile;
  69. import org.apache.tools.ant.BuildException;
  70. import org.apache.tools.ant.Project;
  71. import org.apache.tools.ant.Task;
  72. import org.apache.tools.ant.types.selectors.SelectorUtils;
  73. import org.netbeans.nbbuild.JUnitReportWriter;
  74. /**
  75. * Creates a list of external binaries and their licenses.
  76. */
  77. public class CreateLicenseSummary extends Task {
  78. private File nball;
  79. public void setNball(File nball) {
  80. this.nball = nball;
  81. }
  82. private File build;
  83. public void setBuild(File build) {
  84. this.build = build;
  85. }
  86. private File summary;
  87. public void setSummary(File summary) {
  88. this.summary = summary;
  89. }
  90. private File reportFile;
  91. public void setReport(File report) {
  92. this.reportFile = report;
  93. }
  94. private Map<String,String> pseudoTests;
  95. public @Override void execute() throws BuildException {
  96. pseudoTests = new LinkedHashMap<String,String>();
  97. try {
  98. Map<Long,Map<String,String>> crc2License = findCrc2LicenseHeaderMapping();
  99. Map<String,Map<String,String>> binaries2LicenseHeaders = new TreeMap<String,Map<String,String>>();
  100. StringBuilder testBinariesAreUnique = new StringBuilder();
  101. List<String> ignoredPatterns = VerifyLibsAndLicenses.loadPatterns("ignored-binary-overlaps");
  102. findBinaries(build, binaries2LicenseHeaders, crc2License, new HashMap<Long,String>(), "", testBinariesAreUnique, ignoredPatterns);
  103. pseudoTests.put("testBinariesAreUnique", testBinariesAreUnique.length() > 0 ? "Some binaries are duplicated (edit nbbuild/antsrc/org/netbeans/nbbuild/extlibs/ignored-binary-overlaps as needed)" + testBinariesAreUnique : null);
  104. OutputStream os = new FileOutputStream(summary);
  105. try {
  106. PrintWriter pw = new PrintWriter(new OutputStreamWriter(os, "UTF-8"));
  107. pw.println("DO NOT TRANSLATE OR LOCALIZE.");
  108. pw.println();
  109. pw.println("********************************************************************************");
  110. pw.println("Oracle elects to use only the GNU Lesser General Public License version 2.1");
  111. pw.println("(LGPL) for any software where a choice of LGPL/GPL license versions are made");
  112. pw.println("available with the language indicating that LGPLv2.1/GPLv2 or any later version");
  113. pw.println("may be used, or where a choice of which version of the LGPL/GPL is applied is");
  114. pw.println("unspecified.");
  115. pw.println("********************************************************************************");
  116. pw.println();
  117. Set<String> licenseNames = new TreeSet<String>();
  118. pw.printf("%-60s %s\n", "THIRD-PARTY COMPONENT FILE", "LICENSE");
  119. for (Map.Entry<String,Map<String,String>> entry : binaries2LicenseHeaders.entrySet()) {
  120. String binary = entry.getKey();
  121. Map<String,String> headers = entry.getValue();
  122. pw.printf("%-60s %s\n", binary, getMaybeMissing(headers, "License"));
  123. String license = headers.get("License");
  124. if (license != null) {
  125. licenseNames.add(license);
  126. }
  127. }
  128. String[] otherHeaders = {"Name", "Version", "Description", "Origin"};
  129. Map<Map<String,String>,Set<String>> licenseHeaders2Binaries = new LinkedHashMap<Map<String,String>,Set<String>>();
  130. for (Map.Entry<String,Map<String,String>> entry : binaries2LicenseHeaders.entrySet()) {
  131. Map<String,String> headers = new HashMap<String,String>(entry.getValue());
  132. headers.keySet().retainAll(Arrays.asList(otherHeaders));
  133. Set<String> binaries = licenseHeaders2Binaries.get(headers);
  134. if (binaries == null) {
  135. binaries = new TreeSet<String>();
  136. licenseHeaders2Binaries.put(headers, binaries);
  137. }
  138. binaries.add(entry.getKey());
  139. }
  140. for (Map.Entry<Map<String,String>,Set<String>> entry : licenseHeaders2Binaries.entrySet()) {
  141. pw.println();
  142. for (String header : otherHeaders) {
  143. pw.printf("%s: %s\n", header, getMaybeMissing(entry.getKey(), header));
  144. }
  145. pw.println ("Files:");
  146. for (String binary : entry.getValue()) {
  147. pw.println(binary);
  148. }
  149. }
  150. File licenses = new File(new File(nball, "nbbuild"), "licenses");
  151. for (String licenseName : licenseNames) {
  152. if (licenseName == null) {
  153. continue;
  154. }
  155. File license = new File(licenses, licenseName);
  156. if (!license.isFile()) {
  157. continue;
  158. }
  159. pw.println();
  160. pw.println();
  161. pw.println("===");
  162. pw.println("======");
  163. pw.println("========================= " + licenseName + " =========================");
  164. pw.println();
  165. InputStream is = new FileInputStream(license);
  166. try {
  167. BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  168. String line;
  169. while ((line = r.readLine()) != null) {
  170. pw.println(line);
  171. }
  172. r.close();
  173. } finally {
  174. is.close();
  175. }
  176. }
  177. pw.flush();
  178. pw.close();
  179. } finally {
  180. os.close();
  181. }
  182. log(summary + ": written");
  183. } catch (IOException x) {
  184. throw new BuildException(x, getLocation());
  185. }
  186. JUnitReportWriter.writeReport(this, null, reportFile, pseudoTests);
  187. }
  188. private String getMaybeMissing(Map<String,String> headers, String headerName) {
  189. if (headers.containsKey(headerName)) {
  190. return headers.get(headerName);
  191. } else {
  192. return "<unknown>";
  193. }
  194. }
  195. private Map<Long,Map<String,String>> findCrc2LicenseHeaderMapping() throws IOException {
  196. Map<Long,Map<String,String>> crc2LicenseHeaders = new HashMap<Long,Map<String,String>>();
  197. for (String cluster : getProject().getProperty("nb.clusters.list").split("[, ]+")) {
  198. for (String module : getProject().getProperty(cluster).split("[, ]+")) {
  199. File d = new File(new File(nball, module), "external");
  200. Set<String> hgFiles = VerifyLibsAndLicenses.findHgControlledFiles(d);
  201. Map<String,Map<String,String>> binary2License = findBinary2LicenseHeaderMapping(hgFiles, d);
  202. for (String n : hgFiles) {
  203. if (!n.endsWith(".jar") && !n.endsWith(".zip")) {
  204. continue;
  205. }
  206. Map<String,String> headers = binary2License.get(n);
  207. if (headers == null) {
  208. continue;
  209. }
  210. File f = new File(d, n);
  211. InputStream is = new FileInputStream(f);
  212. try {
  213. crc2LicenseHeaders.put(computeCRC32(is), headers);
  214. } finally {
  215. is.close();
  216. }
  217. ZipFile zf = new ZipFile(f);
  218. try {
  219. Enumeration<? extends ZipEntry> entries = zf.entries();
  220. while (entries.hasMoreElements()) {
  221. ZipEntry entry = entries.nextElement();
  222. String innerName = entry.getName();
  223. if (!innerName.endsWith(".jar") && !innerName.endsWith(".zip")) {
  224. continue;
  225. }
  226. is = zf.getInputStream(entry);
  227. try {
  228. crc2LicenseHeaders.put(computeCRC32(is), headers);
  229. } finally {
  230. is.close();
  231. }
  232. }
  233. } finally {
  234. zf.close();
  235. }
  236. }
  237. }
  238. }
  239. return crc2LicenseHeaders;
  240. }
  241. private long computeCRC32(InputStream is) throws IOException {
  242. byte[] buf = new byte[4096];
  243. CRC32 crc32 = new CRC32();
  244. int read;
  245. while ((read = is.read(buf)) != -1) {
  246. crc32.update(buf, 0, read);
  247. }
  248. return crc32.getValue();
  249. }
  250. private Map<String,Map<String,String>> findBinary2LicenseHeaderMapping(Set<String> cvsFiles, File d) throws IOException {
  251. Map<String,Map<String,String>> binary2LicenseHeaders = new HashMap<String,Map<String,String>>();
  252. for (String n : cvsFiles) {
  253. if (!n.endsWith("-license.txt")) {
  254. continue;
  255. }
  256. Map<String,String> headers = new HashMap<String,String>();
  257. InputStream is = new FileInputStream(new File(d, n));
  258. try {
  259. BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  260. String line;
  261. while ((line = r.readLine()) != null && line.length() > 0) {
  262. Matcher m = Pattern.compile("([a-zA-Z]+): (.+)").matcher(line);
  263. if (m.matches()) {
  264. headers.put(m.group(1), m.group(2));
  265. }
  266. }
  267. r.close();
  268. } finally {
  269. is.close();
  270. }
  271. String files = headers.remove("Files");
  272. if (files != null) {
  273. for (String file : files.split("[, ]+")) {
  274. binary2LicenseHeaders.put(file, headers);
  275. }
  276. } else {
  277. binary2LicenseHeaders.put(n.replaceFirst("-license\\.txt$", ".jar"), headers);
  278. binary2LicenseHeaders.put(n.replaceFirst("-license\\.txt$", ".zip"), headers);
  279. }
  280. }
  281. return binary2LicenseHeaders;
  282. }
  283. private void findBinaries(File d, Map<String,Map<String,String>> binaries2LicenseHeaders, Map<Long,Map<String,String>> crc2LicenseHeaders,
  284. Map<Long,String> crc2Binary, String prefix, StringBuilder testBinariesAreUnique, List<String> ignoredPatterns) throws IOException {
  285. if (prefix.length() > 1000) {
  286. log("#170823: possible loop in " + prefix, Project.MSG_WARN);
  287. }
  288. String[] kids = d.list();
  289. if (kids == null) {
  290. throw new IOException("Could not list " + d);
  291. }
  292. Arrays.sort(kids);
  293. for (String n : kids) {
  294. File f = new File(d, n);
  295. if (f.isDirectory()) {
  296. findBinaries(f, binaries2LicenseHeaders, crc2LicenseHeaders, crc2Binary, prefix + n + "/", testBinariesAreUnique, ignoredPatterns);
  297. } else if (n.endsWith(".jar") || n.endsWith(".zip")) {
  298. InputStream is = new FileInputStream(f);
  299. try {
  300. long crc = computeCRC32(is);
  301. Map<String,String> headers = crc2LicenseHeaders.get(crc);
  302. if (headers != null) {
  303. String path = prefix + n;
  304. binaries2LicenseHeaders.put(path, headers);
  305. String otherPath = crc2Binary.put(crc, path);
  306. if (otherPath != null) {
  307. boolean ignored = false;
  308. for (String pattern : ignoredPatterns) {
  309. String[] parts = pattern.split(" ");
  310. assert parts.length == 2 : pattern;
  311. if (SelectorUtils.matchPath(parts[0], otherPath) && SelectorUtils.matchPath(parts[1], path)) {
  312. ignored = true;
  313. break;
  314. }
  315. if (SelectorUtils.matchPath(parts[0], path) && SelectorUtils.matchPath(parts[1], otherPath)) {
  316. ignored = true;
  317. break;
  318. }
  319. }
  320. if (!ignored) {
  321. testBinariesAreUnique.append('\n').append(otherPath).append(" and ").append(path).append(" are identical");
  322. }
  323. }
  324. }
  325. } finally {
  326. is.close();
  327. }
  328. }
  329. }
  330. }
  331. }