Skip to content

Commit

Permalink
Adjustments from review
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Aug 24, 2022
1 parent 069e906 commit 6e9b3d9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
24 changes: 18 additions & 6 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Resource> getVersionedResource(String path, int release) {
if (isMultiRelease() && release >= MULTI_RELEASE_MIN_VERSION) {
Expand Down Expand Up @@ -499,9 +501,11 @@ public Map<String, Resource> 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<String, Resource> getVersionedResources(int release) {
if (isMultiRelease()) {
Expand Down Expand Up @@ -593,7 +597,7 @@ public Optional<Manifest> getManifest(int release) {
return manifest().map(original -> {
Manifest copy = new Manifest(original);
if (release >= MULTI_RELEASE_MIN_VERSION) {
Optional<Resource> releaseEntry = getVersionedResource(JarFile.MANIFEST_NAME, release);
Optional<Resource> releaseEntry = getVersionedResource(SUPPLEMENTAL_MANIFEST_PATH, release);
releaseEntry.map(resource -> {
try (InputStream in = resource.openInputStream()) {
return new Manifest(in);
Expand Down Expand Up @@ -640,6 +644,10 @@ Optional<Manifest> manifest() {
}
}

Optional<ModuleAttribute> moduleAttribute() throws Exception {
return moduleAttribute(MULTI_RELEASE_DEFAULT_VERSION);
}

Optional<ModuleAttribute> moduleAttribute(int release) throws Exception {
check();
if (release < MULTI_RELEASE_MIN_VERSION) {
Expand Down Expand Up @@ -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()
Expand Down
43 changes: 42 additions & 1 deletion biz.aQute.bndlib/test/aQute/bnd/build/JarTest.java
Expand Up @@ -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";

Expand Down Expand Up @@ -132,6 +132,47 @@ public void testMultiReleaseJaModule() throws Exception {
}
}

/**
* This test assumes the following layout:
*
* <pre>
* /Foo.class
* /Bar.class
* /META-INF/versions/9/Foo.class
* /META-INF/versions/11/Bar.class
* </pre>
*
* Calling
* <ul>
* <li>getVersionedResource(Foo.class, 11) - I would expect to get returned
* the resource at /META-INF/versions/9/Foo.class</li>
* <li>getVersionedResource(Bar.class, 9) - I would expect to get returned
* the resource at /Bar.class</li>
* </ul>
*
* @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)
Expand Down

0 comments on commit 6e9b3d9

Please sign in to comment.