]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/factory/A2Factory.java
Rename factory projects + deb packages for TP
[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 if (doNotModify) {
215 fileEntries: for (Object key : fileProps.keySet()) {
216 if (ManifestConstants.SLC_ORIGIN_M2.toString().equals(key))
217 continue fileEntries;
218 String value = fileProps.getProperty(key.toString());
219 additionalEntries.put(key.toString(), value);
220 }
221 } else {
222 if (artifact != null) {
223 if (!fileProps.containsKey(BUNDLE_SYMBOLICNAME.toString())) {
224 fileProps.put(BUNDLE_SYMBOLICNAME.toString(), artifact.getName());
225 }
226 if (!fileProps.containsKey(BUNDLE_VERSION.toString())) {
227 fileProps.put(BUNDLE_VERSION.toString(), artifact.getVersion());
228 }
229 }
230
231 if (!fileProps.containsKey(EXPORT_PACKAGE.toString())) {
232 fileProps.put(EXPORT_PACKAGE.toString(),
233 "*;version=\"" + fileProps.getProperty(BUNDLE_VERSION.toString()) + "\"");
234 }
235 // if (!fileProps.contains(IMPORT_PACKAGE.toString())) {
236 // fileProps.put(IMPORT_PACKAGE.toString(), "*");
237 // }
238
239 try (Analyzer bndAnalyzer = new Analyzer()) {
240 bndAnalyzer.setProperties(fileProps);
241 Jar jar = new Jar(downloaded.toFile());
242 bndAnalyzer.setJar(jar);
243 Manifest manifest = bndAnalyzer.calcManifest();
244
245 keys: for (Object key : manifest.getMainAttributes().keySet()) {
246 Object value = manifest.getMainAttributes().get(key);
247
248 switch (key.toString()) {
249 case "Tool":
250 case "Bnd-LastModified":
251 case "Created-By":
252 continue keys;
253 }
254 if ("Require-Capability".equals(key.toString())
255 && value.toString().equals("osgi.ee;filter:=\"(&(osgi.ee=JavaSE)(version=1.1))\""))
256 continue keys;// hack for very old classes
257 additionalEntries.put(key.toString(), value.toString());
258 logger.log(DEBUG, () -> key + "=" + value);
259
260 }
261 }
262
263 // try (Builder bndBuilder = new Builder()) {
264 // Jar jar = new Jar(downloaded.toFile());
265 // bndBuilder.addClasspath(jar);
266 // Path targetBundleDir = targetCategoryBase.resolve(artifact.getName() + "." + artifact.getBranch());
267 //
268 // Jar target = new Jar(targetBundleDir.toFile());
269 // bndBuilder.setJar(target);
270 // return targetBundleDir;
271 // }
272 }
273 Path targetBundleDir = processBundleJar(downloaded, targetCategoryBase, additionalEntries);
274 logger.log(Level.DEBUG, () -> "Processed " + downloaded);
275 return targetBundleDir;
276 } catch (Exception e) {
277 throw new RuntimeException("Cannot BND process " + downloaded, e);
278 }
279
280 }
281
282 protected void processM2SourceJar(Path file, Path targetBundleDir) throws IOException {
283 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
284 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
285
286 // TODO make it less dangerous?
287 if (Files.exists(targetSourceDir)) {
288 deleteDirectory(targetSourceDir);
289 } else {
290 Files.createDirectories(targetSourceDir);
291 }
292
293 // copy entries
294 JarEntry entry;
295 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
296 if (entry.isDirectory())
297 continue entries;
298 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
299 continue entries;
300 Path target = targetSourceDir.resolve(entry.getName());
301 Files.createDirectories(target.getParent());
302 Files.copy(jarIn, target);
303 logger.log(Level.TRACE, () -> "Copied source " + target);
304 }
305 }
306
307 }
308
309 public void processEclipseArchive(Path duDir) {
310 try {
311 String category = duDir.getParent().getFileName().toString();
312 Path targetCategoryBase = factoryBase.resolve(category);
313 Files.createDirectories(targetCategoryBase);
314 Files.createDirectories(originBase);
315
316 Path commonBnd = duDir.resolve(COMMON_BND);
317 Properties commonProps = new Properties();
318 try (InputStream in = Files.newInputStream(commonBnd)) {
319 commonProps.load(in);
320 }
321 Properties includes = new Properties();
322 try (InputStream in = Files.newInputStream(duDir.resolve("includes.properties"))) {
323 includes.load(in);
324 }
325 String url = commonProps.getProperty(ManifestConstants.SLC_ORIGIN_URI.toString());
326 Path downloaded = tryDownload(url, originBase);
327
328 FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null);
329
330 List<PathMatcher> pathMatchers = new ArrayList<>();
331 for (Object pattern : includes.keySet()) {
332 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
333 pathMatchers.add(pathMatcher);
334 }
335
336 Files.walkFileTree(zipFs.getRootDirectories().iterator().next(), new SimpleFileVisitor<Path>() {
337
338 @Override
339 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
340 pathMatchers: for (PathMatcher pathMatcher : pathMatchers) {
341 if (pathMatcher.matches(file)) {
342 // Path target = targetBase.resolve(file.getFileName().toString());
343 // if (!Files.exists(target)) {
344 // Files.copy(file, target);
345 // logger.log(Level.DEBUG, () -> "Copied " + target + " from " + downloaded);
346 // } else {
347 // logger.log(Level.DEBUG, () -> target + " already exists.");
348 //
349 // }
350 if (file.getFileName().toString().contains(".source_")) {
351 processEclipseSourceJar(file, targetCategoryBase);
352 logger.log(Level.DEBUG, () -> "Processed source " + file);
353
354 } else {
355 processBundleJar(file, targetCategoryBase, new HashMap<>());
356 logger.log(Level.DEBUG, () -> "Processed " + file);
357 }
358 continue pathMatchers;
359 }
360 }
361 return super.visitFile(file, attrs);
362 }
363 });
364
365 DirectoryStream<Path> dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
366 for (Path dir : dirs) {
367 createJar(dir);
368 }
369 } catch (IOException e) {
370 throw new RuntimeException("Cannot process " + duDir, e);
371 }
372
373 }
374
375 protected Path processBundleJar(Path file, Path targetBase, Map<String, String> entries) throws IOException {
376 NameVersion nameVersion;
377 Path targetBundleDir;
378 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
379 Manifest manifest = new Manifest(jarIn.getManifest());
380
381 // remove problematic entries in MANIFEST
382 manifest.getEntries().clear();
383 // Set<String> entriesToDelete = new HashSet<>();
384 // for (String key : manifest.getEntries().keySet()) {
385 //// logger.log(DEBUG, "## " + key);
386 // Attributes attrs = manifest.getAttributes(key);
387 // for (Object attrName : attrs.keySet()) {
388 //// logger.log(DEBUG, attrName + "=" + attrs.get(attrName));
389 // if ("Specification-Version".equals(attrName.toString())
390 // || "Implementation-Version".equals(attrName.toString())) {
391 // entriesToDelete.add(key);
392 //
393 // }
394 // }
395 // }
396 // for (String key : entriesToDelete) {
397 // manifest.getEntries().remove(key);
398 // }
399
400 String symbolicNameFromEntries = entries.get(BUNDLE_SYMBOLICNAME.toString());
401 String versionFromEntries = entries.get(BUNDLE_VERSION.toString());
402
403 if (symbolicNameFromEntries != null && versionFromEntries != null) {
404 nameVersion = new DefaultNameVersion(symbolicNameFromEntries, versionFromEntries);
405 } else {
406 nameVersion = nameVersionFromManifest(manifest);
407 if (versionFromEntries != null && !nameVersion.getVersion().equals(versionFromEntries)) {
408 logger.log(Level.WARNING, "Original version is " + nameVersion.getVersion()
409 + " while new version is " + versionFromEntries);
410 }
411 }
412 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
413
414 // TODO make it less dangerous?
415 if (Files.exists(targetBundleDir)) {
416 deleteDirectory(targetBundleDir);
417 }
418
419 // copy entries
420 JarEntry entry;
421 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
422 if (entry.isDirectory())
423 continue entries;
424 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
425 continue entries;
426 Path target = targetBundleDir.resolve(entry.getName());
427 Files.createDirectories(target.getParent());
428 Files.copy(jarIn, target);
429 logger.log(Level.TRACE, () -> "Copied " + target);
430 }
431
432 // copy MANIFEST
433 Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
434 Files.createDirectories(manifestPath.getParent());
435 for (String key : entries.keySet()) {
436 String value = entries.get(key);
437 Object previousValue = manifest.getMainAttributes().putValue(key, value);
438 if (previousValue != null && !previousValue.equals(value)) {
439 logger.log(Level.WARNING,
440 file.getFileName() + ": " + key + " was " + previousValue + ", overridden with " + value);
441 }
442 }
443 try (OutputStream out = Files.newOutputStream(manifestPath)) {
444 manifest.write(out);
445 }
446 }
447 return targetBundleDir;
448 }
449
450 protected void processEclipseSourceJar(Path file, Path targetBase) throws IOException {
451 // NameVersion nameVersion;
452 Path targetBundleDir;
453 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
454 Manifest manifest = jarIn.getManifest();
455 // nameVersion = nameVersionFromManifest(manifest);
456
457 String[] relatedBundle = manifest.getMainAttributes().getValue("Eclipse-SourceBundle").split(";");
458 String version = relatedBundle[1].substring("version=\"".length());
459 version = version.substring(0, version.length() - 1);
460 NameVersion nameVersion = new DefaultNameVersion(relatedBundle[0], version);
461 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
462
463 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
464
465 // TODO make it less dangerous?
466 if (Files.exists(targetSourceDir)) {
467 deleteDirectory(targetSourceDir);
468 } else {
469 Files.createDirectories(targetSourceDir);
470 }
471
472 // copy entries
473 JarEntry entry;
474 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
475 if (entry.isDirectory())
476 continue entries;
477 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
478 continue entries;
479 Path target = targetSourceDir.resolve(entry.getName());
480 Files.createDirectories(target.getParent());
481 Files.copy(jarIn, target);
482 logger.log(Level.TRACE, () -> "Copied source " + target);
483 }
484
485 // copy MANIFEST
486 // Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
487 // Files.createDirectories(manifestPath.getParent());
488 // try (OutputStream out = Files.newOutputStream(manifestPath)) {
489 // manifest.write(out);
490 // }
491 }
492
493 }
494
495 static void deleteDirectory(Path path) throws IOException {
496 if (!Files.exists(path))
497 return;
498 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
499 @Override
500 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
501 if (e != null)
502 throw e;
503 Files.delete(directory);
504 return FileVisitResult.CONTINUE;
505 }
506
507 @Override
508 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
509 Files.delete(file);
510 return FileVisitResult.CONTINUE;
511 }
512 });
513 }
514
515 protected NameVersion nameVersionFromManifest(Manifest manifest) {
516 Attributes attrs = manifest.getMainAttributes();
517 // symbolic name
518 String symbolicName = attrs.getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
519 if (symbolicName == null)
520 return null;
521 // make sure there is no directive
522 symbolicName = symbolicName.split(";")[0];
523
524 String version = attrs.getValue(ManifestConstants.BUNDLE_VERSION.toString());
525 return new DefaultNameVersion(symbolicName, version);
526 }
527
528 protected Path tryDownload(String uri, Path dir) throws IOException {
529 // find mirror
530 List<String> urlBases = null;
531 String uriPrefix = null;
532 uriPrefixes: for (String uriPref : mirrors.keySet()) {
533 if (uri.startsWith(uriPref)) {
534 if (mirrors.get(uriPref).size() > 0) {
535 urlBases = mirrors.get(uriPref);
536 uriPrefix = uriPref;
537 break uriPrefixes;
538 }
539 }
540 }
541 if (urlBases == null)
542 try {
543 return download(new URL(uri), dir, null);
544 } catch (FileNotFoundException e) {
545 throw new FileNotFoundException("Cannot find " + uri);
546 }
547
548 // try to download
549 for (String urlBase : urlBases) {
550 String relativePath = uri.substring(uriPrefix.length());
551 URL url = new URL(urlBase + relativePath);
552 try {
553 return download(url, dir, null);
554 } catch (FileNotFoundException e) {
555 logger.log(Level.WARNING, "Cannot download " + url + ", trying another mirror");
556 }
557 }
558 throw new FileNotFoundException("Cannot find " + uri);
559 }
560
561 // protected String simplifyName(URL u) {
562 // String name = u.getPath().substring(u.getPath().lastIndexOf('/') + 1);
563 //
564 // }
565
566 protected Path download(URL url, Path dir, String name) throws IOException {
567
568 Path dest;
569 if (name == null) {
570 name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
571 }
572
573 dest = dir.resolve(name);
574 if (Files.exists(dest)) {
575 logger.log(Level.TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again");
576 return dest;
577 }
578
579 try (InputStream in = url.openStream()) {
580 Files.copy(in, dest);
581 logger.log(Level.DEBUG, () -> "Downloaded " + dest + " from " + url);
582 }
583 return dest;
584 }
585
586 protected Path createJar(Path bundleDir) throws IOException {
587 Path jarPath = bundleDir.getParent().resolve(bundleDir.getFileName() + ".jar");
588 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
589 Manifest manifest;
590 try (InputStream in = Files.newInputStream(manifestPath)) {
591 manifest = new Manifest(in);
592 }
593 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarPath), manifest)) {
594 Files.walkFileTree(bundleDir, new SimpleFileVisitor<Path>() {
595
596 @Override
597 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
598 if (file.getFileName().toString().equals("MANIFEST.MF"))
599 return super.visitFile(file, attrs);
600 JarEntry entry = new JarEntry(bundleDir.relativize(file).toString());
601 jarOut.putNextEntry(entry);
602 Files.copy(file, jarOut);
603 return super.visitFile(file, attrs);
604 }
605
606 });
607 }
608 deleteDirectory(bundleDir);
609 return jarPath;
610 }
611
612 public static void main(String[] args) {
613 Path originBase = Paths.get("../output/origin").toAbsolutePath().normalize();
614 Path factoryBase = Paths.get("../output/a2").toAbsolutePath().normalize();
615 A2Factory factory = new A2Factory(originBase, factoryBase);
616
617 Path descriptorsBase = Paths.get("../tp").toAbsolutePath().normalize();
618
619 // factory.processSingleM2ArtifactDistributionUnit(descriptorsBase.resolve("org.argeo.tp.apache").resolve("org.apache.xml.resolver.bnd"));
620 // factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp/slf4j"));
621 // System.exit(0);
622
623 // Eclipse
624 factory.processEclipseArchive(
625 descriptorsBase.resolve("org.argeo.tp.eclipse.equinox").resolve("eclipse-equinox"));
626 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rap").resolve("eclipse-rap"));
627 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rcp").resolve("eclipse-rcp"));
628
629 // Maven
630 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.sdk"));
631 factory.processCategory(descriptorsBase.resolve("org.argeo.tp"));
632 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.apache"));
633 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jetty"));
634 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jcr"));
635 }
636 }