From 6e9b3d9b6d17a4d9c02d49653032c4194e7784c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 24 Aug 2022 12:38:32 +0200 Subject: [PATCH] Adjustments from review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Christoph Läubrich --- biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java | 24 ++++++++--- .../test/aQute/bnd/build/JarTest.java | 43 ++++++++++++++++++- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java b/biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java index d5ab5c646b6..46d2cb49fd9 100644 --- a/biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java +++ b/biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java @@ -133,6 +133,7 @@ public enum Compression { public static final Pattern METAINF_SIGNING_P = Pattern .compile("META-INF/([^/]+\\.(?:DSA|RSA|EC|SF)|SIG-[^/]+)", Pattern.CASE_INSENSITIVE); static final String MULTI_RELEASE_HEADER = "Multi-Release"; + static final String SUPPLEMENTAL_MANIFEST_PATH = "OSGI-INF/MANIFEST.MF"; static final int MULTI_RELEASE_MIN_VERSION = 9; static final int MULTI_RELEASE_DEFAULT_VERSION = 0; static final int MULTI_RELEASE_VERSION_GROUP = 1; @@ -440,12 +441,13 @@ public Resource getResource(String path) { } /** - * Returns a resource taking the release version into account. + * Returns a resource taking the release version into account as described + * by the {@link JarFile#getJarEntry(String)}. * * @param path the path of the resource to read * @param release the release to use - * @return an optional representing the resource or an empty optional if the - * resource do not exits + * @return an optional representing the highest versioned resource for the + * given release or an empty optional if the resource do not exits */ public Optional getVersionedResource(String path, int release) { if (isMultiRelease() && release >= MULTI_RELEASE_MIN_VERSION) { @@ -499,9 +501,11 @@ public Map getResources() { /** * returns an (unmodifiable) view of resources in this jar according to the - * given release version. + * given release version as described by the + * {@link JarFile#getJarEntry(String)}. * - * @return a map whose keys are resource names and the value the resource + * @return a map whose keys are resource names and the value the highest + * available resource for the given release. */ public Map getVersionedResources(int release) { if (isMultiRelease()) { @@ -593,7 +597,7 @@ public Optional getManifest(int release) { return manifest().map(original -> { Manifest copy = new Manifest(original); if (release >= MULTI_RELEASE_MIN_VERSION) { - Optional releaseEntry = getVersionedResource(JarFile.MANIFEST_NAME, release); + Optional releaseEntry = getVersionedResource(SUPPLEMENTAL_MANIFEST_PATH, release); releaseEntry.map(resource -> { try (InputStream in = resource.openInputStream()) { return new Manifest(in); @@ -640,6 +644,10 @@ Optional manifest() { } } + Optional moduleAttribute() throws Exception { + return moduleAttribute(MULTI_RELEASE_DEFAULT_VERSION); + } + Optional moduleAttribute(int release) throws Exception { check(); if (release < MULTI_RELEASE_MIN_VERSION) { @@ -684,6 +692,10 @@ public String getModuleName(int release) throws Exception { .orElseGet(() -> automaticModuleName(release)); } + String automaticModuleName() { + return automaticModuleName(MULTI_RELEASE_DEFAULT_VERSION); + } + String automaticModuleName(int release) { return getManifest(release) .map(m -> m.getMainAttributes() diff --git a/biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java b/biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java index b465b552b38..e3020bb7df7 100644 --- a/biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java +++ b/biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java @@ -33,7 +33,7 @@ public class JarTest { private static final String MAIN_MANIFEST_PATH = JarFile.MANIFEST_NAME; - private static final String SUPPLEMENTAL_MANIFEST_PATH = "META-INF/versions/9/" + MAIN_MANIFEST_PATH; + private static final String SUPPLEMENTAL_MANIFEST_PATH = "META-INF/versions/9/OSGI-INF/MANIFEST.MF"; private static final String TEST_CLASS_PATH = "a/test/package/Test.class"; @@ -132,6 +132,47 @@ public void testMultiReleaseJaModule() throws Exception { } } + /** + * This test assumes the following layout: + * + *
+	 * /Foo.class
+	 * /Bar.class
+	 * /META-INF/versions/9/Foo.class
+	 * /META-INF/versions/11/Bar.class
+	 * 
+ * + * Calling + *
    + *
  • getVersionedResource(Foo.class, 11) - I would expect to get returned + * the resource at /META-INF/versions/9/Foo.class
  • + *
  • getVersionedResource(Bar.class, 9) - I would expect to get returned + * the resource at /Bar.class
  • + *
+ * + * @throws Exception + */ + @Test + public void testMultiReleaseJarMultipleResources() throws Exception { + try (Jar jar = new Jar("testme")) { + jar.setMultiRelease(true); + Resource Foo = resource(); + Resource Bar = resource(); + Resource Foo9 = resource(); + Resource Bar11 = resource(); + String fooPath = "Foo.class"; + String barPath = "Bar.class"; + jar.putResource(fooPath, Foo); + jar.putResource(barPath, Bar); + jar.putResource("META-INF/versions/9/"+fooPath, Foo9); + jar.putResource("META-INF/versions/11/"+barPath, Bar11); + assertEquals(Foo9, jar.getVersionedResource(fooPath, 11) + .get()); + assertEquals(Bar, jar.getVersionedResource(barPath, 9) + .get()); + } + } + private static Resource module(String moduleName, String moduleVersion) throws IOException { ModuleInfoBuilder builder = new ModuleInfoBuilder().module_name(moduleName) .module_version(moduleVersion)