]> git.argeo.org Git - cc0/argeo-build.git/blob - src/org/argeo/build/Repackage.java
b637666c9b4f9d5aa070ac10287b7328436b1016
[cc0/argeo-build.git] / src / org / argeo / build / Repackage.java
1 package org.argeo.build;
2
3 import static java.lang.System.Logger.Level.DEBUG;
4 import static java.lang.System.Logger.Level.ERROR;
5 import static java.lang.System.Logger.Level.INFO;
6 import static java.lang.System.Logger.Level.TRACE;
7 import static java.lang.System.Logger.Level.WARNING;
8 import static java.nio.file.FileVisitResult.CONTINUE;
9 import static java.util.jar.Attributes.Name.MANIFEST_VERSION;
10 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_EMBED;
11 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_M2;
12 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_M2_MERGE;
13 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_M2_REPO;
14 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED;
15 import static org.argeo.build.Repackage.ManifestConstants.ARGEO_ORIGIN_URI;
16 import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_LICENSE;
17 import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_SYMBOLICNAME;
18 import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_VERSION;
19 import static org.argeo.build.Repackage.ManifestConstants.ECLIPSE_SOURCE_BUNDLE;
20 import static org.argeo.build.Repackage.ManifestConstants.EXPORT_PACKAGE;
21 import static org.argeo.build.Repackage.ManifestConstants.IMPORT_PACKAGE;
22 import static org.argeo.build.Repackage.ManifestConstants.SPDX_LICENSE_IDENTIFIER;
23
24 import java.io.BufferedWriter;
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.lang.System.Logger;
31 import java.net.MalformedURLException;
32 import java.net.URL;
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.DirectoryStream;
35 import java.nio.file.FileSystem;
36 import java.nio.file.FileSystems;
37 import java.nio.file.FileVisitResult;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.PathMatcher;
41 import java.nio.file.Paths;
42 import java.nio.file.SimpleFileVisitor;
43 import java.nio.file.StandardOpenOption;
44 import java.nio.file.attribute.BasicFileAttributes;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.Iterator;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Objects;
51 import java.util.Properties;
52 import java.util.Set;
53 import java.util.TreeMap;
54 import java.util.TreeSet;
55 import java.util.concurrent.CompletableFuture;
56 import java.util.jar.Attributes;
57 import java.util.jar.JarEntry;
58 import java.util.jar.JarInputStream;
59 import java.util.jar.JarOutputStream;
60 import java.util.jar.Manifest;
61 import java.util.zip.Deflater;
62
63 import aQute.bnd.osgi.Analyzer;
64 import aQute.bnd.osgi.Jar;
65
66 /**
67 * Simple tool repackaging existing jar files into OSGi bundles in an A2
68 * repository.
69 */
70 public class Repackage {
71 final static Logger logger = System.getLogger(Repackage.class.getName());
72
73 /**
74 * Environment variable on whether sources should be packaged separately or
75 * integrated in the bundles.
76 */
77 final static String ENV_SOURCE_BUNDLES = "SOURCE_BUNDLES";
78
79 /** Whether repackaging should run in parallel or sequentially. */
80 final static boolean parallel = true;
81
82 // cache
83 /** Summary of all license seen during the repackaging. */
84 final static Map<String, Set<String>> licensesUsed = new TreeMap<>();
85
86 /** Main entry point. */
87 public static void main(String[] args) {
88 if (args.length < 2) {
89 System.err.println("Usage: <path to a2 output dir> <category1> <category2> ...");
90 System.exit(1);
91 }
92 Path a2Base = Paths.get(args[0]).toAbsolutePath().normalize();
93 Path descriptorsBase = Paths.get(".").toAbsolutePath().normalize();
94 Repackage factory = new Repackage(a2Base, descriptorsBase);
95
96 List<CompletableFuture<Void>> toDos = new ArrayList<>();
97 for (int i = 1; i < args.length; i++) {
98 Path p = Paths.get(args[i]);
99 if (parallel)
100 toDos.add(CompletableFuture.runAsync(() -> factory.processCategory(p)));
101 else
102 factory.processCategory(p);
103 }
104 CompletableFuture.allOf(toDos.toArray(new CompletableFuture[toDos.size()])).join();
105
106 // Summary
107 StringBuilder sb = new StringBuilder();
108 for (String licenseId : licensesUsed.keySet())
109 for (String name : licensesUsed.get(licenseId))
110 sb.append((licenseId.equals("") ? "Proprietary" : licenseId) + "\t\t" + name + "\n");
111 logger.log(INFO, "# License summary:\n" + sb);
112 }
113
114 /** Name of the file centralising information for multiple M2 artifacts. */
115 final static String COMMON_BND = "common.bnd";
116 /** Name of the file centralising information for mergin M2 artifacts. */
117 final static String MERGE_BND = "merge.bnd";
118 /**
119 * Subdirectory of the jar file where origin informations (changes, legal
120 * notices etc. are stored)
121 */
122 final static String A2_ORIGIN = "A2-ORIGIN";
123
124 /** Directory where to download archives */
125 final Path originBase;
126 /** Directory where to download Maven artifacts */
127 final Path mavenBase;
128
129 /** A2 repository base for binary bundles */
130 final Path a2Base;
131 /** A2 repository base for source bundles */
132 final Path a2SrcBase;
133 /** A2 base for native components */
134 final Path a2LibBase;
135 /** Location of the descriptors driving the packaging */
136 final Path descriptorsBase;
137 /** URIs of archives to download */
138 final Properties uris = new Properties();
139 /** Mirrors for archive download. Key is URI prefix, value list of base URLs */
140 final Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
141
142 /** Whether sources should be packaged separately */
143 final boolean sourceBundles;
144
145 /** Constructor initialises the various variables */
146 public Repackage(Path a2Base, Path descriptorsBase) {
147 sourceBundles = Boolean.parseBoolean(System.getenv(ENV_SOURCE_BUNDLES));
148 if (sourceBundles)
149 logger.log(INFO, "Sources will be packaged separately");
150
151 Objects.requireNonNull(a2Base);
152 Objects.requireNonNull(descriptorsBase);
153 this.originBase = Paths.get(System.getProperty("user.home"), ".cache", "argeo/build/origin");
154 this.mavenBase = Paths.get(System.getProperty("user.home"), ".m2", "repository");
155
156 // TODO define and use a build base
157 this.a2Base = a2Base;
158 this.a2SrcBase = a2Base.getParent().resolve(a2Base.getFileName() + ".src");
159 this.a2LibBase = a2Base.resolve("lib");
160 this.descriptorsBase = descriptorsBase;
161 if (!Files.exists(this.descriptorsBase))
162 throw new IllegalArgumentException(this.descriptorsBase + " does not exist");
163
164 // URIs mapping
165 Path urisPath = this.descriptorsBase.resolve("uris.properties");
166 if (Files.exists(urisPath)) {
167 try (InputStream in = Files.newInputStream(urisPath)) {
168 uris.load(in);
169 } catch (IOException e) {
170 throw new IllegalStateException("Cannot load " + urisPath, e);
171 }
172 }
173
174 // Eclipse mirrors
175 Path eclipseMirrorsPath = this.descriptorsBase.resolve("eclipse.mirrors.txt");
176 List<String> eclipseMirrors = new ArrayList<>();
177 if (Files.exists(eclipseMirrorsPath)) {
178 try {
179 eclipseMirrors = Files.readAllLines(eclipseMirrorsPath, StandardCharsets.UTF_8);
180 } catch (IOException e) {
181 throw new IllegalStateException("Cannot load " + eclipseMirrorsPath, e);
182 }
183 for (Iterator<String> it = eclipseMirrors.iterator(); it.hasNext();) {
184 String value = it.next();
185 if (value.strip().equals(""))
186 it.remove();
187 }
188 }
189 mirrors.put("http://www.eclipse.org/downloads", eclipseMirrors);
190 }
191
192 /*
193 * MAVEN ORIGIN
194 */
195 /** Process a whole category/group id. */
196 void processCategory(Path categoryRelativePath) {
197 try {
198 Path targetCategoryBase = descriptorsBase.resolve(categoryRelativePath);
199 DirectoryStream<Path> bnds = Files.newDirectoryStream(targetCategoryBase,
200 (p) -> p.getFileName().toString().endsWith(".bnd") && !p.getFileName().toString().equals(COMMON_BND)
201 && !p.getFileName().toString().equals(MERGE_BND));
202 for (Path p : bnds) {
203 processSingleM2ArtifactDistributionUnit(p);
204 }
205
206 DirectoryStream<Path> dus = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
207 for (Path duDir : dus) {
208 if (duDir.getFileName().toString().startsWith("eclipse-")) {
209 processEclipseArchive(duDir);
210 } else {
211 processM2BasedDistributionUnit(duDir);
212 }
213 }
214 } catch (IOException e) {
215 throw new RuntimeException("Cannot process category " + categoryRelativePath, e);
216 }
217 }
218
219 /** Process a standalone Maven artifact. */
220 void processSingleM2ArtifactDistributionUnit(Path bndFile) {
221 try {
222 Path categoryRelativePath = descriptorsBase.relativize(bndFile.getParent());
223 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
224
225 Properties fileProps = new Properties();
226 try (InputStream in = Files.newInputStream(bndFile)) {
227 fileProps.load(in);
228 }
229 String repoStr = fileProps.containsKey(ARGEO_ORIGIN_M2_REPO.toString())
230 ? fileProps.getProperty(ARGEO_ORIGIN_M2_REPO.toString())
231 : null;
232
233 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
234 // use file name as symbolic name
235 String symbolicName = bndFile.getFileName().toString();
236 symbolicName = symbolicName.substring(0, symbolicName.length() - ".bnd".length());
237 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), symbolicName);
238 }
239
240 String m2Coordinates = fileProps.getProperty(ARGEO_ORIGIN_M2.toString());
241 if (m2Coordinates == null)
242 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile);
243 M2Artifact artifact = new M2Artifact(m2Coordinates);
244 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
245 Path downloaded = downloadMaven(url, artifact);
246
247 // some proprietary artifacts do not allow any modification
248 boolean doNotModify = Boolean.parseBoolean(
249 fileProps.getOrDefault(ManifestConstants.ARGEO_DO_NOT_MODIFY.toString(), "false").toString());
250 if (doNotModify) {
251 Path unmodifiedTarget = targetCategoryBase.resolve(
252 fileProps.getProperty(BUNDLE_SYMBOLICNAME.toString()) + "." + artifact.getBranch() + ".jar");
253 Files.copy(downloaded, unmodifiedTarget);
254 return;
255 }
256
257 A2Origin origin = new A2Origin();
258 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact, origin);
259
260 downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir, false);
261
262 createJar(targetBundleDir, origin);
263 } catch (Exception e) {
264 throw new RuntimeException("Cannot process " + bndFile, e);
265 }
266 }
267
268 /**
269 * Process multiple Maven artifacts coming from a same project and therefore
270 * with information in common (typically the version), generating single bundles
271 * or merging them if necessary.
272 */
273 void processM2BasedDistributionUnit(Path duDir) {
274 try {
275 Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent());
276 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
277
278 Path mergeBnd = duDir.resolve(MERGE_BND);
279 if (Files.exists(mergeBnd)) // merge
280 mergeM2Artifacts(mergeBnd);
281
282 Path commonBnd = duDir.resolve(COMMON_BND);
283 if (!Files.exists(commonBnd))
284 return;
285
286 Properties commonProps = new Properties();
287 try (InputStream in = Files.newInputStream(commonBnd)) {
288 commonProps.load(in);
289 }
290
291 String m2Version = commonProps.getProperty(ARGEO_ORIGIN_M2.toString());
292 if (m2Version == null) {
293 logger.log(WARNING, "Ignoring " + duDir + " as it is not an M2-based distribution unit");
294 return;// ignore, this is probably an Eclipse archive
295 }
296 if (!m2Version.startsWith(":")) {
297 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
298 }
299 m2Version = m2Version.substring(1);
300
301 DirectoryStream<Path> ds = Files.newDirectoryStream(duDir,
302 (p) -> p.getFileName().toString().endsWith(".bnd") && !p.getFileName().toString().equals(COMMON_BND)
303 && !p.getFileName().toString().equals(MERGE_BND));
304 for (Path p : ds) {
305 Properties fileProps = new Properties();
306 try (InputStream in = Files.newInputStream(p)) {
307 fileProps.load(in);
308 }
309 String m2Coordinates = fileProps.getProperty(ARGEO_ORIGIN_M2.toString());
310 M2Artifact artifact = new M2Artifact(m2Coordinates);
311 artifact.setVersion(m2Version);
312
313 // prepare manifest entries
314 Properties mergeProps = new Properties();
315 mergeProps.putAll(commonProps);
316
317 fileEntries: for (Object key : fileProps.keySet()) {
318 if (ARGEO_ORIGIN_M2.toString().equals(key))
319 continue fileEntries;
320 String value = fileProps.getProperty(key.toString());
321 Object previousValue = mergeProps.put(key.toString(), value);
322 if (previousValue != null) {
323 logger.log(WARNING,
324 commonBnd + ": " + key + " was " + previousValue + ", overridden with " + value);
325 }
326 }
327 mergeProps.put(ARGEO_ORIGIN_M2.toString(), artifact.toM2Coordinates());
328 if (!mergeProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
329 // use file name as symbolic name
330 String symbolicName = p.getFileName().toString();
331 symbolicName = symbolicName.substring(0, symbolicName.length() - ".bnd".length());
332 mergeProps.put(BUNDLE_SYMBOLICNAME.toString(), symbolicName);
333 }
334
335 String repoStr = mergeProps.containsKey(ARGEO_ORIGIN_M2_REPO.toString())
336 ? mergeProps.getProperty(ARGEO_ORIGIN_M2_REPO.toString())
337 : null;
338
339 // download
340 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
341 Path downloaded = downloadMaven(url, artifact);
342
343 A2Origin origin = new A2Origin();
344 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergeProps, artifact, origin);
345 downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir, false);
346 createJar(targetBundleDir, origin);
347 }
348 } catch (IOException e) {
349 throw new RuntimeException("Cannot process " + duDir, e);
350 }
351 }
352
353 /** Merge multiple Maven artifacts. */
354 void mergeM2Artifacts(Path mergeBnd) throws IOException {
355 Path duDir = mergeBnd.getParent();
356 String category = duDir.getParent().getFileName().toString();
357 Path targetCategoryBase = a2Base.resolve(category);
358
359 Properties mergeProps = new Properties();
360 try (InputStream in = Files.newInputStream(mergeBnd)) {
361 mergeProps.load(in);
362 }
363
364 String m2Version = mergeProps.getProperty(ARGEO_ORIGIN_M2.toString());
365 if (m2Version == null) {
366 logger.log(WARNING, "Ignoring " + duDir + " as it is not an M2-based distribution unit");
367 return;// ignore, this is probably an Eclipse archive
368 }
369 if (!m2Version.startsWith(":")) {
370 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
371 }
372 m2Version = m2Version.substring(1);
373 mergeProps.put(BUNDLE_VERSION.toString(), m2Version);
374
375 String artifactsStr = mergeProps.getProperty(ARGEO_ORIGIN_M2_MERGE.toString());
376 if (artifactsStr == null)
377 throw new IllegalArgumentException(mergeBnd + ": " + ARGEO_ORIGIN_M2_MERGE + " must be set");
378
379 String repoStr = mergeProps.containsKey(ARGEO_ORIGIN_M2_REPO.toString())
380 ? mergeProps.getProperty(ARGEO_ORIGIN_M2_REPO.toString())
381 : null;
382
383 String bundleSymbolicName = mergeProps.getProperty(BUNDLE_SYMBOLICNAME.toString());
384 if (bundleSymbolicName == null)
385 throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd);
386 CategoryNameVersion nameVersion = new M2Artifact(category + ":" + bundleSymbolicName + ":" + m2Version);
387
388 A2Origin origin = new A2Origin();
389 Path bundleDir = targetCategoryBase.resolve(bundleSymbolicName + "." + nameVersion.getBranch());
390
391 String[] artifacts = artifactsStr.split(",");
392 artifacts: for (String str : artifacts) {
393 String m2Coordinates = str.trim();
394 if ("".equals(m2Coordinates))
395 continue artifacts;
396 M2Artifact artifact = new M2Artifact(m2Coordinates.trim());
397 if (artifact.getVersion() == null)
398 artifact.setVersion(m2Version);
399 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
400 Path downloaded = downloadMaven(url, artifact);
401 JarEntry entry;
402 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(downloaded), false)) {
403 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
404 if (entry.isDirectory())
405 continue entries;
406 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF")) {
407 origin.deleted.add("cryptographic signatures from " + artifact);
408 continue entries;
409 }
410 if (entry.getName().endsWith("module-info.class")) { // skip Java 9 module info
411 origin.deleted.add("Java module information (module-info.class) from " + artifact);
412 continue entries;
413 }
414 if (entry.getName().startsWith("META-INF/versions/")) { // skip multi-version
415 origin.deleted.add("additional Java versions (META-INF/versions) from " + artifact);
416 continue entries;
417 }
418 if (entry.getName().startsWith("META-INF/maven/")) {
419 origin.deleted.add("Maven information (META-INF/maven) from " + artifact);
420 continue entries;
421 }
422 if (entry.getName().startsWith(".cache/")) { // Apache SSHD
423 origin.deleted.add("cache directory (.cache) from " + artifact);
424 continue entries;
425 }
426 if (entry.getName().equals("META-INF/DEPENDENCIES")) {
427 origin.deleted.add("Dependencies (META-INF/DEPENDENCIES) from " + artifact);
428 continue entries;
429 }
430 if (entry.getName().equals("META-INF/MANIFEST.MF")) {
431 Path originalManifest = bundleDir.resolve(A2_ORIGIN).resolve(artifact.getGroupId())
432 .resolve(artifact.getArtifactId()).resolve("MANIFEST.MF");
433 Files.createDirectories(originalManifest.getParent());
434 try (OutputStream out = Files.newOutputStream(originalManifest)) {
435 Files.copy(jarIn, originalManifest);
436 }
437 origin.added.add(
438 "original MANIFEST (" + bundleDir.relativize(originalManifest) + ") from " + artifact);
439 continue entries;
440 }
441
442 if (entry.getName().endsWith("NOTICE") || entry.getName().endsWith("NOTICE.txt")
443 || entry.getName().endsWith("LICENSE") || entry.getName().endsWith("LICENSE.md")
444 || entry.getName().endsWith("LICENSE-notice.md") || entry.getName().endsWith("COPYING")
445 || entry.getName().endsWith("COPYING.LESSER")) {
446 Path artifactOriginDir = bundleDir.resolve(A2_ORIGIN).resolve(artifact.getGroupId())
447 .resolve(artifact.getArtifactId());
448 Path target = artifactOriginDir.resolve(entry.getName());
449 Files.createDirectories(target.getParent());
450 Files.copy(jarIn, target);
451 origin.moved.add(entry.getName() + " in " + artifact + " to " + bundleDir.relativize(target));
452 continue entries;
453 }
454 Path target = bundleDir.resolve(entry.getName());
455 Files.createDirectories(target.getParent());
456 if (!Files.exists(target)) {
457 Files.copy(jarIn, target);
458 } else {
459 if (entry.getName().startsWith("META-INF/services/")) {
460 try (OutputStream out = Files.newOutputStream(target, StandardOpenOption.APPEND)) {
461 out.write("\n".getBytes());
462 jarIn.transferTo(out);
463 logger.log(DEBUG, artifact.getArtifactId() + " - Appended " + entry.getName());
464 }
465 origin.modified.add(entry.getName() + ", merging from " + artifact);
466 } else if (entry.getName().startsWith("org/apache/batik/")) {
467 logger.log(TRACE, "Skip " + entry.getName());
468 continue entries;
469 } else {
470 throw new IllegalStateException("File " + target + " from " + artifact + " already exists");
471 }
472 }
473 logger.log(TRACE, () -> "Copied " + target);
474 }
475 }
476 origin.added.add("binary content of " + artifact);
477
478 // process sources
479 downloadAndProcessM2Sources(repoStr, artifact, bundleDir, true);
480 }
481
482 // additional service files
483 Path servicesDir = duDir.resolve("services");
484 if (Files.exists(servicesDir)) {
485 for (Path p : Files.newDirectoryStream(servicesDir)) {
486 Path target = bundleDir.resolve("META-INF/services/").resolve(p.getFileName());
487 try (InputStream in = Files.newInputStream(p);
488 OutputStream out = Files.newOutputStream(target, StandardOpenOption.APPEND);) {
489 out.write("\n".getBytes());
490 in.transferTo(out);
491 logger.log(DEBUG, "Appended " + p);
492 }
493 origin.added.add(bundleDir.relativize(target).toString());
494 }
495 }
496
497 Map<String, String> entries = new TreeMap<>();
498 try (Analyzer bndAnalyzer = new Analyzer()) {
499 bndAnalyzer.setProperties(mergeProps);
500 Jar jar = new Jar(bundleDir.toFile());
501 bndAnalyzer.setJar(jar);
502 Manifest manifest = bndAnalyzer.calcManifest();
503
504 keys: for (Object key : manifest.getMainAttributes().keySet()) {
505 Object value = manifest.getMainAttributes().get(key);
506
507 switch (key.toString()) {
508 case "Tool":
509 case "Bnd-LastModified":
510 case "Created-By":
511 continue keys;
512 }
513 if ("Require-Capability".equals(key.toString())
514 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
515 continue keys;// hack for very old classes
516 entries.put(key.toString(), value.toString());
517 }
518 } catch (Exception e) {
519 throw new RuntimeException("Cannot process " + mergeBnd, e);
520 }
521
522 Manifest manifest = new Manifest();
523 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
524 Files.createDirectories(manifestPath.getParent());
525 for (String key : entries.keySet()) {
526 String value = entries.get(key);
527 manifest.getMainAttributes().putValue(key, value);
528 }
529
530 try (OutputStream out = Files.newOutputStream(manifestPath)) {
531 manifest.write(out);
532 }
533 createJar(bundleDir, origin);
534 }
535
536 /** Generate MANIFEST using BND. */
537 Path processBndJar(Path downloaded, Path targetCategoryBase, Properties fileProps, M2Artifact artifact,
538 A2Origin origin) {
539 try {
540 Map<String, String> additionalEntries = new TreeMap<>();
541 boolean doNotModifyManifest = Boolean.parseBoolean(
542 fileProps.getOrDefault(ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED.toString(), "false").toString());
543
544 // Note: we always force the symbolic name
545 if (doNotModifyManifest) {
546 fileEntries: for (Object key : fileProps.keySet()) {
547 if (ARGEO_ORIGIN_M2.toString().equals(key))
548 continue fileEntries;
549 String value = fileProps.getProperty(key.toString());
550 additionalEntries.put(key.toString(), value);
551 }
552 } else {
553 if (artifact != null) {
554 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
555 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), artifact.getName());
556 }
557 if (!fileProps.containsKey(BUNDLE_VERSION.toString())) {
558 fileProps.put(BUNDLE_VERSION.toString(), artifact.getVersion());
559 }
560 }
561
562 if (!fileProps.containsKey(EXPORT_PACKAGE.toString())) {
563 fileProps.put(EXPORT_PACKAGE.toString(),
564 "*;version=\"" + fileProps.getProperty(BUNDLE_VERSION.toString()) + "\"");
565 }
566
567 try (Analyzer bndAnalyzer = new Analyzer()) {
568 bndAnalyzer.setProperties(fileProps);
569 Jar jar = new Jar(downloaded.toFile());
570 bndAnalyzer.setJar(jar);
571 Manifest manifest = bndAnalyzer.calcManifest();
572
573 keys: for (Object key : manifest.getMainAttributes().keySet()) {
574 Object value = manifest.getMainAttributes().get(key);
575
576 switch (key.toString()) {
577 case "Tool":
578 case "Bnd-LastModified":
579 case "Created-By":
580 continue keys;
581 }
582 if ("Require-Capability".equals(key.toString())
583 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
584 continue keys;// !! hack for very old classes
585 additionalEntries.put(key.toString(), value.toString());
586 }
587 }
588 }
589 Path targetBundleDir = processBundleJar(downloaded, targetCategoryBase, additionalEntries, origin);
590 logger.log(DEBUG, () -> "Processed " + downloaded);
591 return targetBundleDir;
592 } catch (Exception e) {
593 throw new RuntimeException("Cannot BND process " + downloaded, e);
594 }
595
596 }
597
598 /** Download and integrates sources for a single Maven artifact. */
599 void downloadAndProcessM2Sources(String repoStr, M2Artifact artifact, Path targetBundleDir, boolean merging)
600 throws IOException {
601 try {
602 M2Artifact sourcesArtifact = new M2Artifact(artifact.toM2Coordinates(), "sources");
603 URL sourcesUrl = M2ConventionsUtils.mavenRepoUrl(repoStr, sourcesArtifact);
604 Path sourcesDownloaded = downloadMaven(sourcesUrl, artifact, true);
605 processM2SourceJar(sourcesDownloaded, targetBundleDir, merging ? artifact : null);
606 logger.log(TRACE, () -> "Processed source " + sourcesDownloaded);
607 } catch (Exception e) {
608 logger.log(ERROR, () -> "Cannot download source for " + artifact);
609 }
610
611 }
612
613 /** Integrate sources from a downloaded jar file. */
614 void processM2SourceJar(Path file, Path bundleDir, M2Artifact mergingFrom) throws IOException {
615 A2Origin origin = new A2Origin();
616 Path sourceDir = sourceBundles ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
617 : bundleDir.resolve("OSGI-OPT/src");
618 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
619
620 String mergingMsg = "";
621 if (mergingFrom != null)
622 mergingMsg = " of " + mergingFrom;
623
624 Files.createDirectories(sourceDir);
625 JarEntry entry;
626 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
627 if (entry.isDirectory())
628 continue entries;
629 if (entry.getName().startsWith("META-INF")) {// skip META-INF entries
630 origin.deleted.add("META-INF directory from the sources" + mergingMsg);
631 continue entries;
632 }
633 if (entry.getName().startsWith("module-info.java")) {// skip Java module information
634 origin.deleted.add("Java module information from the sources (module-info.java)" + mergingMsg);
635 continue entries;
636 }
637 if (entry.getName().startsWith("/")) { // absolute paths
638 // TODO does it really happen?
639 logger.log(WARNING, entry.getName() + " has an absolute path");
640 origin.deleted.add(entry.getName() + " from the sources" + mergingMsg);
641 continue entries;
642 }
643 Path target = sourceDir.resolve(entry.getName());
644 Files.createDirectories(target.getParent());
645 if (!Files.exists(target)) {
646 Files.copy(jarIn, target);
647 logger.log(TRACE, () -> "Copied source " + target);
648 } else {
649 logger.log(TRACE, () -> target + " already exists, skipping...");
650 }
651 }
652 }
653 // write the changes
654 if (sourceBundles) {
655 origin.appendChanges(sourceDir);
656 } else {
657 origin.added.add("source code under OSGI-OPT/src");
658 origin.appendChanges(bundleDir);
659 }
660 }
661
662 /** Download a Maven artifact. */
663 Path downloadMaven(URL url, M2Artifact artifact) throws IOException {
664 return downloadMaven(url, artifact, false);
665 }
666
667 /** Download a Maven artifact. */
668 Path downloadMaven(URL url, M2Artifact artifact, boolean sources) throws IOException {
669 return download(url, mavenBase, artifact.getGroupId().replace(".", "/") //
670 + '/' + artifact.getArtifactId() + '/' + artifact.getVersion() //
671 + '/' + artifact.getArtifactId() + "-" + artifact.getVersion() + (sources ? "-sources" : "") + ".jar");
672 }
673
674 /*
675 * ECLIPSE ORIGIN
676 */
677 /** Process an archive in Eclipse format. */
678 void processEclipseArchive(Path duDir) {
679 try {
680 Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent());
681 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
682 Files.createDirectories(targetCategoryBase);
683 // first delete all directories from previous builds
684 for (Path dir : Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p)))
685 deleteDirectory(dir);
686
687 Files.createDirectories(originBase);
688
689 Path commonBnd = duDir.resolve(COMMON_BND);
690 Properties commonProps = new Properties();
691 try (InputStream in = Files.newInputStream(commonBnd)) {
692 commonProps.load(in);
693 }
694 String url = commonProps.getProperty(ARGEO_ORIGIN_URI.toString());
695 if (url == null) {
696 url = uris.getProperty(duDir.getFileName().toString());
697 if (url == null)
698 throw new IllegalStateException("No url available for " + duDir);
699 commonProps.put(ARGEO_ORIGIN_URI.toString(), url);
700 }
701 Path downloaded = tryDownloadArchive(url, originBase);
702
703 FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null);
704
705 // filters
706 List<PathMatcher> includeMatchers = new ArrayList<>();
707 Properties includes = new Properties();
708 try (InputStream in = Files.newInputStream(duDir.resolve("includes.properties"))) {
709 includes.load(in);
710 }
711 for (Object pattern : includes.keySet()) {
712 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
713 includeMatchers.add(pathMatcher);
714 }
715
716 List<PathMatcher> excludeMatchers = new ArrayList<>();
717 Path excludeFile = duDir.resolve("excludes.properties");
718 if (Files.exists(excludeFile)) {
719 Properties excludes = new Properties();
720 try (InputStream in = Files.newInputStream(excludeFile)) {
721 excludes.load(in);
722 }
723 for (Object pattern : excludes.keySet()) {
724 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
725 excludeMatchers.add(pathMatcher);
726 }
727 }
728
729 // keys are the bundle directories
730 Map<Path, A2Origin> origins = new HashMap<>();
731 Files.walkFileTree(zipFs.getRootDirectories().iterator().next(), new SimpleFileVisitor<Path>() {
732
733 @Override
734 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
735 includeMatchers: for (PathMatcher includeMatcher : includeMatchers) {
736 if (includeMatcher.matches(file)) {
737 for (PathMatcher excludeMatcher : excludeMatchers) {
738 if (excludeMatcher.matches(file)) {
739 logger.log(TRACE, "Skipping excluded " + file);
740 return FileVisitResult.CONTINUE;
741 }
742 }
743 if (file.getFileName().toString().contains(".source_")) {
744 processEclipseSourceJar(file, targetCategoryBase);
745 logger.log(DEBUG, () -> "Processed source " + file);
746 } else {
747 Map<String, String> map = new HashMap<>();
748 for (Object key : commonProps.keySet())
749 map.put(key.toString(), commonProps.getProperty(key.toString()));
750 A2Origin origin = new A2Origin();
751 Path bundleDir = processBundleJar(file, targetCategoryBase, map, origin);
752 origins.put(bundleDir, origin);
753 logger.log(DEBUG, () -> "Processed " + file);
754 }
755 break includeMatchers;
756 }
757 }
758 return FileVisitResult.CONTINUE;
759 }
760 });
761
762 DirectoryStream<Path> dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p)
763 && p.getFileName().toString().indexOf('.') >= 0 && !p.getFileName().toString().endsWith(".src"));
764 for (Path bundleDir : dirs) {
765 A2Origin origin = origins.get(bundleDir);
766 Objects.requireNonNull(origin, "No A2 origin found for " + bundleDir);
767 createJar(bundleDir, origin);
768 }
769 } catch (IOException e) {
770 throw new RuntimeException("Cannot process " + duDir, e);
771 }
772
773 }
774
775 /** Process sources in Eclipse format. */
776 void processEclipseSourceJar(Path file, Path targetBase) throws IOException {
777 try {
778 A2Origin origin = new A2Origin();
779 Path bundleDir;
780 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
781 Manifest manifest = jarIn.getManifest();
782
783 String[] relatedBundle = manifest.getMainAttributes().getValue(ECLIPSE_SOURCE_BUNDLE.toString())
784 .split(";");
785 String version = relatedBundle[1].substring("version=\"".length());
786 version = version.substring(0, version.length() - 1);
787 NameVersion nameVersion = new NameVersion(relatedBundle[0], version);
788 bundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
789
790 Path sourceDir = sourceBundles ? bundleDir.getParent().resolve(bundleDir.toString() + ".src")
791 : bundleDir.resolve("OSGI-OPT/src");
792
793 Files.createDirectories(sourceDir);
794 JarEntry entry;
795 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
796 if (entry.isDirectory())
797 continue entries;
798 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
799 continue entries;
800 Path target = sourceDir.resolve(entry.getName());
801 Files.createDirectories(target.getParent());
802 Files.copy(jarIn, target);
803 logger.log(TRACE, () -> "Copied source " + target);
804 }
805
806 // write the changes
807 if (sourceBundles) {
808 origin.appendChanges(sourceDir);
809 } else {
810 origin.added.add("source code under OSGI-OPT/src");
811 origin.appendChanges(bundleDir);
812 }
813 }
814 } catch (IOException e) {
815 throw new IllegalStateException("Cannot process " + file, e);
816 }
817 }
818
819 /*
820 * COMMON PROCESSING
821 */
822 /** Normalise a bundle. */
823 Path processBundleJar(Path file, Path targetBase, Map<String, String> entries, A2Origin origin) throws IOException {
824 boolean embed = Boolean.parseBoolean(entries.getOrDefault(ARGEO_ORIGIN_EMBED.toString(), "false").toString());
825 NameVersion nameVersion;
826 Path bundleDir;
827 // singleton
828 boolean isSingleton = false;
829 Manifest manifest;
830 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
831 Manifest sourceManifest = jarIn.getManifest();
832 manifest = sourceManifest != null ? new Manifest(sourceManifest) : new Manifest();
833
834 String rawSourceSymbolicName = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLICNAME.toString());
835 if (rawSourceSymbolicName != null) {
836 // make sure there is no directive
837 String[] arr = rawSourceSymbolicName.split(";");
838 for (int i = 1; i < arr.length; i++) {
839 if (arr[i].trim().equals("singleton:=true"))
840 isSingleton = true;
841 logger.log(DEBUG, file.getFileName() + " is a singleton");
842 }
843 }
844 // remove problematic entries in MANIFEST
845 manifest.getEntries().clear();
846
847 String ourSymbolicName = entries.get(BUNDLE_SYMBOLICNAME.toString());
848 String ourVersion = entries.get(BUNDLE_VERSION.toString());
849
850 if (ourSymbolicName != null && ourVersion != null) {
851 nameVersion = new NameVersion(ourSymbolicName, ourVersion);
852 } else {
853 nameVersion = nameVersionFromManifest(manifest);
854 if (ourVersion != null && !nameVersion.getVersion().equals(ourVersion)) {
855 logger.log(WARNING,
856 "Original version is " + nameVersion.getVersion() + " while new version is " + ourVersion);
857 entries.put(BUNDLE_VERSION.toString(), ourVersion);
858 }
859 if (ourSymbolicName != null) {
860 // we always force our symbolic name
861 nameVersion.setName(ourSymbolicName);
862 }
863 }
864 bundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
865
866 if (sourceManifest != null) {// copy original MANIFEST
867 Path originalManifest = bundleDir.resolve(A2_ORIGIN).resolve("MANIFEST.MF");
868 Files.createDirectories(originalManifest.getParent());
869 try (OutputStream out = Files.newOutputStream(originalManifest)) {
870 sourceManifest.write(out);
871 }
872 origin.added.add("original MANIFEST (" + bundleDir.relativize(originalManifest) + ")");
873 }
874
875 // force Java 9 module name
876 entries.put(ManifestConstants.AUTOMATIC_MODULE_NAME.toString(), nameVersion.getName());
877
878 boolean isNative = false;
879 String os = null;
880 String arch = null;
881 if (bundleDir.startsWith(a2LibBase)) {
882 isNative = true;
883 Path libRelativePath = a2LibBase.relativize(bundleDir);
884 os = libRelativePath.getName(0).toString();
885 arch = libRelativePath.getName(1).toString();
886 }
887
888 if (!embed) {
889 // copy entries
890 JarEntry entry;
891 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
892 if (entry.isDirectory())
893 continue entries;
894 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF")) {
895 origin.deleted.add("cryptographic signatures");
896 continue entries;
897 }
898 if (entry.getName().endsWith("module-info.class")) { // skip Java 9 module info
899 origin.deleted.add("Java module information (module-info.class)");
900 continue entries;
901 }
902 if (entry.getName().startsWith("META-INF/versions/")) { // skip multi-version
903 origin.deleted.add("additional Java versions (META-INF/versions)");
904 continue entries;
905 }
906 if (entry.getName().startsWith("META-INF/maven/")) {
907 origin.deleted.add("Maven information (META-INF/maven)");
908 continue entries;
909 }
910 // skip file system providers as they cause issues with native image
911 if (entry.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider")) {
912 origin.deleted
913 .add("file system providers (META-INF/services/java.nio.file.spi.FileSystemProvider)");
914 continue entries;
915 }
916 if (entry.getName().startsWith("OSGI-OPT/src/")) { // skip embedded sources
917 origin.deleted.add("embedded sources");
918 continue entries;
919 }
920 Path target = bundleDir.resolve(entry.getName());
921 Files.createDirectories(target.getParent());
922 Files.copy(jarIn, target);
923
924 // native libraries
925 if (isNative && (entry.getName().endsWith(".so") || entry.getName().endsWith(".dll")
926 || entry.getName().endsWith(".jnilib"))) {
927 Path categoryDir = bundleDir.getParent();
928 boolean copyDll = false;
929 Path targetDll = categoryDir.resolve(bundleDir.relativize(target));
930 if (nameVersion.getName().equals("com.sun.jna")) {
931 if (arch.equals("x86_64"))
932 arch = "x86-64";
933 if (os.equals("macosx"))
934 os = "darwin";
935 if (target.getParent().getFileName().toString().equals(os + "-" + arch)) {
936 copyDll = true;
937 }
938 targetDll = categoryDir.resolve(target.getFileName());
939 } else {
940 copyDll = true;
941 }
942 if (copyDll) {
943 Files.createDirectories(targetDll.getParent());
944 if (Files.exists(targetDll))
945 Files.delete(targetDll);
946 Files.copy(target, targetDll);
947 }
948 Files.delete(target);
949 origin.deleted.add(bundleDir.relativize(target).toString());
950 }
951 logger.log(TRACE, () -> "Copied " + target);
952 }
953 }
954 }
955
956 // copy MANIFEST
957 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
958 Files.createDirectories(manifestPath.getParent());
959
960 if (isSingleton && entries.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
961 entries.put(BUNDLE_SYMBOLICNAME.toString(),
962 entries.get(BUNDLE_SYMBOLICNAME.toString()) + ";singleton:=true");
963 }
964
965 if (embed) {// copy embedded jar
966 Files.copy(file, bundleDir.resolve(file.getFileName()));
967 entries.put(ManifestConstants.BUNDLE_CLASSPATH.toString(), file.getFileName().toString());
968 }
969
970 // Final MANIFEST decisions
971 // This also where we check the original OSGi metadata and compare with our
972 // changes
973 for (String key : entries.keySet()) {
974 String value = entries.get(key);
975 String previousValue = manifest.getMainAttributes().getValue(key);
976 boolean wasDifferent = previousValue != null && !previousValue.equals(value);
977 boolean keepPrevious = false;
978 if (wasDifferent) {
979 if (SPDX_LICENSE_IDENTIFIER.toString().equals(key) && previousValue != null)
980 keepPrevious = true;
981 else if (BUNDLE_VERSION.toString().equals(key) && wasDifferent)
982 if (previousValue.equals(value + ".0")) // typically a Maven first release
983 keepPrevious = true;
984
985 if (keepPrevious) {
986 if (logger.isLoggable(DEBUG))
987 logger.log(DEBUG, file.getFileName() + ": " + key + " was NOT modified, value kept is "
988 + previousValue + ", not overriden with " + value);
989 value = previousValue;
990 }
991 }
992
993 manifest.getMainAttributes().putValue(key, value);
994 if (wasDifferent && !keepPrevious) {
995 if (IMPORT_PACKAGE.toString().equals(key) || EXPORT_PACKAGE.toString().equals(key))
996 logger.log(TRACE, () -> file.getFileName() + ": " + key + " was modified");
997 else
998 logger.log(WARNING,
999 file.getFileName() + ": " + key + " was " + previousValue + ", overridden with " + value);
1000 }
1001
1002 // de-pollute MANIFEST
1003 switch (key) {
1004 case "Archiver-Version":
1005 case "Build-By":
1006 case "Created-By":
1007 case "Originally-Created-By":
1008 case "Tool":
1009 case "Bnd-LastModified":
1010 manifest.getMainAttributes().remove(key);
1011 break;
1012 default: // do nothing
1013 }
1014
1015 // !! hack to remove unresolvable
1016 if (key.equals("Provide-Capability") || key.equals("Require-Capability"))
1017 if (nameVersion.getName().equals("osgi.core") || nameVersion.getName().equals("osgi.cmpn")) {
1018 manifest.getMainAttributes().remove(key);
1019 }
1020 }
1021
1022 // license checks
1023 String spdxLicenceId = manifest.getMainAttributes().getValue(SPDX_LICENSE_IDENTIFIER.toString());
1024 String bundleLicense = manifest.getMainAttributes().getValue(BUNDLE_LICENSE.toString());
1025 if (spdxLicenceId == null) {
1026 logger.log(ERROR, file.getFileName() + ": " + SPDX_LICENSE_IDENTIFIER + " not available, " + BUNDLE_LICENSE
1027 + " is " + bundleLicense);
1028 } else {
1029 if (!licensesUsed.containsKey(spdxLicenceId))
1030 licensesUsed.put(spdxLicenceId, new TreeSet<>());
1031 licensesUsed.get(spdxLicenceId).add(nameVersion.toString());
1032 }
1033
1034 origin.modified.add("jar MANIFEST (META-INF/MANIFEST.MF)");
1035 // write the MANIFEST
1036 try (OutputStream out = Files.newOutputStream(manifestPath)) {
1037 manifest.write(out);
1038 }
1039 return bundleDir;
1040 }
1041
1042 /*
1043 * UTILITIES
1044 */
1045 /** Recursively deletes a directory. */
1046 static void deleteDirectory(Path path) throws IOException {
1047 if (!Files.exists(path))
1048 return;
1049 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
1050 @Override
1051 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
1052 if (e != null)
1053 throw e;
1054 Files.delete(directory);
1055 return CONTINUE;
1056 }
1057
1058 @Override
1059 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
1060 Files.delete(file);
1061 return CONTINUE;
1062 }
1063 });
1064 }
1065
1066 /** Extract name/version from a MANIFEST. */
1067 NameVersion nameVersionFromManifest(Manifest manifest) {
1068 Attributes attrs = manifest.getMainAttributes();
1069 // symbolic name
1070 String symbolicName = attrs.getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
1071 if (symbolicName == null)
1072 return null;
1073 // make sure there is no directive
1074 symbolicName = symbolicName.split(";")[0];
1075
1076 String version = attrs.getValue(ManifestConstants.BUNDLE_VERSION.toString());
1077 return new NameVersion(symbolicName, version);
1078 }
1079
1080 /** Try to download from an URI. */
1081 Path tryDownloadArchive(String uri, Path dir) throws IOException {
1082 // find mirror
1083 List<String> urlBases = null;
1084 String uriPrefix = null;
1085 uriPrefixes: for (String uriPref : mirrors.keySet()) {
1086 if (uri.startsWith(uriPref)) {
1087 if (mirrors.get(uriPref).size() > 0) {
1088 urlBases = mirrors.get(uriPref);
1089 uriPrefix = uriPref;
1090 break uriPrefixes;
1091 }
1092 }
1093 }
1094 if (urlBases == null)
1095 try {
1096 return downloadArchive(new URL(uri), dir);
1097 } catch (FileNotFoundException e) {
1098 throw new FileNotFoundException("Cannot find " + uri);
1099 }
1100
1101 // try to download
1102 for (String urlBase : urlBases) {
1103 String relativePath = uri.substring(uriPrefix.length());
1104 URL url = new URL(urlBase + relativePath);
1105 try {
1106 return downloadArchive(url, dir);
1107 } catch (FileNotFoundException e) {
1108 logger.log(WARNING, "Cannot download " + url + ", trying another mirror");
1109 }
1110 }
1111 throw new FileNotFoundException("Cannot find " + uri);
1112 }
1113
1114 /**
1115 * Effectively download. Synchronised in order to avoid downloading twice in
1116 * parallel.
1117 */
1118 synchronized Path downloadArchive(URL url, Path dir) throws IOException {
1119 return download(url, dir, (String) null);
1120 }
1121
1122 /** Effectively download. */
1123 Path download(URL url, Path dir, String name) throws IOException {
1124
1125 Path dest;
1126 if (name == null) {
1127 // We use also use parent directory in case the archive itself has a fixed name
1128 String[] segments = url.getPath().split("/");
1129 name = segments.length > 1 ? segments[segments.length - 2] + '-' + segments[segments.length - 1]
1130 : segments[segments.length - 1];
1131 }
1132
1133 dest = dir.resolve(name);
1134 if (Files.exists(dest)) {
1135 logger.log(TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again");
1136 return dest;
1137 } else {
1138 Files.createDirectories(dest.getParent());
1139 }
1140
1141 try (InputStream in = url.openStream()) {
1142 Files.copy(in, dest);
1143 logger.log(DEBUG, () -> "Downloaded " + dest + " from " + url);
1144 }
1145 return dest;
1146 }
1147
1148 /** Create a JAR file from a directory. */
1149 Path createJar(Path bundleDir, A2Origin origin) throws IOException {
1150 // write changes
1151 origin.appendChanges(bundleDir);
1152 // Create the jar
1153 Path jarPath = bundleDir.getParent().resolve(bundleDir.getFileName() + ".jar");
1154 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
1155 Manifest manifest;
1156 try (InputStream in = Files.newInputStream(manifestPath)) {
1157 manifest = new Manifest(in);
1158 }
1159 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarPath), manifest)) {
1160 jarOut.setLevel(Deflater.DEFAULT_COMPRESSION);
1161 Files.walkFileTree(bundleDir, new SimpleFileVisitor<Path>() {
1162
1163 @Override
1164 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
1165 if (file.getFileName().toString().equals("MANIFEST.MF"))
1166 return super.visitFile(file, attrs);
1167 JarEntry entry = new JarEntry(
1168 bundleDir.relativize(file).toString().replace(File.separatorChar, '/'));
1169 jarOut.putNextEntry(entry);
1170 Files.copy(file, jarOut);
1171 return super.visitFile(file, attrs);
1172 }
1173
1174 });
1175 }
1176 deleteDirectory(bundleDir);
1177
1178 if (sourceBundles) {
1179 Path bundleCategoryDir = bundleDir.getParent();
1180 Path sourceDir = bundleCategoryDir.resolve(bundleDir.toString() + ".src");
1181 if (!Files.exists(sourceDir)) {
1182 logger.log(WARNING, sourceDir + " does not exist, skipping...");
1183 return jarPath;
1184
1185 }
1186
1187 Path relPath = a2Base.relativize(bundleCategoryDir);
1188 Path srcCategoryDir = a2SrcBase.resolve(relPath);
1189 Path srcJarP = srcCategoryDir.resolve(sourceDir.getFileName() + ".jar");
1190 Files.createDirectories(srcJarP.getParent());
1191
1192 String bundleSymbolicName = manifest.getMainAttributes().getValue("Bundle-SymbolicName").toString();
1193 // in case there are additional directives
1194 bundleSymbolicName = bundleSymbolicName.split(";")[0];
1195 Manifest srcManifest = new Manifest();
1196 srcManifest.getMainAttributes().put(MANIFEST_VERSION, "1.0");
1197 srcManifest.getMainAttributes().putValue(BUNDLE_SYMBOLICNAME.toString(), bundleSymbolicName + ".src");
1198 srcManifest.getMainAttributes().putValue(BUNDLE_VERSION.toString(),
1199 manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()).toString());
1200 srcManifest.getMainAttributes().putValue(ECLIPSE_SOURCE_BUNDLE.toString(), bundleSymbolicName
1201 + ";version=\"" + manifest.getMainAttributes().getValue(BUNDLE_VERSION.toString()));
1202
1203 try (JarOutputStream srcJarOut = new JarOutputStream(Files.newOutputStream(srcJarP), srcManifest)) {
1204 srcJarOut.setLevel(Deflater.BEST_COMPRESSION);
1205 Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
1206
1207 @Override
1208 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
1209 if (file.getFileName().toString().equals("MANIFEST.MF"))
1210 return super.visitFile(file, attrs);
1211 JarEntry entry = new JarEntry(
1212 sourceDir.relativize(file).toString().replace(File.separatorChar, '/'));
1213 srcJarOut.putNextEntry(entry);
1214 Files.copy(file, srcJarOut);
1215 return super.visitFile(file, attrs);
1216 }
1217
1218 });
1219 }
1220 deleteDirectory(sourceDir);
1221 }
1222
1223 return jarPath;
1224 }
1225
1226 /** MANIFEST headers. */
1227 enum ManifestConstants {
1228 // OSGi
1229 /** OSGi bundle symbolic name. */
1230 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
1231 /** OSGi bundle version. */
1232 BUNDLE_VERSION("Bundle-Version"), //
1233 /** OSGi bundle license. */
1234 BUNDLE_LICENSE("Bundle-License"), //
1235 /** OSGi exported packages list. */
1236 EXPORT_PACKAGE("Export-Package"), //
1237 /** OSGi imported packages list. */
1238 IMPORT_PACKAGE("Import-Package"), //
1239 /** OSGi path to embedded jar. */
1240 BUNDLE_CLASSPATH("Bundle-Classpath"), //
1241 // Java
1242 /** Java module name. */
1243 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
1244 // Eclipse
1245 /** Eclipse source bundle. */
1246 ECLIPSE_SOURCE_BUNDLE("Eclipse-SourceBundle"), //
1247 // SPDX
1248 /**
1249 * SPDX license identifier.
1250 *
1251 * @see https://spdx.org/licenses/
1252 */
1253 SPDX_LICENSE_IDENTIFIER("SPDX-License-Identifier"), //
1254 // Argeo Origin
1255 /**
1256 * Maven coordinates of the origin, possibly partial when using common.bnd or
1257 * merge.bnd.
1258 */
1259 ARGEO_ORIGIN_M2("Argeo-Origin-M2"), //
1260 /** List of Maven coordinates to merge. */
1261 ARGEO_ORIGIN_M2_MERGE("Argeo-Origin-M2-Merge"), //
1262 /** Maven repository, if not the default one. */
1263 ARGEO_ORIGIN_M2_REPO("Argeo-Origin-M2-Repo"), //
1264 /**
1265 * Do not perform BND analysis of the origin component. Typically IMport_package
1266 * and Export-Package will be kept untouched.
1267 */
1268 ARGEO_ORIGIN_MANIFEST_NOT_MODIFIED("Argeo-Origin-ManifestNotModified"), //
1269 /**
1270 * Embed the original jar without modifying it (may be required by some
1271 * proprietary licenses, such as JCR Day License).
1272 */
1273 ARGEO_ORIGIN_EMBED("Argeo-Origin-Embed"), //
1274 /**
1275 * Do not modify original jar (may be required by some proprietary licenses,
1276 * such as JCR Day License).
1277 */
1278 ARGEO_DO_NOT_MODIFY("Argeo-Origin-Do-Not-Modify"), //
1279 /**
1280 * Origin (non-Maven) URI of the component. It may be anything (jar, archive,
1281 * etc.).
1282 */
1283 ARGEO_ORIGIN_URI("Argeo-Origin-URI"), //
1284 ;
1285
1286 final String value;
1287
1288 private ManifestConstants(String value) {
1289 this.value = value;
1290 }
1291
1292 @Override
1293 public String toString() {
1294 return value;
1295 }
1296 }
1297 }
1298
1299 /**
1300 * Gathers modifications performed on the original binaries and sources,
1301 * especially in order to comply with their license requirements.
1302 */
1303 class A2Origin {
1304 Set<String> modified = new TreeSet<>();
1305 Set<String> deleted = new TreeSet<>();
1306 Set<String> added = new TreeSet<>();
1307 Set<String> moved = new TreeSet<>();
1308
1309 /** Append changes to the A2-ORIGIN/changes file. */
1310 void appendChanges(Path baseDirectory) throws IOException {
1311 Path changesFile = baseDirectory.resolve("A2-ORIGIN/changes");
1312 Files.createDirectories(changesFile.getParent());
1313 try (BufferedWriter writer = Files.newBufferedWriter(changesFile, StandardOpenOption.APPEND,
1314 StandardOpenOption.CREATE)) {
1315 for (String msg : added)
1316 writer.write("- Added " + msg + ".\n");
1317 for (String msg : modified)
1318 writer.write("- Modified " + msg + ".\n");
1319 for (String msg : moved)
1320 writer.write("- Moved " + msg + ".\n");
1321 for (String msg : deleted)
1322 writer.write("- Deleted " + msg + ".\n");
1323 }
1324 }
1325 }
1326
1327 /** Simple representation of an M2 artifact. */
1328 class M2Artifact extends CategoryNameVersion {
1329 private String classifier;
1330
1331 M2Artifact(String m2coordinates) {
1332 this(m2coordinates, null);
1333 }
1334
1335 M2Artifact(String m2coordinates, String classifier) {
1336 String[] parts = m2coordinates.split(":");
1337 setCategory(parts[0]);
1338 setName(parts[1]);
1339 if (parts.length > 2) {
1340 setVersion(parts[2]);
1341 }
1342 this.classifier = classifier;
1343 }
1344
1345 String getGroupId() {
1346 return super.getCategory();
1347 }
1348
1349 String getArtifactId() {
1350 return super.getName();
1351 }
1352
1353 String toM2Coordinates() {
1354 return getCategory() + ":" + getName() + (getVersion() != null ? ":" + getVersion() : "");
1355 }
1356
1357 String getClassifier() {
1358 return classifier != null ? classifier : "";
1359 }
1360
1361 String getExtension() {
1362 return "jar";
1363 }
1364 }
1365
1366 /** Utilities around Maven (conventions based). */
1367 class M2ConventionsUtils {
1368 final static String MAVEN_CENTRAL_BASE_URL = "https://repo1.maven.org/maven2/";
1369
1370 /** The file name of this artifact when stored */
1371 static String artifactFileName(M2Artifact artifact) {
1372 return artifact.getArtifactId() + '-' + artifact.getVersion()
1373 + (artifact.getClassifier().equals("") ? "" : '-' + artifact.getClassifier()) + '.'
1374 + artifact.getExtension();
1375 }
1376
1377 /** Absolute path to the file */
1378 static String artifactPath(String artifactBasePath, M2Artifact artifact) {
1379 return artifactParentPath(artifactBasePath, artifact) + '/' + artifactFileName(artifact);
1380 }
1381
1382 /** Absolute path to the file */
1383 static String artifactUrl(String repoUrl, M2Artifact artifact) {
1384 if (repoUrl.endsWith("/"))
1385 return repoUrl + artifactPath("/", artifact).substring(1);
1386 else
1387 return repoUrl + artifactPath("/", artifact);
1388 }
1389
1390 /** Absolute path to the file */
1391 static URL mavenRepoUrl(String repoBase, M2Artifact artifact) {
1392 String url = artifactUrl(repoBase == null ? MAVEN_CENTRAL_BASE_URL : repoBase, artifact);
1393 try {
1394 return new URL(url);
1395 } catch (MalformedURLException e) {
1396 // it should not happen
1397 throw new IllegalStateException(e);
1398 }
1399 }
1400
1401 /** Absolute path to the directories where the files will be stored */
1402 static String artifactParentPath(String artifactBasePath, M2Artifact artifact) {
1403 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact);
1404 }
1405
1406 /** Relative path to the directories where the files will be stored */
1407 static String artifactParentPath(M2Artifact artifact) {
1408 return artifact.getGroupId().replace('.', '/') + '/' + artifact.getArtifactId() + '/' + artifact.getVersion();
1409 }
1410
1411 /** Singleton */
1412 private M2ConventionsUtils() {
1413 }
1414 }
1415
1416 /** Combination of a category, a name and a version. */
1417 class CategoryNameVersion extends NameVersion {
1418 private String category;
1419
1420 CategoryNameVersion() {
1421 }
1422
1423 CategoryNameVersion(String category, String name, String version) {
1424 super(name, version);
1425 this.category = category;
1426 }
1427
1428 CategoryNameVersion(String category, NameVersion nameVersion) {
1429 super(nameVersion);
1430 this.category = category;
1431 }
1432
1433 String getCategory() {
1434 return category;
1435 }
1436
1437 void setCategory(String category) {
1438 this.category = category;
1439 }
1440
1441 @Override
1442 public String toString() {
1443 return category + ":" + super.toString();
1444 }
1445
1446 }
1447
1448 /** Combination of a name and a version. */
1449 class NameVersion implements Comparable<NameVersion> {
1450 private String name;
1451 private String version;
1452
1453 NameVersion() {
1454 }
1455
1456 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1457 NameVersion(String nameVersion) {
1458 int index = nameVersion.indexOf(";version=");
1459 if (index < 0) {
1460 setName(nameVersion);
1461 setVersion(null);
1462 } else {
1463 setName(nameVersion.substring(0, index));
1464 setVersion(nameVersion.substring(index + ";version=".length()));
1465 }
1466 }
1467
1468 NameVersion(String name, String version) {
1469 this.name = name;
1470 this.version = version;
1471 }
1472
1473 NameVersion(NameVersion nameVersion) {
1474 this.name = nameVersion.getName();
1475 this.version = nameVersion.getVersion();
1476 }
1477
1478 String getName() {
1479 return name;
1480 }
1481
1482 void setName(String name) {
1483 this.name = name;
1484 }
1485
1486 String getVersion() {
1487 return version;
1488 }
1489
1490 void setVersion(String version) {
1491 this.version = version;
1492 }
1493
1494 String getBranch() {
1495 String[] parts = getVersion().split("\\.");
1496 if (parts.length < 2)
1497 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1498 return parts[0] + "." + parts[1];
1499 }
1500
1501 @Override
1502 public boolean equals(Object obj) {
1503 if (obj instanceof NameVersion) {
1504 NameVersion nameVersion = (NameVersion) obj;
1505 return name.equals(nameVersion.getName()) && version.equals(nameVersion.getVersion());
1506 } else
1507 return false;
1508 }
1509
1510 @Override
1511 public int hashCode() {
1512 return name.hashCode();
1513 }
1514
1515 @Override
1516 public String toString() {
1517 return name + ":" + version;
1518 }
1519
1520 public int compareTo(NameVersion o) {
1521 if (o.getName().equals(name))
1522 return version.compareTo(o.getVersion());
1523 else
1524 return name.compareTo(o.getName());
1525 }
1526 }