Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion source/java/src/org/lucee/extension/osgi/OSGiServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,66 @@ private Bundle install(BundleContext ctx, File jar) throws IOException {
}
}

private static final ThreadLocal<Set<String>> REQUIRING = ThreadLocal.withInitial(HashSet::new);

/** the bundles a jar needs through Require-Bundle (not optional): name and range, the range is null when there is no bundle-version */
static List<Object[]> requiredBundles(File jar) {
List<Object[]> result = new ArrayList<>();
try (JarFile jf = new JarFile(jar)) {
Manifest mf = jf.getManifest();
String header = mf == null ? null : mf.getMainAttributes().getValue("Require-Bundle");
if (header == null) return result;
for (String entry: header.split(",(?=[^\"]*(?:\"[^\"]*\"[^\"]*)*$)")) {
String[] parts = entry.split(";(?=[^\"]*(?:\"[^\"]*\"[^\"]*)*$)");
String name = parts[0].trim();
if (name.isEmpty()) continue;
VersionRange range = null;
boolean optional = false;
for (int i = 1; i < parts.length; i++) {
String a = parts[i].trim();
if (a.startsWith("resolution:=")) optional = "optional".equals(removeQuotes(a.substring(12)));
else if (a.startsWith("bundle-version=")) {
try {
range = VersionRange.valueOf(removeQuotes(a.substring(15)));
}
catch (IllegalArgumentException iae) {
}
}
}
// the system bundle is the framework itself
if (!optional && !"system.bundle".equals(name)) result.add(new Object[] { name, range });
}
}
catch (IOException e) {
}
return result;
}

/**
* a bundle with Require-Bundle is not resolved without the bundles it requires, and the
* resolver reports the first missing requirement only (often a package that the required bundle exports),
* so the required bundles are loaded before the bundle is installed. What cannot be loaded here is
* reported by the resolver when the bundle is started.
*/
private void loadRequiredBundles(File jar, File[] dirs) {
Set<String> active = REQUIRING.get();
if (!active.add(jar.getAbsolutePath())) return;
try {
for (Object[] required: requiredBundles(jar)) {
try {
if (required[1] == null) load((String) required[0], (String) null, dirs, true);
else load((String) required[0], (VersionRange) required[1], dirs);
}
catch (IOException | RuntimeException e) {
}
}
}
finally {
active.remove(jar.getAbsolutePath());
if (active.isEmpty()) REQUIRING.remove();
}
}

/**
* the installed bundle, installing it from the local bundle directory, one of the given directories or a
* download when necessary
Expand All @@ -385,6 +445,7 @@ private Bundle load(String name, String version, File[] dirs, boolean versionOnl
}
if (jar == null && versionOnlyMatters) jar = findJar(bundleDirectory, name, (String) null);
if (jar == null) jar = download(name, version);
loadRequiredBundles(jar, dirs);
return install(ctx, jar);
}

Expand Down Expand Up @@ -414,6 +475,7 @@ private Bundle load(String name, VersionRange range, File[] dirs) throws IOExcep
if (version == null) version = unmappedVersion(range);
jar = download(name, version);
}
loadRequiredBundles(jar, dirs);
return install(ctx, jar);
}

Expand All @@ -426,7 +488,7 @@ private File download(String name, String version) throws IOException {
try {
url = provider.getBundleAsURL(name, version);
}
catch (BundleException | RuntimeException e) {
catch (IOException | BundleException | RuntimeException e) {
throw new IOException("no download for the bundle [" + name + ":" + version + "]: " + e.getMessage(), e);
}
// a unique file, the same bundle may be downloaded by two threads at the same time
Expand Down
90 changes: 90 additions & 0 deletions source/java/test/org/lucee/extension/osgi/RequireBundleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.lucee.extension.osgi;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/** a bundle that has Require-Bundle gets the required bundles loaded first, the load of a missing bundle keeps the reason as cause */
class RequireBundleTest {

@TempDir
File dir;

private OSGiServiceImpl service;

@BeforeEach
void start() throws IOException {
service = new OSGiServiceImpl(Files.createTempDirectory("osgi-require").toFile());
}

@AfterEach
void stop() {
service.shutdown();
}

private File bundle(String fileName, String symbolicName, String version, String... headers) throws IOException {
Manifest mf = new Manifest();
Attributes a = mf.getMainAttributes();
a.put(Attributes.Name.MANIFEST_VERSION, "1.0");
a.putValue("Bundle-ManifestVersion", "2");
a.putValue("Bundle-SymbolicName", symbolicName);
a.putValue("Bundle-Version", version);
for (int i = 0; i < headers.length; i += 2) {
a.putValue(headers[i], headers[i + 1]);
}
File jar = new File(dir, fileName);
new JarOutputStream(new FileOutputStream(jar), mf).close();
return jar;
}

@Test
void requiredBundlesOfAJar() throws IOException {
File jar = bundle("a.jar", "test.a", "1.0.0", "Require-Bundle", "test.b;bundle-version=\"[1.0.0,2)\",test.c,test.d;resolution:=optional");
List<Object[]> required = OSGiServiceImpl.requiredBundles(jar);
assertEquals(2, required.size());
assertEquals("test.b", required.get(0)[0]);
assertEquals(org.osgi.framework.VersionRange.valueOf("[1.0.0,2)"), required.get(0)[1]);
assertEquals("test.c", required.get(1)[0]);
assertEquals(null, required.get(1)[1]);
}

@Test
void requiredBundleIsLoadedBeforeTheBundleThatImportsItsPackage() throws IOException {
// the resolver names the missing package first and no bundle is known for it, only the Require-Bundle tells where it comes from
bundle("b.jar", "test.b", "1.0.0", "Export-Package", "test.b.pkg;version=\"1.0.0\"");
bundle("a.jar", "test.a", "1.0.0", "Import-Package", "test.b.pkg;version=\"[1.0,2)\"", "Require-Bundle", "test.b;bundle-version=\"1.0.0\"");
assertNotNull(service.getClassLoader("test.a", "1.0.0", new File[] { dir }));

BundleContext ctx = (BundleContext) service.getBundleContext();
boolean b = false;
for (Bundle bundle: ctx.getBundles()) {
if ("test.b".equals(bundle.getSymbolicName())) b = true;
}
assertTrue(b, "test.b must be installed");
}

@Test
void failedDownloadIsTheCauseOfTheError() {
IOException e = assertThrows(IOException.class, () -> service.loadClass("org.lucee.ldev6313.does-not-exist", "1.2.3", "org.ldev6313.DoesNotExist", null, false));
assertTrue(e.getMessage().contains("org.lucee.ldev6313.does-not-exist"), e.getMessage());
assertNotNull(e.getCause(), "the failed download is attached as cause");
assertTrue(e.getCause().getMessage() != null && !e.getCause().getMessage().isEmpty());
}
}