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