]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/factory/A2Factory.java
Update non-Eclipse third-parties.
[gpl/argeo-slc.git] / org.argeo.slc.factory / src / org / argeo / slc / factory / A2Factory.java
1 package org.argeo.slc.factory;
2
3 import static java.lang.System.Logger.Level.DEBUG;
4 import static org.argeo.slc.ManifestConstants.BUNDLE_LICENSE;
5 import static org.argeo.slc.ManifestConstants.BUNDLE_SYMBOLICNAME;
6 import static org.argeo.slc.ManifestConstants.BUNDLE_VERSION;
7 import static org.argeo.slc.ManifestConstants.EXPORT_PACKAGE;
8 import static org.argeo.slc.ManifestConstants.SLC_ORIGIN_M2;
9
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.io.Writer;
15 import java.lang.System.Logger;
16 import java.lang.System.Logger.Level;
17 import java.net.URL;
18 import java.nio.file.DirectoryStream;
19 import java.nio.file.FileSystem;
20 import java.nio.file.FileSystems;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.PathMatcher;
25 import java.nio.file.Paths;
26 import java.nio.file.SimpleFileVisitor;
27 import java.nio.file.attribute.BasicFileAttributes;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33 import java.util.TreeMap;
34 import java.util.jar.Attributes;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarInputStream;
37 import java.util.jar.JarOutputStream;
38 import java.util.jar.Manifest;
39
40 import org.argeo.slc.DefaultNameVersion;
41 import org.argeo.slc.ManifestConstants;
42 import org.argeo.slc.NameVersion;
43 import org.argeo.slc.factory.m2.DefaultArtifact;
44 import org.argeo.slc.factory.m2.MavenConventionsUtils;
45
46 import aQute.bnd.osgi.Analyzer;
47 import aQute.bnd.osgi.Jar;
48
49 /** The central class for A2 packaging. */
50 public class A2Factory {
51 private final static Logger logger = System.getLogger(A2Factory.class.getName());
52
53 private final static String COMMON_BND = "common.bnd";
54
55 private Path originBase;
56 private Path factoryBase;
57
58 /** key is URI prefix, value list of base URLs */
59 private Map<String, List<String>> mirrors = new HashMap<String, List<String>>();
60
61 public A2Factory(Path originBase, Path factoryBase) {
62 super();
63 this.originBase = originBase;
64 this.factoryBase = factoryBase;
65
66 // TODO make it configurable
67 List<String> eclipseMirrors = new ArrayList<>();
68 eclipseMirrors.add("https://archive.eclipse.org/");
69
70 mirrors.put("http://www.eclipse.org/downloads", eclipseMirrors);
71 }
72
73 public void processCategory(Path targetCategoryBase) {
74 try {
75 DirectoryStream<Path> bnds = Files.newDirectoryStream(targetCategoryBase,
76 (p) -> p.getFileName().toString().endsWith(".bnd")
77 && !p.getFileName().toString().equals(COMMON_BND));
78 for (Path p : bnds) {
79 processSingleM2ArtifactDistributionUnit(p);
80 }
81
82 DirectoryStream<Path> dus = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
83 for (Path duDir : dus) {
84 processM2BasedDistributionUnit(duDir);
85 }
86 } catch (IOException e) {
87 throw new RuntimeException("Cannot process category " + targetCategoryBase, e);
88 }
89 }
90
91 public void processSingleM2ArtifactDistributionUnit(Path bndFile) {
92 try {
93 String category = bndFile.getParent().getFileName().toString();
94 Path targetCategoryBase = factoryBase.resolve(category);
95 Properties fileProps = new Properties();
96 try (InputStream in = Files.newInputStream(bndFile)) {
97 fileProps.load(in);
98 }
99
100 String m2Coordinates = fileProps.getProperty(SLC_ORIGIN_M2.toString());
101 if (m2Coordinates == null)
102 throw new IllegalArgumentException("No M2 coordinates available for " + bndFile);
103 DefaultArtifact artifact = new DefaultArtifact(m2Coordinates);
104 URL url = MavenConventionsUtils.mavenCentralUrl(artifact);
105 Path downloaded = download(url, originBase, artifact.toM2Coordinates() + ".jar");
106
107 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, fileProps, artifact);
108
109 downloadAndProcessM2Sources(artifact, targetBundleDir);
110
111 createJar(targetBundleDir);
112 } catch (Exception e) {
113 throw new RuntimeException("Cannot process " + bndFile, e);
114 }
115 }
116
117 public void processM2BasedDistributionUnit(Path duDir) {
118 try {
119 String category = duDir.getParent().getFileName().toString();
120 Path targetCategoryBase = factoryBase.resolve(category);
121 Path commonBnd = duDir.resolve(COMMON_BND);
122 Properties commonProps = new Properties();
123 try (InputStream in = Files.newInputStream(commonBnd)) {
124 commonProps.load(in);
125 }
126
127 String m2Version = commonProps.getProperty(SLC_ORIGIN_M2.toString());
128 if (!m2Version.startsWith(":")) {
129 throw new IllegalStateException("Only the M2 version can be specified: " + m2Version);
130 }
131 m2Version = m2Version.substring(1);
132
133 // String license = commonProps.getProperty(BUNDLE_LICENSE.toString());
134
135 DirectoryStream<Path> ds = Files.newDirectoryStream(duDir,
136 (p) -> p.getFileName().toString().endsWith(".bnd")
137 && !p.getFileName().toString().equals(COMMON_BND));
138 for (Path p : ds) {
139 Properties fileProps = new Properties();
140 try (InputStream in = Files.newInputStream(p)) {
141 fileProps.load(in);
142 }
143 String m2Coordinates = fileProps.getProperty(SLC_ORIGIN_M2.toString());
144 DefaultArtifact artifact = new DefaultArtifact(m2Coordinates);
145
146 // temporary rewrite, for migration
147 String localLicense = fileProps.getProperty(BUNDLE_LICENSE.toString());
148 if (localLicense != null || artifact.getVersion() != null) {
149 fileProps.remove(BUNDLE_LICENSE.toString());
150 fileProps.put(SLC_ORIGIN_M2.toString(), artifact.getGroupId() + ":" + artifact.getArtifactId());
151 try (Writer writer = Files.newBufferedWriter(p)) {
152 for (Object key : fileProps.keySet()) {
153 String value = fileProps.getProperty(key.toString());
154 writer.write(key + ": " + value + '\n');
155 }
156 logger.log(DEBUG, () -> "Migrated " + p);
157 }
158 }
159
160 artifact.setVersion(m2Version);
161 URL url = MavenConventionsUtils.mavenCentralUrl(artifact);
162 Path downloaded = download(url, originBase, artifact.toM2Coordinates() + ".jar");
163
164 // prepare manifest entries
165 Properties mergeProps = new Properties();
166 mergeProps.putAll(commonProps);
167
168 // Map<String, String> entries = new HashMap<>();
169 // for (Object key : commonProps.keySet()) {
170 // entries.put(key.toString(), commonProps.getProperty(key.toString()));
171 // }
172 fileEntries: for (Object key : fileProps.keySet()) {
173 if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
174 continue fileEntries;
175 String value = fileProps.getProperty(key.toString());
176 Object previousValue = mergeProps.put(key.toString(), value);
177 if (previousValue != null) {
178 logger.log(Level.WARNING,
179 downloaded + ": " + key + " was " + previousValue + ", overridden with " + value);
180 }
181 }
182 mergeProps.put(ManifestConstants.SLC_ORIGIN_M2.toString(), artifact.toM2Coordinates());
183 Path targetBundleDir = processBndJar(downloaded, targetCategoryBase, mergeProps, artifact);
184 // logger.log(Level.DEBUG, () -> "Processed " + downloaded);
185
186 // sources
187 downloadAndProcessM2Sources(artifact, targetBundleDir);
188
189 createJar(targetBundleDir);
190 }
191 } catch (IOException e) {
192 throw new RuntimeException("Cannot process " + duDir, e);
193 }
194
195 }
196
197 protected void downloadAndProcessM2Sources(DefaultArtifact artifact, Path targetBundleDir) throws IOException {
198 DefaultArtifact sourcesArtifact = new DefaultArtifact(artifact.toM2Coordinates(), "sources");
199 URL sourcesUrl = MavenConventionsUtils.mavenCentralUrl(sourcesArtifact);
200 Path sourcesDownloaded = download(sourcesUrl, originBase, artifact.toM2Coordinates() + ".sources.jar");
201 processM2SourceJar(sourcesDownloaded, targetBundleDir);
202 logger.log(Level.DEBUG, () -> "Processed source " + sourcesDownloaded);
203
204 }
205
206 protected Path processBndJar(Path downloaded, Path targetCategoryBase, Properties fileProps,
207 DefaultArtifact artifact) {
208
209 try {
210 Map<String, String> additionalEntries = new TreeMap<>();
211 boolean doNotModify = Boolean.parseBoolean(fileProps
212 .getOrDefault(ManifestConstants.SLC_ORIGIN_MANIFEST_NOT_MODIFIED.toString(), "false").toString());
213
214 // we always force the symbolic name
215 if (artifact != null) {
216 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
217 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), artifact.getName());
218 }
219 if (!fileProps.containsKey(BUNDLE_VERSION.toString())) {
220 fileProps.put(BUNDLE_VERSION.toString(), artifact.getVersion());
221 }
222 }
223
224 if (doNotModify) {
225 fileEntries: for (Object key : fileProps.keySet()) {
226 if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
227 continue fileEntries;
228 String value = fileProps.getProperty(key.toString());
229 additionalEntries.put(key.toString(), value);
230 }
231 } else {
232
233 if (!fileProps.containsKey(EXPORT_PACKAGE.toString())) {
234 fileProps.put(EXPORT_PACKAGE.toString(),
235 "*;version=\"" + fileProps.getProperty(BUNDLE_VERSION.toString()) + "\"");
236 }
237 // if (!fileProps.contains(IMPORT_PACKAGE.toString())) {
238 // fileProps.put(IMPORT_PACKAGE.toString(), "*");
239 // }
240
241 try (Analyzer bndAnalyzer = new Analyzer()) {
242 bndAnalyzer.setProperties(fileProps);
243 Jar jar = new Jar(downloaded.toFile());
244 bndAnalyzer.setJar(jar);
245 Manifest manifest = bndAnalyzer.calcManifest();
246
247 keys: for (Object key : manifest.getMainAttributes().keySet()) {
248 Object value = manifest.getMainAttributes().get(key);
249
250 switch (key.toString()) {
251 case "Tool":
252 case "Bnd-LastModified":
253 case "Created-By":
254 continue keys;
255 }
256 if ("Require-Capability".equals(key.toString())
257 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
258 continue keys;// hack for very old classes
259 additionalEntries.put(key.toString(), value.toString());
260 logger.log(DEBUG, () -> key + "=" + value);
261
262 }
263 }
264
265 // try (Builder bndBuilder = new Builder()) {
266 // Jar jar = new Jar(downloaded.toFile());
267 // bndBuilder.addClasspath(jar);
268 // Path targetBundleDir = targetCategoryBase.resolve(artifact.getName() + "." + artifact.getBranch());
269 //
270 // Jar target = new Jar(targetBundleDir.toFile());
271 // bndBuilder.setJar(target);
272 // return targetBundleDir;
273 // }
274 }
275 Path targetBundleDir = processBundleJar(downloaded, targetCategoryBase, additionalEntries);
276 logger.log(Level.DEBUG, () -> "Processed " + downloaded);
277 return targetBundleDir;
278 } catch (Exception e) {
279 throw new RuntimeException("Cannot BND process " + downloaded, e);
280 }
281
282 }
283
284 protected void processM2SourceJar(Path file, Path targetBundleDir) throws IOException {
285 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
286 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
287
288 // TODO make it less dangerous?
289 if (Files.exists(targetSourceDir)) {
290 deleteDirectory(targetSourceDir);
291 } else {
292 Files.createDirectories(targetSourceDir);
293 }
294
295 // copy entries
296 JarEntry entry;
297 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
298 if (entry.isDirectory())
299 continue entries;
300 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
301 continue entries;
302 Path target = targetSourceDir.resolve(entry.getName());
303 Files.createDirectories(target.getParent());
304 Files.copy(jarIn, target);
305 logger.log(Level.TRACE, () -> "Copied source " + target);
306 }
307 }
308
309 }
310
311 public void processEclipseArchive(Path duDir) {
312 try {
313 String category = duDir.getParent().getFileName().toString();
314 Path targetCategoryBase = factoryBase.resolve(category);
315 Files.createDirectories(targetCategoryBase);
316 Files.createDirectories(originBase);
317
318 Path commonBnd = duDir.resolve(COMMON_BND);
319 Properties commonProps = new Properties();
320 try (InputStream in = Files.newInputStream(commonBnd)) {
321 commonProps.load(in);
322 }
323 Properties includes = new Properties();
324 try (InputStream in = Files.newInputStream(duDir.resolve("includes.properties"))) {
325 includes.load(in);
326 }
327 String url = commonProps.getProperty(ManifestConstants.SLC_ORIGIN_URI.toString());
328 Path downloaded = tryDownload(url, originBase);
329
330 FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null);
331
332 List<PathMatcher> pathMatchers = new ArrayList<>();
333 for (Object pattern : includes.keySet()) {
334 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
335 pathMatchers.add(pathMatcher);
336 }
337
338 Files.walkFileTree(zipFs.getRootDirectories().iterator().next(), new SimpleFileVisitor<Path>() {
339
340 @Override
341 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
342 pathMatchers: for (PathMatcher pathMatcher : pathMatchers) {
343 if (pathMatcher.matches(file)) {
344 // Path target = targetBase.resolve(file.getFileName().toString());
345 // if (!Files.exists(target)) {
346 // Files.copy(file, target);
347 // logger.log(Level.DEBUG, () -> "Copied " + target + " from " + downloaded);
348 // } else {
349 // logger.log(Level.DEBUG, () -> target + " already exists.");
350 //
351 // }
352 if (file.getFileName().toString().contains(".source_")) {
353 processEclipseSourceJar(file, targetCategoryBase);
354 logger.log(Level.DEBUG, () -> "Processed source " + file);
355
356 } else {
357 processBundleJar(file, targetCategoryBase, new HashMap<>());
358 logger.log(Level.DEBUG, () -> "Processed " + file);
359 }
360 continue pathMatchers;
361 }
362 }
363 return super.visitFile(file, attrs);
364 }
365 });
366
367 DirectoryStream<Path> dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
368 for (Path dir : dirs) {
369 createJar(dir);
370 }
371 } catch (IOException e) {
372 throw new RuntimeException("Cannot process " + duDir, e);
373 }
374
375 }
376
377 protected Path processBundleJar(Path file, Path targetBase, Map<String, String> entries) throws IOException {
378 NameVersion nameVersion;
379 Path targetBundleDir;
380 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
381 Manifest manifest = new Manifest(jarIn.getManifest());
382
383 // remove problematic entries in MANIFEST
384 manifest.getEntries().clear();
385 // Set<String> entriesToDelete = new HashSet<>();
386 // for (String key : manifest.getEntries().keySet()) {
387 //// logger.log(DEBUG, "## " + key);
388 // Attributes attrs = manifest.getAttributes(key);
389 // for (Object attrName : attrs.keySet()) {
390 //// logger.log(DEBUG, attrName + "=" + attrs.get(attrName));
391 // if ("Specification-Version".equals(attrName.toString())
392 // || "Implementation-Version".equals(attrName.toString())) {
393 // entriesToDelete.add(key);
394 //
395 // }
396 // }
397 // }
398 // for (String key : entriesToDelete) {
399 // manifest.getEntries().remove(key);
400 // }
401
402 String symbolicNameFromEntries = entries.get(BUNDLE_SYMBOLICNAME.toString());
403 String versionFromEntries = entries.get(BUNDLE_VERSION.toString());
404
405 if (symbolicNameFromEntries != null && versionFromEntries != null) {
406 nameVersion = new DefaultNameVersion(symbolicNameFromEntries, versionFromEntries);
407 } else {
408 nameVersion = nameVersionFromManifest(manifest);
409 if (versionFromEntries != null && !nameVersion.getVersion().equals(versionFromEntries)) {
410 logger.log(Level.WARNING, "Original version is " + nameVersion.getVersion()
411 + " while new version is " + versionFromEntries);
412 }
413 }
414 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
415
416 // TODO make it less dangerous?
417 if (Files.exists(targetBundleDir)) {
418 deleteDirectory(targetBundleDir);
419 }
420
421 // copy entries
422 JarEntry entry;
423 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
424 if (entry.isDirectory())
425 continue entries;
426 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
427 continue entries;
428 Path target = targetBundleDir.resolve(entry.getName());
429 Files.createDirectories(target.getParent());
430 Files.copy(jarIn, target);
431 logger.log(Level.TRACE, () -> "Copied " + target);
432 }
433
434 // copy MANIFEST
435 Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
436 Files.createDirectories(manifestPath.getParent());
437 for (String key : entries.keySet()) {
438 String value = entries.get(key);
439 Object previousValue = manifest.getMainAttributes().putValue(key, value);
440 if (previousValue != null && !previousValue.equals(value)) {
441 logger.log(Level.WARNING,
442 file.getFileName() + ": " + key + " was " + previousValue + ", overridden with " + value);
443 }
444 }
445 try (OutputStream out = Files.newOutputStream(manifestPath)) {
446 manifest.write(out);
447 }
448 }
449 return targetBundleDir;
450 }
451
452 protected void processEclipseSourceJar(Path file, Path targetBase) throws IOException {
453 // NameVersion nameVersion;
454 Path targetBundleDir;
455 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
456 Manifest manifest = jarIn.getManifest();
457 // nameVersion = nameVersionFromManifest(manifest);
458
459 String[] relatedBundle = manifest.getMainAttributes().getValue("Eclipse-SourceBundle").split(";");
460 String version = relatedBundle[1].substring("version=\"".length());
461 version = version.substring(0, version.length() - 1);
462 NameVersion nameVersion = new DefaultNameVersion(relatedBundle[0], version);
463 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
464
465 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
466
467 // TODO make it less dangerous?
468 if (Files.exists(targetSourceDir)) {
469 deleteDirectory(targetSourceDir);
470 } else {
471 Files.createDirectories(targetSourceDir);
472 }
473
474 // copy entries
475 JarEntry entry;
476 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
477 if (entry.isDirectory())
478 continue entries;
479 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
480 continue entries;
481 Path target = targetSourceDir.resolve(entry.getName());
482 Files.createDirectories(target.getParent());
483 Files.copy(jarIn, target);
484 logger.log(Level.TRACE, () -> "Copied source " + target);
485 }
486
487 // copy MANIFEST
488 // Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
489 // Files.createDirectories(manifestPath.getParent());
490 // try (OutputStream out = Files.newOutputStream(manifestPath)) {
491 // manifest.write(out);
492 // }
493 }
494
495 }
496
497 static void deleteDirectory(Path path) throws IOException {
498 if (!Files.exists(path))
499 return;
500 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
501 @Override
502 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
503 if (e != null)
504 throw e;
505 Files.delete(directory);
506 return FileVisitResult.CONTINUE;
507 }
508
509 @Override
510 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
511 Files.delete(file);
512 return FileVisitResult.CONTINUE;
513 }
514 });
515 }
516
517 protected NameVersion nameVersionFromManifest(Manifest manifest) {
518 Attributes attrs = manifest.getMainAttributes();
519 // symbolic name
520 String symbolicName = attrs.getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
521 if (symbolicName == null)
522 return null;
523 // make sure there is no directive
524 symbolicName = symbolicName.split(";")[0];
525
526 String version = attrs.getValue(ManifestConstants.BUNDLE_VERSION.toString());
527 return new DefaultNameVersion(symbolicName, version);
528 }
529
530 protected Path tryDownload(String uri, Path dir) throws IOException {
531 // find mirror
532 List<String> urlBases = null;
533 String uriPrefix = null;
534 uriPrefixes: for (String uriPref : mirrors.keySet()) {
535 if (uri.startsWith(uriPref)) {
536 if (mirrors.get(uriPref).size() > 0) {
537 urlBases = mirrors.get(uriPref);
538 uriPrefix = uriPref;
539 break uriPrefixes;
540 }
541 }
542 }
543 if (urlBases == null)
544 try {
545 return download(new URL(uri), dir, null);
546 } catch (FileNotFoundException e) {
547 throw new FileNotFoundException("Cannot find " + uri);
548 }
549
550 // try to download
551 for (String urlBase : urlBases) {
552 String relativePath = uri.substring(uriPrefix.length());
553 URL url = new URL(urlBase + relativePath);
554 try {
555 return download(url, dir, null);
556 } catch (FileNotFoundException e) {
557 logger.log(Level.WARNING, "Cannot download " + url + ", trying another mirror");
558 }
559 }
560 throw new FileNotFoundException("Cannot find " + uri);
561 }
562
563 // protected String simplifyName(URL u) {
564 // String name = u.getPath().substring(u.getPath().lastIndexOf('/') + 1);
565 //
566 // }
567
568 protected Path download(URL url, Path dir, String name) throws IOException {
569
570 Path dest;
571 if (name == null) {
572 name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
573 }
574
575 dest = dir.resolve(name);
576 if (Files.exists(dest)) {
577 logger.log(Level.TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again");
578 return dest;
579 }
580
581 try (InputStream in = url.openStream()) {
582 Files.copy(in, dest);
583 logger.log(Level.DEBUG, () -> "Downloaded " + dest + " from " + url);
584 }
585 return dest;
586 }
587
588 protected Path createJar(Path bundleDir) throws IOException {
589 Path jarPath = bundleDir.getParent().resolve(bundleDir.getFileName() + ".jar");
590 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
591 Manifest manifest;
592 try (InputStream in = Files.newInputStream(manifestPath)) {
593 manifest = new Manifest(in);
594 }
595 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarPath), manifest)) {
596 Files.walkFileTree(bundleDir, new SimpleFileVisitor<Path>() {
597
598 @Override
599 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
600 if (file.getFileName().toString().equals("MANIFEST.MF"))
601 return super.visitFile(file, attrs);
602 JarEntry entry = new JarEntry(bundleDir.relativize(file).toString());
603 jarOut.putNextEntry(entry);
604 Files.copy(file, jarOut);
605 return super.visitFile(file, attrs);
606 }
607
608 });
609 }
610 deleteDirectory(bundleDir);
611 return jarPath;
612 }
613
614 public static void main(String[] args) {
615 Path originBase = Paths.get("../output/origin").toAbsolutePath().normalize();
616 Path factoryBase = Paths.get("../output/a2").toAbsolutePath().normalize();
617 A2Factory factory = new A2Factory(originBase, factoryBase);
618
619 Path descriptorsBase = Paths.get("../tp").toAbsolutePath().normalize();
620
621 // factory.processSingleM2ArtifactDistributionUnit(descriptorsBase.resolve("org.argeo.tp.apache").resolve("org.apache.xml.resolver.bnd"));
622 factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp.apache/apache-sshd"));
623 // factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp.jcr/oak"));
624 System.exit(0);
625
626 // Eclipse
627 factory.processEclipseArchive(
628 descriptorsBase.resolve("org.argeo.tp.eclipse.equinox").resolve("eclipse-equinox"));
629 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rap").resolve("eclipse-rap"));
630 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rcp").resolve("eclipse-rcp"));
631
632 // Maven
633 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.sdk"));
634 factory.processCategory(descriptorsBase.resolve("org.argeo.tp"));
635 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.apache"));
636 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jetty"));
637 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jcr"));
638 }
639 }