]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.factory/src/org/argeo/slc/factory/A2Factory.java
4691167afdf5f8aa115516e9e62b51c503f0445b
[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 Files.createDirectories(originBase);
322
323 Path commonBnd = duDir.resolve(COMMON_BND);
324 Properties commonProps = new Properties();
325 try (InputStream in = Files.newInputStream(commonBnd)) {
326 commonProps.load(in);
327 }
328 Properties includes = new Properties();
329 try (InputStream in = Files.newInputStream(duDir.resolve("includes.properties"))) {
330 includes.load(in);
331 }
332 String url = commonProps.getProperty(ManifestConstants.SLC_ORIGIN_URI.toString());
333 Path downloaded = tryDownload(url, originBase);
334
335 FileSystem zipFs = FileSystems.newFileSystem(downloaded, (ClassLoader) null);
336
337 List<PathMatcher> pathMatchers = new ArrayList<>();
338 for (Object pattern : includes.keySet()) {
339 PathMatcher pathMatcher = zipFs.getPathMatcher("glob:/" + pattern);
340 pathMatchers.add(pathMatcher);
341 }
342
343 Files.walkFileTree(zipFs.getRootDirectories().iterator().next(), new SimpleFileVisitor<Path>() {
344
345 @Override
346 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
347 pathMatchers: for (PathMatcher pathMatcher : pathMatchers) {
348 if (pathMatcher.matches(file)) {
349 // Path target = targetBase.resolve(file.getFileName().toString());
350 // if (!Files.exists(target)) {
351 // Files.copy(file, target);
352 // logger.log(Level.DEBUG, () -> "Copied " + target + " from " + downloaded);
353 // } else {
354 // logger.log(Level.DEBUG, () -> target + " already exists.");
355 //
356 // }
357 if (file.getFileName().toString().contains(".source_")) {
358 processEclipseSourceJar(file, targetCategoryBase);
359 logger.log(Level.DEBUG, () -> "Processed source " + file);
360
361 } else {
362 processBundleJar(file, targetCategoryBase, new HashMap<>());
363 logger.log(Level.DEBUG, () -> "Processed " + file);
364 }
365 continue pathMatchers;
366 }
367 }
368 return super.visitFile(file, attrs);
369 }
370 });
371
372 DirectoryStream<Path> dirs = Files.newDirectoryStream(targetCategoryBase, (p) -> Files.isDirectory(p));
373 for (Path dir : dirs) {
374 createJar(dir);
375 }
376 } catch (IOException e) {
377 throw new RuntimeException("Cannot process " + duDir, e);
378 }
379
380 }
381
382 protected Path processBundleJar(Path file, Path targetBase, Map<String, String> entries) throws IOException {
383 DefaultNameVersion nameVersion;
384 Path targetBundleDir;
385 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
386 Manifest manifest = new Manifest(jarIn.getManifest());
387
388 // remove problematic entries in MANIFEST
389 manifest.getEntries().clear();
390 // Set<String> entriesToDelete = new HashSet<>();
391 // for (String key : manifest.getEntries().keySet()) {
392 //// logger.log(DEBUG, "## " + key);
393 // Attributes attrs = manifest.getAttributes(key);
394 // for (Object attrName : attrs.keySet()) {
395 //// logger.log(DEBUG, attrName + "=" + attrs.get(attrName));
396 // if ("Specification-Version".equals(attrName.toString())
397 // || "Implementation-Version".equals(attrName.toString())) {
398 // entriesToDelete.add(key);
399 //
400 // }
401 // }
402 // }
403 // for (String key : entriesToDelete) {
404 // manifest.getEntries().remove(key);
405 // }
406
407 String symbolicNameFromEntries = entries.get(BUNDLE_SYMBOLICNAME.toString());
408 String versionFromEntries = entries.get(BUNDLE_VERSION.toString());
409
410 if (symbolicNameFromEntries != null && versionFromEntries != null) {
411 nameVersion = new DefaultNameVersion(symbolicNameFromEntries, versionFromEntries);
412 } else {
413 nameVersion = nameVersionFromManifest(manifest);
414 if (versionFromEntries != null && !nameVersion.getVersion().equals(versionFromEntries)) {
415 logger.log(Level.WARNING, "Original version is " + nameVersion.getVersion()
416 + " while new version is " + versionFromEntries);
417 }
418 if (symbolicNameFromEntries != null) {
419 // we always force our symbolic name
420 nameVersion.setName(symbolicNameFromEntries);
421 }
422 }
423 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
424
425 // TODO make it less dangerous?
426 if (Files.exists(targetBundleDir)) {
427 deleteDirectory(targetBundleDir);
428 }
429
430 // copy entries
431 JarEntry entry;
432 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
433 if (entry.isDirectory())
434 continue entries;
435 if (entry.getName().endsWith(".RSA") || entry.getName().endsWith(".SF"))
436 continue entries;
437 Path target = targetBundleDir.resolve(entry.getName());
438 Files.createDirectories(target.getParent());
439 Files.copy(jarIn, target);
440 logger.log(Level.TRACE, () -> "Copied " + target);
441 }
442
443 // copy MANIFEST
444 Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
445 Files.createDirectories(manifestPath.getParent());
446 for (String key : entries.keySet()) {
447 String value = entries.get(key);
448 Object previousValue = manifest.getMainAttributes().putValue(key, value);
449 if (previousValue != null && !previousValue.equals(value)) {
450 if (ManifestConstants.IMPORT_PACKAGE.toString().equals(key)
451 || ManifestConstants.EXPORT_PACKAGE.toString().equals(key))
452 logger.log(Level.WARNING, file.getFileName() + ": " + key + " was modified");
453
454 else
455 logger.log(Level.WARNING, file.getFileName() + ": " + key + " was " + previousValue
456 + ", overridden with " + value);
457 }
458 }
459 try (OutputStream out = Files.newOutputStream(manifestPath)) {
460 manifest.write(out);
461 }
462 }
463 return targetBundleDir;
464 }
465
466 protected void processEclipseSourceJar(Path file, Path targetBase) throws IOException {
467 // NameVersion nameVersion;
468 Path targetBundleDir;
469 try (JarInputStream jarIn = new JarInputStream(Files.newInputStream(file), false)) {
470 Manifest manifest = jarIn.getManifest();
471 // nameVersion = nameVersionFromManifest(manifest);
472
473 String[] relatedBundle = manifest.getMainAttributes().getValue("Eclipse-SourceBundle").split(";");
474 String version = relatedBundle[1].substring("version=\"".length());
475 version = version.substring(0, version.length() - 1);
476 NameVersion nameVersion = new DefaultNameVersion(relatedBundle[0], version);
477 targetBundleDir = targetBase.resolve(nameVersion.getName() + "." + nameVersion.getBranch());
478
479 Path targetSourceDir = targetBundleDir.resolve("OSGI-OPT/src");
480
481 // TODO make it less dangerous?
482 if (Files.exists(targetSourceDir)) {
483 deleteDirectory(targetSourceDir);
484 } else {
485 Files.createDirectories(targetSourceDir);
486 }
487
488 // copy entries
489 JarEntry entry;
490 entries: while ((entry = jarIn.getNextJarEntry()) != null) {
491 if (entry.isDirectory())
492 continue entries;
493 if (entry.getName().startsWith("META-INF"))// skip META-INF entries
494 continue entries;
495 Path target = targetSourceDir.resolve(entry.getName());
496 Files.createDirectories(target.getParent());
497 Files.copy(jarIn, target);
498 logger.log(Level.TRACE, () -> "Copied source " + target);
499 }
500
501 // copy MANIFEST
502 // Path manifestPath = targetBundleDir.resolve("META-INF/MANIFEST.MF");
503 // Files.createDirectories(manifestPath.getParent());
504 // try (OutputStream out = Files.newOutputStream(manifestPath)) {
505 // manifest.write(out);
506 // }
507 }
508
509 }
510
511 static void deleteDirectory(Path path) throws IOException {
512 if (!Files.exists(path))
513 return;
514 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
515 @Override
516 public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException {
517 if (e != null)
518 throw e;
519 Files.delete(directory);
520 return FileVisitResult.CONTINUE;
521 }
522
523 @Override
524 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
525 Files.delete(file);
526 return FileVisitResult.CONTINUE;
527 }
528 });
529 }
530
531 protected DefaultNameVersion nameVersionFromManifest(Manifest manifest) {
532 Attributes attrs = manifest.getMainAttributes();
533 // symbolic name
534 String symbolicName = attrs.getValue(ManifestConstants.BUNDLE_SYMBOLICNAME.toString());
535 if (symbolicName == null)
536 return null;
537 // make sure there is no directive
538 symbolicName = symbolicName.split(";")[0];
539
540 String version = attrs.getValue(ManifestConstants.BUNDLE_VERSION.toString());
541 return new DefaultNameVersion(symbolicName, version);
542 }
543
544 protected Path tryDownload(String uri, Path dir) throws IOException {
545 // find mirror
546 List<String> urlBases = null;
547 String uriPrefix = null;
548 uriPrefixes: for (String uriPref : mirrors.keySet()) {
549 if (uri.startsWith(uriPref)) {
550 if (mirrors.get(uriPref).size() > 0) {
551 urlBases = mirrors.get(uriPref);
552 uriPrefix = uriPref;
553 break uriPrefixes;
554 }
555 }
556 }
557 if (urlBases == null)
558 try {
559 return download(new URL(uri), dir, null);
560 } catch (FileNotFoundException e) {
561 throw new FileNotFoundException("Cannot find " + uri);
562 }
563
564 // try to download
565 for (String urlBase : urlBases) {
566 String relativePath = uri.substring(uriPrefix.length());
567 URL url = new URL(urlBase + relativePath);
568 try {
569 return download(url, dir, null);
570 } catch (FileNotFoundException e) {
571 logger.log(Level.WARNING, "Cannot download " + url + ", trying another mirror");
572 }
573 }
574 throw new FileNotFoundException("Cannot find " + uri);
575 }
576
577 // protected String simplifyName(URL u) {
578 // String name = u.getPath().substring(u.getPath().lastIndexOf('/') + 1);
579 //
580 // }
581
582 protected Path download(URL url, Path dir, String name) throws IOException {
583
584 Path dest;
585 if (name == null) {
586 name = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
587 }
588
589 dest = dir.resolve(name);
590 if (Files.exists(dest)) {
591 logger.log(Level.TRACE, () -> "File " + dest + " already exists for " + url + ", not downloading again");
592 return dest;
593 }
594
595 try (InputStream in = url.openStream()) {
596 Files.copy(in, dest);
597 logger.log(Level.DEBUG, () -> "Downloaded " + dest + " from " + url);
598 }
599 return dest;
600 }
601
602 protected Path createJar(Path bundleDir) throws IOException {
603 Path jarPath = bundleDir.getParent().resolve(bundleDir.getFileName() + ".jar");
604 Path manifestPath = bundleDir.resolve("META-INF/MANIFEST.MF");
605 Manifest manifest;
606 try (InputStream in = Files.newInputStream(manifestPath)) {
607 manifest = new Manifest(in);
608 }
609 try (JarOutputStream jarOut = new JarOutputStream(Files.newOutputStream(jarPath), manifest)) {
610 Files.walkFileTree(bundleDir, new SimpleFileVisitor<Path>() {
611
612 @Override
613 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
614 if (file.getFileName().toString().equals("MANIFEST.MF"))
615 return super.visitFile(file, attrs);
616 JarEntry entry = new JarEntry(bundleDir.relativize(file).toString());
617 jarOut.putNextEntry(entry);
618 Files.copy(file, jarOut);
619 return super.visitFile(file, attrs);
620 }
621
622 });
623 }
624 deleteDirectory(bundleDir);
625 return jarPath;
626 }
627
628 public static void main(String[] args) {
629 Path factoryBase = Paths.get("../../output/a2").toAbsolutePath().normalize();
630 A2Factory factory = new A2Factory(factoryBase);
631
632 Path descriptorsBase = Paths.get("../tp").toAbsolutePath().normalize();
633
634 // factory.processSingleM2ArtifactDistributionUnit(descriptorsBase.resolve("org.argeo.tp.apache").resolve("org.apache.xml.resolver.bnd"));
635 // factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp.apache/apache-sshd"));
636 // factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp.jetty/jetty"));
637 // factory.processM2BasedDistributionUnit(descriptorsBase.resolve("org.argeo.tp.jetty/jetty-websocket"));
638 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.eclipse.rcp"));
639 System.exit(0);
640
641 // Eclipse
642 factory.processEclipseArchive(
643 descriptorsBase.resolve("org.argeo.tp.eclipse.equinox").resolve("eclipse-equinox"));
644 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rap").resolve("eclipse-rap"));
645 factory.processEclipseArchive(descriptorsBase.resolve("org.argeo.tp.eclipse.rcp").resolve("eclipse-rcp"));
646
647 System.exit(0);
648
649 // Maven
650 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.sdk"));
651 factory.processCategory(descriptorsBase.resolve("org.argeo.tp"));
652 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.apache"));
653 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jetty"));
654 factory.processCategory(descriptorsBase.resolve("org.argeo.tp.jcr"));
655 }
656 }