]> git.argeo.org Git - cc0/argeo-build.git/blob - Repackage.java
de99e5f8c453af44d532d69f67db3952ddafc204
[cc0/argeo-build.git] / Repackage.java
1 package org.argeo.build;
2
3 import static java.lang.System.Logger.Level.DEBUG;
4 import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_SYMBOLICNAME;
5 import static org.argeo.build.Repackage.ManifestConstants.BUNDLE_VERSION;
6 import static org.argeo.build.Repackage.ManifestConstants.EXPORT_PACKAGE;
7 import static org.argeo.build.Repackage.ManifestConstants.SLC_ORIGIN_M2;
8 import static org.argeo.build.Repackage.ManifestConstants.SLC_ORIGIN_M2_REPO;
9
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.lang.System.Logger;
15 import java.lang.System.Logger.Level;
16 import java.net.MalformedURLException;
17 import java.net.URL;
18 import java.nio.charset.StandardCharsets;
19 import java.nio.file.DirectoryStream;
20 import java.nio.file.FileSystem;
21 import java.nio.file.FileSystems;
22 import java.nio.file.FileVisitResult;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.PathMatcher;
26 import java.nio.file.Paths;
27 import java.nio.file.SimpleFileVisitor;
28 import java.nio.file.StandardOpenOption;
29 import java.nio.file.attribute.BasicFileAttributes;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.Properties;
37 import java.util.TreeMap;
38 import java.util.jar.Attributes;
39 import java.util.jar.JarEntry;
40 import java.util.jar.JarInputStream;
41 import java.util.jar.JarOutputStream;
42 import java.util.jar.Manifest;
43 import java.util.zip.Deflater;
44
45 import aQute.bnd.osgi.Analyzer;
46 import aQute.bnd.osgi.Jar;
47
48 /** The central class for A2 packaging. */
49 public class Repackage {
50 private final static Logger logger = System.getLogger(Repackage.class.getName());
51
52 /** Main entry point. */
53 public static void main(String[] args) {
54 if (args.length < 2) {
55 System.err.println("Usage: <path to a2 output dir> <category1> <category2> ...");
56 System.exit(1);
57 }
58 Path a2Base = Paths.get(args[0]).toAbsolutePath().normalize();
59 Path descriptorsBase = Paths.get(".").toAbsolutePath().normalize();
60 Repackage factory = new Repackage(a2Base, descriptorsBase, true);
61
62 for (int i = 1; i < args.length; i++) {
63 Path p = Paths.get(args[i]);
64 factory.processCategory(p);
65 }
66 }
67
68 private final static String COMMON_BND = "common.bnd";
69 private final static String MERGE_BND = "merge.bnd";
70
71 private Path originBase;
72 private Path a2Base;
73 private Path a2LibBase;
74 private Path descriptorsBase;
75
76 private Properties uris = new Properties();
77
78 private boolean includeSources = true;
79
80 /** key is URI prefix, value list of base URLs */
81 private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
82
83 public Repackage(Path a2Base, Path descriptorsBase, boolean includeSources) {
84 Objects.requireNonNull(a2Base);
85 Objects.requireNonNull(descriptorsBase);
86 this.originBase = Paths.get(System.getProperty("user.home"), ".cache", "argeo/build/origin");
87 // TODO define and use a build base
88 this.a2Base = a2Base;
89 this.a2LibBase = a2Base.resolve("lib");
90 this.descriptorsBase = descriptorsBase;
91 if (!Files.exists(this.descriptorsBase))
92 throw new IllegalArgumentException(this.descriptorsBase + " does not exist");
93 this.includeSources = includeSources;
94
95 // URIs mapping
96 Path urisPath = this.descriptorsBase.resolve("uris.properties");
97 if (Files.exists(urisPath)) {
98 try (InputStream in = Files.newInputStream(urisPath)) {
99 uris.load(in);
100 } catch (IOException e) {
101 throw new IllegalStateException("Cannot load " + urisPath, e);
102 }
103 }
104
105 // Eclipse mirrors
106 Path eclipseMirrorsPath = this.descriptorsBase.resolve("eclipse.mirrors.txt");
107 List<String> eclipseMirrors = new ArrayList<>();
108 if (Files.exists(eclipseMirrorsPath)) {
109 try {
110 eclipseMirrors = Files.readAllLines(eclipseMirrorsPath, StandardCharsets.UTF_8);
111 } catch (IOException e) {
112 throw new IllegalStateException("Cannot load " + eclipseMirrorsPath, e);
113 }
114 for (Iterator<String> it = eclipseMirrors.iterator(); it.hasNext();) {
115 String value = it.next();
116 if (value.strip().equals(""))
117 it.remove();
118 }
119 }
120
121 mirrors.put("http://www.eclipse.org/downloads", eclipseMirrors);
122 }
123
124 /*
125 * MAVEN ORIGIN
126 */
127
128 /** Process a whole category/group id. */
129 public void processCategory(Path categoryRelativePath) {
130 try {
131 Path targetCategoryBase = descriptorsBase.resolve(categoryRelativePath);
132 DirectoryStream<Path> bnds = Files.newDirectoryStream(targetCategoryBase,
133 (p) -> p.getFileName().toString().endsWith(".bnd") && !p.getFileName().toString().equals(COMMON_BND)
134 && !p.getFileName().toString().equals(MERGE_BND));
135 for (Path p : bnds) {
136 processSingleM2ArtifactDistributionUnit(p);
137 }
138
139 DirectoryStream<Path> dus = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
140 for (Path duDir : dus) {
141 if (duDir.getFileName().toString().startsWith("eclipse-")) {
142 processEclipseArchive(duDir);
143 } else {
144 processM2BasedDistributionUnit(duDir);
145 }
146 }
147 } catch (IOException e) {
148 throw new RuntimeException("Cannot process category " + categoryRelativePath, e);
149 }
150 }
151
152 /** Process a standalone Maven artifact. */
153 public void processSingleM2ArtifactDistributionUnit(Path bndFile) {
154 try {
155 // String category = bndFile.getParent().getFileName().toString();
156 Path categoryRelativePath = descriptorsBase.relativize(bndFile.getParent());
157 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
158
159 Properties fileProps = new Properties();
160 try (InputStream in = Files.newInputStream(bndFile)) {
161 fileProps.load(in);
162 }
163 String repoStr = fileProps.containsKey(SLC_ORIGIN_M2_REPO.toString())
164 ? fileProps.getProperty(SLC_ORIGIN_M2_REPO.toString())
165 : null;
166
167 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
168 // use file name as symbolic name
169 String symbolicName = bndFile.getFileName().toString();
170 symbolicName = symbolicName.substring(0, symbolicName.length() - ".bnd".length());
171 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), symbolicName);
172 }
173
174 String m2Coordinates = fileProps.getProperty(SLC_ORIGIN_M2.toString());
175 if (m2Coordinates == null)
176 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile);
177 M2Artifact artifact = new M2Artifact(m2Coordinates);
178 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
179 Path downloaded = download(url, originBase, artifact);
180
181 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact);
182
183 downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir);
184
185 createJar(targetBundleDir);
186 } catch (Exception e) {
187 throw new RuntimeException("Cannot process " + bndFile, e);
188 }
189 }
190
191 /** Process multiple Maven artifacts. */
192 public void processM2BasedDistributionUnit(Path duDir) {
193 try {
194 // String category = duDir.getParent().getFileName().toString();
195 Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent());
196 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
197
198 // merge
199 Path mergeBnd = duDir.resolve(MERGE_BND);
200 if (Files.exists(mergeBnd)) {
201 mergeM2Artifacts(mergeBnd);
202 // return;
203 }
204
205 Path commonBnd = duDir.resolve(COMMON_BND);
206 if (!Files.exists(commonBnd)) {
207 return;
208 }
209 Properties commonProps = new Properties();
210 try (InputStream in = Files.newInputStream(commonBnd)) {
211 commonProps.load(in);
212 }
213
214 String m2Version = commonProps.getProperty(SLC_ORIGIN_M2.toString());
215 if (m2Version == null) {
216 logger.log(Level.WARNING, "Ignoring " + duDir + " as it is not an M2-based distribution unit");
217 return;// ignore, this is probably an Eclipse archive
218 }
219 if (!m2Version.startsWith(":")) {
220 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
221 }
222 m2Version = m2Version.substring(1);
223
224 DirectoryStream<Path> ds = Files.newDirectoryStream(duDir,
225 (p) -> p.getFileName().toString().endsWith(".bnd") && !p.getFileName().toString().equals(COMMON_BND)
226 && !p.getFileName().toString().equals(MERGE_BND));
227 for (Path p : ds) {
228 Properties fileProps = new Properties();
229 try (InputStream in = Files.newInputStream(p)) {
230 fileProps.load(in);
231 }
232 String m2Coordinates = fileProps.getProperty(SLC_ORIGIN_M2.toString());
233 M2Artifact artifact = new M2Artifact(m2Coordinates);
234
235 artifact.setVersion(m2Version);
236
237 // prepare manifest entries
238 Properties mergeProps = new Properties();
239 mergeProps.putAll(commonProps);
240
241 fileEntries: for (Object key : fileProps.keySet()) {
242 if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
243 continue fileEntries;
244 String value = fileProps.getProperty(key.toString());
245 Object previousValue = mergeProps.put(key.toString(), value);
246 if (previousValue != null) {
247 logger.log(Level.WARNING,
248 commonBnd + ": " + key + " was " + previousValue + ", overridden with " + value);
249 }
250 }
251 mergeProps.put(ManifestConstants.SLC_ORIGIN_M2.toString(), artifact.toM2Coordinates());
252 if (!mergeProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
253 // use file name as symbolic name
254 String symbolicName = p.getFileName().toString();
255 symbolicName = symbolicName.substring(0, symbolicName.length() - ".bnd".length());
256 mergeProps.put(BUNDLE_SYMBOLICNAME.toString(), symbolicName);
257 }
258
259 String repoStr = mergeProps.containsKey(SLC_ORIGIN_M2_REPO.toString())
260 ? mergeProps.getProperty(SLC_ORIGIN_M2_REPO.toString())
261 : null;
262
263 // download
264 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
265 Path downloaded = download(url, originBase, artifact);
266
267 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergeProps, artifact);
268 // logger.log(Level.DEBUG, () -> "Processed " + downloaded);
269
270 // sources
271 downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir);
272
273 createJar(targetBundleDir);
274 }
275 } catch (IOException e) {
276 throw new RuntimeException("Cannot process " + duDir, e);
277 }
278
279 }
280
281 /** Merge multiple Maven artifacts. */
282 protected void mergeM2Artifacts(Path mergeBnd) throws IOException {
283 Path duDir = mergeBnd.getParent();
284 String category = duDir.getParent().getFileName().toString();
285 Path targetCategoryBase = a2Base.resolve(category);
286
287 Properties mergeProps = new Properties();
288 try (InputStream in = Files.newInputStream(mergeBnd)) {
289 mergeProps.load(in);
290 }
291
292 // Version
293 String m2Version = mergeProps.getProperty(SLC_ORIGIN_M2.toString());
294 if (m2Version == null) {
295 logger.log(Level.WARNING, "Ignoring " + duDir + " as it is not an M2-based distribution unit");
296 return;// ignore, this is probably an Eclipse archive
297 }
298 if (!m2Version.startsWith(":")) {
299 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
300 }
301 m2Version = m2Version.substring(1);
302 mergeProps.put(ManifestConstants.BUNDLE_VERSION.toString(), m2Version);
303
304 String artifactsStr = mergeProps.getProperty(ManifestConstants.SLC_ORIGIN_M2_MERGE.toString());
305 String repoStr = mergeProps.containsKey(SLC_ORIGIN_M2_REPO.toString())
306 ? mergeProps.getProperty(SLC_ORIGIN_M2_REPO.toString())
307 : null;
308
309 String bundleSymbolicName = mergeProps.getProperty(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
310 if (bundleSymbolicName == null)
311 throw new IllegalArgumentException("Bundle-SymbolicName must be set in " + mergeBnd);
312 CategoryNameVersion nameVersion = new M2Artifact(category + ":" + bundleSymbolicName + ":" + m2Version);
313 Path targetBundleDir = targetCategoryBase.resolve(bundleSymbolicName + "." + nameVersion.getBranch());
314
315 String[] artifacts = artifactsStr.split(",");
316 artifacts: for (String str : artifacts) {
317 String m2Coordinates = str.trim();
318 if ("".equals(m2Coordinates))
319 continue artifacts;
320 M2Artifact artifact = new M2Artifact(m2Coordinates.trim());
321 if (artifact.getVersion() == null)
322 artifact.setVersion(m2Version);
323 URL url = M2ConventionsUtils.mavenRepoUrl(repoStr, artifact);
324 Path downloaded = download(url, originBase, artifact);
325 JarEntry entry;
326 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(downloaded), false)) {
327 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
328 if (entry.isDirectory())
329 continue entries;
330 else if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
331 continue entries;
332 else if (entry.getName().startsWith("META-INF/versions/"))
333 continue entries;
334 else if (entry.getName().startsWith("META-INF/maven/"))
335 continue entries;
336 else if (entry.getName().equals("module-info.class"))
337 continue entries;
338 else if (entry.getName().equals("META-INF/NOTICE"))
339 continue entries;
340 else if (entry.getName().equals("META-INF/NOTICE.txt"))
341 continue entries;
342 else if (entry.getName().equals("META-INF/LICENSE"))
343 continue entries;
344 else if (entry.getName().equals("META-INF/LICENSE.md"))
345 continue entries;
346 else if (entry.getName().equals("META-INF/LICENSE-notice.md"))
347 continue entries;
348 else if (entry.getName().equals("META-INF/DEPENDENCIES"))
349 continue entries;
350 if (entry.getName().startsWith(".cache/")) // Apache SSHD
351 continue entries;
352 Path target = targetBundleDir.resolve(entry.getName());
353 Files.createDirectories(target.getParent());
354 if (!Files.exists(target)) {
355 Files.copy(jarIn, target);
356 } else {
357 if (entry.getName().startsWith("META-INF/services/")) {
358 try (OutputStream out = Files.newOutputStream(target, StandardOpenOption.APPEND)) {
359 out.write("\n".getBytes());
360 jarIn.transferTo(out);
361 if (logger.isLoggable(DEBUG))
362 logger.log(DEBUG, artifact.getArtifactId() + " - Appended " + entry.getName());
363 }
364 } else if (entry.getName().startsWith("org/apache/batik/")) {
365 logger.log(Level.WARNING, "Skip " + entry.getName());
366 continue entries;
367 } else {
368 throw new IllegalStateException("File " + target + " from " + artifact + " already exists");
369 }
370 }
371 logger.log(Level.TRACE, () -> "Copied " + target);
372 }
373
374 }
375 downloadAndProcessM2Sources(repoStr, artifact, targetBundleDir);
376 }
377
378 // additional service files
379 Path servicesDir = duDir.resolve("services");
380 if (Files.exists(servicesDir)) {
381 for (Path p : Files.newDirectoryStream(servicesDir)) {
382 Path target = targetBundleDir.resolve("META-INF/services/").resolve(p.getFileName());
383 try (InputStream in = Files.newInputStream(p);
384 OutputStream out = Files.newOutputStream(target, StandardOpenOption.APPEND);) {
385 out.write("\n".getBytes());
386 in.transferTo(out);
387 if (logger.isLoggable(DEBUG))
388 logger.log(DEBUG, "Appended " + p);
389 }
390 }
391 }
392
393 Map<String, String> entries = new TreeMap<>();
394 try (Analyzer bndAnalyzer = new Analyzer()) {
395 bndAnalyzer.setProperties(mergeProps);
396 Jar jar = new Jar(targetBundleDir.toFile());
397 bndAnalyzer.setJar(jar);
398 Manifest manifest = bndAnalyzer.calcManifest();
399
400 keys: for (Object key : manifest.getMainAttributes().keySet()) {
401 Object value = manifest.getMainAttributes().get(key);
402
403 switch (key.toString()) {
404 case "Tool":
405 case "Bnd-LastModified":
406 case "Created-By":
407 continue keys;
408 }
409 if ("Require-Capability".equals(key.toString())
410 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
411 continue keys;// hack for very old classes
412 entries.put(key.toString(), value.toString());
413 // logger.log(DEBUG, () -> key + "=" + value);
414
415 }
416 } catch (Exception e) {
417 throw new RuntimeException("Cannot process " + mergeBnd, e);
418 }
419
420 Manifest manifest = new Manifest();
421 Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
422 Files.createDirectories(manifestPath.getParent());
423 for (String key : entries.keySet()) {
424 String value = entries.get(key);
425 manifest.getMainAttributes().putValue(key, value);
426 }
427
428 try (OutputStream out = Files.newOutputStream(manifestPath)) {
429 manifest.write(out);
430 }
431 createJar(targetBundleDir);
432 }
433
434 /** Generate MANIFEST using BND. */
435 protected Path processBndJar(Path downloaded, Path targetCategoryBase, Properties fileProps, M2Artifact artifact) {
436
437 try {
438 Map<String, String> additionalEntries = new TreeMap<>();
439 boolean doNotModify = Boolean.parseBoolean(fileProps
440 .getOrDefault(ManifestConstants.SLC_ORIGIN_MANIFEST_NOT_MODIFIED.toString(), "false").toString());
441
442 // we always force the symbolic name
443
444 if (doNotModify) {
445 fileEntries: for (Object key : fileProps.keySet()) {
446 if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
447 continue fileEntries;
448 String value = fileProps.getProperty(key.toString());
449 additionalEntries.put(key.toString(), value);
450 }
451 } else {
452 if (artifact != null) {
453 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
454 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), artifact.getName());
455 }
456 if (!fileProps.containsKey(BUNDLE_VERSION.toString())) {
457 fileProps.put(BUNDLE_VERSION.toString(), artifact.getVersion());
458 }
459 }
460
461 if (!fileProps.containsKey(EXPORT_PACKAGE.toString())) {
462 fileProps.put(EXPORT_PACKAGE.toString(),
463 "*;version=\"" + fileProps.getProperty(BUNDLE_VERSION.toString()) + "\"");
464 }
465
466 try (Analyzer bndAnalyzer = new Analyzer()) {
467 bndAnalyzer.setProperties(fileProps);
468 Jar jar = new Jar(downloaded.toFile());
469 bndAnalyzer.setJar(jar);
470 Manifest manifest = bndAnalyzer.calcManifest();
471
472 keys: for (Object key : manifest.getMainAttributes().keySet()) {
473 Object value = manifest.getMainAttributes().get(key);
474
475 switch (key.toString()) {
476 case "Tool":
477 case "Bnd-LastModified":
478 case "Created-By":
479 continue keys;
480 }
481 if ("Require-Capability".equals(key.toString())
482 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
483 continue keys;// hack for very old classes
484 additionalEntries.put(key.toString(), value.toString());
485 // logger.log(DEBUG, () -> key + "=" + value);
486
487 }
488 }
489 }
490 Path targetBundleDir = processBundleJar(downloaded, targetCategoryBase, additionalEntries);
491 logger.log(Level.DEBUG, () -> "Processed " + downloaded);
492 return targetBundleDir;
493 } catch (Exception e) {
494 throw new RuntimeException("Cannot BND process " + downloaded, e);
495 }
496
497 }
498
499 /** Download and integrates sources for a single Maven artifact. */
500 protected void downloadAndProcessM2Sources(String repoStr, M2Artifact artifact, Path targetBundleDir)
501 throws IOException {
502 if (!includeSources)
503 return;
504 M2Artifact sourcesArtifact = new M2Artifact(artifact.toM2Coordinates(), "sources");
505 URL sourcesUrl = M2ConventionsUtils.mavenRepoUrl(repoStr, sourcesArtifact);
506 Path sourcesDownloaded = download(sourcesUrl, originBase, artifact, true);
507 processM2SourceJar(sourcesDownloaded, targetBundleDir);
508 logger.log(Level.TRACE, () -> "Processed source " + sourcesDownloaded);
509
510 }
511
512 /** Integrate sources from a downloaded jar file. */
513 protected void processM2SourceJar(Path file, Path targetBundleDir) throws IOException {
514 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
515 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
516
517 // TODO make it less dangerous?
518 if (Files.exists(targetSourceDir)) {
519 // deleteDirectory(targetSourceDir);
520 } else {
521 Files.createDirectories(targetSourceDir);
522 }
523
524 // copy entries
525 JarEntry entry;
526 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
527 if (entry.isDirectory())
528 continue entries;
529 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
530 continue entries;
531 if (entry.getName().startsWith("module-info.java"))// skip META-INF entries
532 continue entries;
533 if (entry.getName().startsWith("/")) // absolute paths
534 continue entries;
535 Path target = targetSourceDir.resolve(entry.getName());
536 Files.createDirectories(target.getParent());
537 if (!Files.exists(target)) {
538 Files.copy(jarIn, target);
539 logger.log(Level.TRACE, () -> "Copied source " + target);
540 } else {
541 logger.log(Level.WARNING, () -> target + " already exists, skipping...");
542 }
543 }
544 }
545
546 }
547
548 /** Download a Maven artifact. */
549 protected Path download(URL url, Path dir, M2Artifact artifact) throws IOException {
550 return download(url, dir, artifact, false);
551 }
552
553 /** Download a Maven artifact. */
554 protected Path download(URL url, Path dir, M2Artifact artifact, boolean sources) throws IOException {
555 return download(url, dir, artifact.getGroupId() + '/' + artifact.getArtifactId() + "-" + artifact.getVersion()
556 + (sources ? "-sources" : "") + ".jar");
557 }
558
559 /*
560 * ECLIPSE ORIGIN
561 */
562
563 /** Process an archive in Eclipse format. */
564 public void processEclipseArchive(Path duDir) {
565 try {
566 Path categoryRelativePath = descriptorsBase.relativize(duDir.getParent());
567 // String category = categoryRelativePath.getFileName().toString();
568 Path targetCategoryBase = a2Base.resolve(categoryRelativePath);
569 Files.createDirectories(targetCategoryBase);
570 // first delete all directories from previous builds
571 for (Path dir : Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p))) {
572 deleteDirectory(dir);
573 }
574
575 Files.createDirectories(originBase);
576
577 Path commonBnd = duDir.resolve(COMMON_BND);
578 Properties commonProps = new Properties();
579 try (InputStream in = Files.newInputStream(commonBnd)) {
580 commonProps.load(in);
581 }
582 String url = commonProps.getProperty(ManifestConstants.SLC_ORIGIN_URI.toString());
583 if (url == null) {
584 url = uris.getProperty(duDir.getFileName().toString());
585 if (url == null)
586 throw new IllegalStateException("No url available for " + duDir);
587 commonProps.put(ManifestConstants.SLC_ORIGIN_URI.toString(), url);
588 }
589 Path downloaded = tryDownload(url, originBase);
590
591 FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null);
592
593 // filters
594 List<PathMatcher> includeMatchers = new ArrayList<>();
595 Properties includes = new Properties();
596 try (InputStream in = Files.newInputStream(duDir.resolve("includes.properties"))) {
597 includes.load(in);
598 }
599 for (Object pattern : includes.keySet()) {
600 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
601 includeMatchers.add(pathMatcher);
602 }
603
604 List<PathMatcher> excludeMatchers = new ArrayList<>();
605 Path excludeFile = duDir.resolve("excludes.properties");
606 if (Files.exists(excludeFile)) {
607 Properties excludes = new Properties();
608 try (InputStream in = Files.newInputStream(excludeFile)) {
609 excludes.load(in);
610 }
611 for (Object pattern : excludes.keySet()) {
612 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
613 excludeMatchers.add(pathMatcher);
614 }
615 }
616
617 Files.walkFileTree(zipFs.getRootDirectories().iterator().next(), new SimpleFileVisitor<Path>() {
618
619 @Override
620 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
621 includeMatchers: for (PathMatcher includeMatcher : includeMatchers) {
622 if (includeMatcher.matches(file)) {
623 for (PathMatcher excludeMatcher : excludeMatchers) {
624 if (excludeMatcher.matches(file)) {
625 logger.log(Level.TRACE, "Skipping excluded " + file);
626 return FileVisitResult.CONTINUE;
627 }
628 }
629 if (includeSources && file.getFileName().toString().contains(".source_")) {
630 processEclipseSourceJar(file, targetCategoryBase);
631 logger.log(Level.DEBUG, () -> "Processed source " + file);
632
633 } else {
634 Map<String, String> map = new HashMap<>();
635 for (Object key : commonProps.keySet())
636 map.put(key.toString(), commonProps.getProperty(key.toString()));
637 processBundleJar(file, targetCategoryBase, map);
638 logger.log(Level.DEBUG, () -> "Processed " + file);
639 }
640 break includeMatchers;
641 }
642 }
643 return FileVisitResult.CONTINUE;
644 }
645 });
646
647 DirectoryStream<Path> dirs = Files.newDirectoryStream(targetCategoryBase,
648 (p) -> Files.isDirectory(p) && p.getFileName().toString().indexOf('.') >= 0);
649 for (Path dir : dirs) {
650 createJar(dir);
651 }
652 } catch (IOException e) {
653 throw new RuntimeException("Cannot process " + duDir, e);
654 }
655
656 }
657
658 /** Process sources in Eclipse format. */
659 protected void processEclipseSourceJar(Path file, Path targetBase) throws IOException {
660 try {
661 Path targetBundleDir;
662 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
663 Manifest manifest = jarIn.getManifest();
664
665 String[] relatedBundle = manifest.getMainAttributes().getValue("Eclipse-SourceBundle").split(";");
666 String version = relatedBundle[1].substring("version=\"".length());
667 version = version.substring(0, version.length() - 1);
668 NameVersion nameVersion = new NameVersion(relatedBundle[0], version);
669 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
670
671 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
672
673 // TODO make it less dangerous?
674 if (Files.exists(targetSourceDir)) {
675 // deleteDirectory(targetSourceDir);
676 } else {
677 Files.createDirectories(targetSourceDir);
678 }
679
680 // copy entries
681 JarEntry entry;
682 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
683 if (entry.isDirectory())
684 continue entries;
685 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
686 continue entries;
687 Path target = targetSourceDir.resolve(entry.getName());
688 Files.createDirectories(target.getParent());
689 Files.copy(jarIn, target);
690 logger.log(Level.TRACE, () -> "Copied source " + target);
691 }
692
693 // copy MANIFEST
694 }
695 } catch (IOException e) {
696 throw new IllegalStateException("Cannot process " + file, e);
697 }
698
699 }
700
701 /*
702 * COMMON PROCESSING
703 */
704 /** Normalise a bundle. */
705 protected Path processBundleJar(Path file, Path targetBase, Map<String, String> entries) throws IOException {
706 NameVersion nameVersion;
707 Path targetBundleDir;
708 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
709 Manifest sourceManifest = jarIn.getManifest();
710 Manifest manifest = sourceManifest != null ? new Manifest(sourceManifest) : new Manifest();
711
712 // singleton
713 boolean isSingleton = false;
714 String rawSourceSymbolicName = manifest.getMainAttributes()
715 .getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
716 if (rawSourceSymbolicName != null) {
717
718 // make sure there is no directive
719 String[] arr = rawSourceSymbolicName.split(";");
720 for (int i = 1; i < arr.length; i++) {
721 if (arr[i].trim().equals("singleton:=true"))
722 isSingleton = true;
723 logger.log(DEBUG, file.getFileName() + " is a singleton");
724 }
725 }
726
727 // remove problematic entries in MANIFEST
728 manifest.getEntries().clear();
729
730 String ourSymbolicName = entries.get(BUNDLE_SYMBOLICNAME.toString());
731 String ourVersion = entries.get(BUNDLE_VERSION.toString());
732
733 if (ourSymbolicName != null && ourVersion != null) {
734 nameVersion = new NameVersion(ourSymbolicName, ourVersion);
735 } else {
736 nameVersion = nameVersionFromManifest(manifest);
737 if (ourVersion != null && !nameVersion.getVersion().equals(ourVersion)) {
738 logger.log(Level.WARNING,
739 "Original version is " + nameVersion.getVersion() + " while new version is " + ourVersion);
740 entries.put(BUNDLE_VERSION.toString(), ourVersion);
741 }
742 if (ourSymbolicName != null) {
743 // we always force our symbolic name
744 nameVersion.setName(ourSymbolicName);
745 }
746 }
747 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
748
749 // force Java 9 module name
750 entries.put(ManifestConstants.AUTOMATIC_MODULE_NAME.toString(), nameVersion.getName());
751
752 boolean isNative = false;
753 String os = null;
754 String arch = null;
755 if (targetBundleDir.startsWith(a2LibBase)) {
756 isNative = true;
757 Path libRelativePath = a2LibBase.relativize(targetBundleDir);
758 os = libRelativePath.getName(0).toString();
759 arch = libRelativePath.getName(1).toString();
760 }
761
762 // copy entries
763 JarEntry entry;
764 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
765 if (entry.isDirectory())
766 continue entries;
767 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
768 continue entries;
769 if (entry.getName().endsWith("module-info.class")) // skip Java 9 module info
770 continue entries;
771 if (entry.getName().startsWith("META-INF/versions/")) // skip multi-version
772 continue entries;
773 // skip file system providers as they cause issues with native image
774 if (entry.getName().startsWith("META-INF/services/java.nio.file.spi.FileSystemProvider"))
775 continue entries;
776 if (entry.getName().startsWith("OSGI-OPT/src/")) // skip embedded sources
777 continue entries;
778 Path target = targetBundleDir.resolve(entry.getName());
779 Files.createDirectories(target.getParent());
780 Files.copy(jarIn, target);
781
782 // native libraries
783 if (isNative && (entry.getName().endsWith(".so") || entry.getName().endsWith(".dll")
784 || entry.getName().endsWith(".jnilib"))) {
785 Path categoryDir = targetBundleDir.getParent();
786 // String[] segments = categoryDir.getFileName().toString().split("\\.");
787 // String arch = segments[segments.length - 1];
788 // String os = segments[segments.length - 2];
789 boolean copyDll = false;
790 Path targetDll = categoryDir.resolve(targetBundleDir.relativize(target));
791 if (nameVersion.getName().equals("com.sun.jna")) {
792 if (arch.equals("x86_64"))
793 arch = "x86-64";
794 if (os.equals("macosx"))
795 os = "darwin";
796 if (target.getParent().getFileName().toString().equals(os + "-" + arch)) {
797 copyDll = true;
798 }
799 targetDll = categoryDir.resolve(target.getFileName());
800 } else {
801 copyDll = true;
802 }
803 if (copyDll) {
804 Files.createDirectories(targetDll.getParent());
805 if (Files.exists(targetDll))
806 Files.delete(targetDll);
807 Files.copy(target, targetDll);
808 }
809 Files.delete(target);
810 }
811 logger.log(Level.TRACE, () -> "Copied " + target);
812 }
813
814 // copy MANIFEST
815 Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
816 Files.createDirectories(manifestPath.getParent());
817
818 if (isSingleton && entries.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
819 entries.put(BUNDLE_SYMBOLICNAME.toString(),
820 entries.get(BUNDLE_SYMBOLICNAME.toString()) + ";singleton:=true");
821 }
822
823 for (String key : entries.keySet()) {
824 String value = entries.get(key);
825 Object previousValue = manifest.getMainAttributes().putValue(key, value);
826 if (previousValue != null && !previousValue.equals(value)) {
827 if (ManifestConstants.IMPORT_PACKAGE.toString().equals(key)
828 || ManifestConstants.EXPORT_PACKAGE.toString().equals(key))
829 logger.log(Level.TRACE, file.getFileName() + ": " + key + " was modified");
830 else
831 logger.log(Level.WARNING, file.getFileName() + ": " + key + " was " + previousValue
832 + ", overridden with " + value);
833 }
834
835 // hack to remove unresolvable
836 if (key.equals("Provide-Capability") || key.equals("Require-Capability"))
837 if (nameVersion.getName().equals("osgi.core") || nameVersion.getName().equals("osgi.cmpn")) {
838 manifest.getMainAttributes().remove(key);
839 }
840 }
841 try (OutputStream out = Files.newOutputStream(manifestPath)) {
842 manifest.write(out);
843 }
844 }
845 return targetBundleDir;
846 }
847
848 /*
849 * UTILITIES
850 */
851
852 /** Recursively deletes a directory. */
853 private static void deleteDirectory(Path path) throws IOException {
854 if (!Files.exists(path))
855 return;
856 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
857 @Override
858 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
859 if (e != null)
860 throw e;
861 Files.delete(directory);
862 return FileVisitResult.CONTINUE;
863 }
864
865 @Override
866 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
867 Files.delete(file);
868 return FileVisitResult.CONTINUE;
869 }
870 });
871 }
872
873 /** Extract name/version from a MANIFEST. */
874 protected NameVersion nameVersionFromManifest(Manifest manifest) {
875 Attributes attrs = manifest.getMainAttributes();
876 // symbolic name
877 String symbolicName = attrs.getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
878 if (symbolicName == null)
879 return null;
880 // make sure there is no directive
881 symbolicName = symbolicName.split(";")[0];
882
883 String version = attrs.getValue(ManifestConstants.BUNDLE_VERSION.toString());
884 return new NameVersion(symbolicName, version);
885 }
886
887 /** Try to download from an URI. */
888 protected Path tryDownload(String uri, Path dir) throws IOException {
889 // find mirror
890 List<String> urlBases = null;
891 String uriPrefix = null;
892 uriPrefixes: for (String uriPref : mirrors.keySet()) {
893 if (uri.startsWith(uriPref)) {
894 if (mirrors.get(uriPref).size() > 0) {
895 urlBases = mirrors.get(uriPref);
896 uriPrefix = uriPref;
897 break uriPrefixes;
898 }
899 }
900 }
901 if (urlBases == null)
902 try {
903 return download(new URL(uri), dir);
904 } catch (FileNotFoundException e) {
905 throw new FileNotFoundException("Cannot find " + uri);
906 }
907
908 // try to download
909 for (String urlBase : urlBases) {
910 String relativePath = uri.substring(uriPrefix.length());
911 URL url = new URL(urlBase + relativePath);
912 try {
913 return download(url, dir);
914 } catch (FileNotFoundException e) {
915 logger.log(Level.WARNING, "Cannot download " + url + ", trying another mirror");
916 }
917 }
918 throw new FileNotFoundException("Cannot find " + uri);
919 }
920
921 /** Effectively download. */
922 protected Path download(URL url, Path dir) throws IOException {
923 return download(url, dir, (String) null);
924 }
925
926 /** Effectively download. */
927 protected Path download(URL url, Path dir, String name) throws IOException {
928
929 Path dest;
930 if (name == null) {
931 name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
932 }
933
934 dest = dir.resolve(name);
935 if (Files.exists(dest)) {
936 logger.log(Level.TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again");
937 return dest;
938 } else {
939 Files.createDirectories(dest.getParent());
940 }
941
942 try (InputStream in = url.openStream()) {
943 Files.copy(in, dest);
944 logger.log(Level.DEBUG, () -> "Downloaded " + dest + " from " + url);
945 }
946 return dest;
947 }
948
949 /** Create a JAR file from a directory. */
950 protected Path createJar(Path bundleDir) throws IOException {
951 // Create the jar
952 Path jarPath = bundleDir.getParent().resolve(bundleDir.getFileName() + ".jar");
953 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
954 Manifest manifest;
955 try (InputStream in = Files.newInputStream(manifestPath)) {
956 manifest = new Manifest(in);
957 }
958 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarPath), manifest)) {
959 jarOut.setLevel(Deflater.DEFAULT_COMPRESSION);
960 Files.walkFileTree(bundleDir, new SimpleFileVisitor<Path>() {
961
962 @Override
963 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
964 if (file.getFileName().toString().equals("MANIFEST.MF"))
965 return super.visitFile(file, attrs);
966 JarEntry entry = new JarEntry(bundleDir.relativize(file).toString());
967 jarOut.putNextEntry(entry);
968 Files.copy(file, jarOut);
969 return super.visitFile(file, attrs);
970 }
971
972 });
973 }
974 deleteDirectory(bundleDir);
975 return jarPath;
976 }
977
978 enum ManifestConstants {
979 // OSGi
980 BUNDLE_SYMBOLICNAME("Bundle-SymbolicName"), //
981 BUNDLE_VERSION("Bundle-Version"), //
982 BUNDLE_LICENSE("Bundle-License"), //
983 EXPORT_PACKAGE("Export-Package"), //
984 IMPORT_PACKAGE("Import-Package"), //
985 // JAVA
986 AUTOMATIC_MODULE_NAME("Automatic-Module-Name"), //
987 // SLC
988 SLC_CATEGORY("SLC-Category"), //
989 SLC_ORIGIN_M2("SLC-Origin-M2"), //
990 SLC_ORIGIN_M2_MERGE("SLC-Origin-M2-Merge"), //
991 SLC_ORIGIN_M2_REPO("SLC-Origin-M2-Repo"), //
992 SLC_ORIGIN_MANIFEST_NOT_MODIFIED("SLC-Origin-ManifestNotModified"), //
993 SLC_ORIGIN_URI("SLC-Origin-URI"),//
994 ;
995
996 final String value;
997
998 private ManifestConstants(String value) {
999 this.value = value;
1000 }
1001
1002 @Override
1003 public String toString() {
1004 return value;
1005 }
1006
1007 }
1008
1009 }
1010
1011 /** Simple representation of an M2 artifact. */
1012 class M2Artifact extends CategoryNameVersion {
1013 private String classifier;
1014
1015 M2Artifact(String m2coordinates) {
1016 this(m2coordinates, null);
1017 }
1018
1019 M2Artifact(String m2coordinates, String classifier) {
1020 String[] parts = m2coordinates.split(":");
1021 setCategory(parts[0]);
1022 setName(parts[1]);
1023 if (parts.length > 2) {
1024 setVersion(parts[2]);
1025 }
1026 this.classifier = classifier;
1027 }
1028
1029 String getGroupId() {
1030 return super.getCategory();
1031 }
1032
1033 String getArtifactId() {
1034 return super.getName();
1035 }
1036
1037 String toM2Coordinates() {
1038 return getCategory() + ":" + getName() + (getVersion() != null ? ":" + getVersion() : "");
1039 }
1040
1041 String getClassifier() {
1042 return classifier != null ? classifier : "";
1043 }
1044
1045 String getExtension() {
1046 return "jar";
1047 }
1048 }
1049
1050 /** Utilities around Maven (conventions based). */
1051 class M2ConventionsUtils {
1052 final static String MAVEN_CENTRAL_BASE_URL = "https://repo1.maven.org/maven2/";
1053
1054 /** The file name of this artifact when stored */
1055 static String artifactFileName(M2Artifact artifact) {
1056 return artifact.getArtifactId() + '-' + artifact.getVersion()
1057 + (artifact.getClassifier().equals("") ? "" : '-' + artifact.getClassifier()) + '.'
1058 + artifact.getExtension();
1059 }
1060
1061 /** Absolute path to the file */
1062 static String artifactPath(String artifactBasePath, M2Artifact artifact) {
1063 return artifactParentPath(artifactBasePath, artifact) + '/' + artifactFileName(artifact);
1064 }
1065
1066 /** Absolute path to the file */
1067 static String artifactUrl(String repoUrl, M2Artifact artifact) {
1068 if (repoUrl.endsWith("/"))
1069 return repoUrl + artifactPath("/", artifact).substring(1);
1070 else
1071 return repoUrl + artifactPath("/", artifact);
1072 }
1073
1074 /** Absolute path to the file */
1075 static URL mavenRepoUrl(String repoBase, M2Artifact artifact) {
1076 String url = artifactUrl(repoBase == null ? MAVEN_CENTRAL_BASE_URL : repoBase, artifact);
1077 try {
1078 return new URL(url);
1079 } catch (MalformedURLException e) {
1080 // it should not happen
1081 throw new IllegalStateException(e);
1082 }
1083 }
1084
1085 /** Absolute path to the directories where the files will be stored */
1086 static String artifactParentPath(String artifactBasePath, M2Artifact artifact) {
1087 return artifactBasePath + (artifactBasePath.endsWith("/") ? "" : "/") + artifactParentPath(artifact);
1088 }
1089
1090 /** Relative path to the directories where the files will be stored */
1091 static String artifactParentPath(M2Artifact artifact) {
1092 return artifact.getGroupId().replace('.', '/') + '/' + artifact.getArtifactId() + '/' + artifact.getVersion();
1093 }
1094
1095 /** Singleton */
1096 private M2ConventionsUtils() {
1097 }
1098 }
1099
1100 class CategoryNameVersion extends NameVersion {
1101 private String category;
1102
1103 CategoryNameVersion() {
1104 }
1105
1106 CategoryNameVersion(String category, String name, String version) {
1107 super(name, version);
1108 this.category = category;
1109 }
1110
1111 CategoryNameVersion(String category, NameVersion nameVersion) {
1112 super(nameVersion);
1113 this.category = category;
1114 }
1115
1116 String getCategory() {
1117 return category;
1118 }
1119
1120 void setCategory(String category) {
1121 this.category = category;
1122 }
1123
1124 @Override
1125 public String toString() {
1126 return category + ":" + super.toString();
1127 }
1128
1129 }
1130
1131 class NameVersion implements Comparable<NameVersion> {
1132 private String name;
1133 private String version;
1134
1135 NameVersion() {
1136 }
1137
1138 /** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
1139 NameVersion(String nameVersion) {
1140 int index = nameVersion.indexOf(";version=");
1141 if (index < 0) {
1142 setName(nameVersion);
1143 setVersion(null);
1144 } else {
1145 setName(nameVersion.substring(0, index));
1146 setVersion(nameVersion.substring(index + ";version=".length()));
1147 }
1148 }
1149
1150 NameVersion(String name, String version) {
1151 this.name = name;
1152 this.version = version;
1153 }
1154
1155 NameVersion(NameVersion nameVersion) {
1156 this.name = nameVersion.getName();
1157 this.version = nameVersion.getVersion();
1158 }
1159
1160 String getName() {
1161 return name;
1162 }
1163
1164 void setName(String name) {
1165 this.name = name;
1166 }
1167
1168 String getVersion() {
1169 return version;
1170 }
1171
1172 void setVersion(String version) {
1173 this.version = version;
1174 }
1175
1176 String getBranch() {
1177 String[] parts = getVersion().split("\\.");
1178 if (parts.length < 2)
1179 throw new IllegalStateException("Version " + getVersion() + " cannot be interpreted as branch.");
1180 return parts[0] + "." + parts[1];
1181 }
1182
1183 @Override
1184 public boolean equals(Object obj) {
1185 if (obj instanceof NameVersion) {
1186 NameVersion nameVersion = (NameVersion) obj;
1187 return name.equals(nameVersion.getName()) && version.equals(nameVersion.getVersion());
1188 } else
1189 return false;
1190 }
1191
1192 @Override
1193 public int hashCode() {
1194 return name.hashCode();
1195 }
1196
1197 @Override
1198 public String toString() {
1199 return name + ":" + version;
1200 }
1201
1202 public int compareTo(NameVersion o) {
1203 if (o.getName().equals(name))
1204 return version.compareTo(o.getVersion());
1205 else
1206 return name.compareTo(o.getName());
1207 }
1208 }