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