Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ private synchronized List<Content> initializeContentPath() throws Exception
{
return m_contentPath;
}
// A closed revision has no content, but a class loader of its
// wiring may still look up resources concurrently. Don't cache
// the empty path, since a refresh can reopen this revision with
// resetContent().
if (m_content == null)
{
return Collections.emptyList();
}
List<Content> contentList = new ArrayList<>();
calculateContentPath(this, getContent(), contentList, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
Expand All @@ -43,8 +47,11 @@
import org.junit.jupiter.api.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.FrameworkEvent;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.wiring.BundleRevision;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.framework.wiring.FrameworkWiring;

class ResourceLoadingTest
{
Expand Down Expand Up @@ -227,6 +234,53 @@ void resourceLoadingUsingURLClassLoaderJDK9() throws Exception {
assertThat(urlClassLoader.getResource("ej2.txt")).isNotNull();
}

@Test
void resourceLoadingFromClosedRevisionFindsNothing() throws Exception
{
String bmf = "Bundle-SymbolicName: cap.bundle\n"
+ "Bundle-Version: 1.2.3.Blah\n"
+ "Bundle-ManifestVersion: 2\n"
+ "Import-Package: org.osgi.framework\n";
File bundleFile = File.createTempFile("felix-bundle", ".jar", tempDir);

Manifest mf = new Manifest(new ByteArrayInputStream(bmf.getBytes("utf-8")));
mf.getMainAttributes().putValue("Manifest-Version", "1.0");
JarOutputStream os = new JarOutputStream(new FileOutputStream(bundleFile), mf);

String name = "b1.txt";
os.putNextEntry(new ZipEntry(name));
os.write("This is a Test".getBytes());
os.close();

Bundle testBundle = felix.getBundleContext().installBundle(bundleFile.toURI().toASCIIString());
testBundle.start();

BundleRevisionImpl oldRevision = (BundleRevisionImpl) testBundle.adapt(BundleRevision.class);
BundleWiringImpl.BundleClassLoader oldClassLoader =
(BundleWiringImpl.BundleClassLoader) testBundle.adapt(BundleWiring.class).getClassLoader();
assertThat(oldClassLoader.findResource(name)).isNotNull();

// Updating and refreshing closes the old revision, while a lookup
// through the class loader of its wiring may still be in progress
try (FileInputStream updated = new FileInputStream(bundleFile))
{
testBundle.update(updated);
}
CountDownLatch refreshed = new CountDownLatch(1);
felix.adapt(FrameworkWiring.class).refreshBundles(Collections.singletonList(testBundle), event ->
{
if (event.getType() == FrameworkEvent.PACKAGES_REFRESHED)
{
refreshed.countDown();
}
});
assertThat(refreshed.await(10, TimeUnit.SECONDS)).as("refresh completed").isTrue();
assertThat(oldRevision.getContent()).as("precondition: old revision is closed").isNull();

assertThat(oldClassLoader.findResource(name)).isNull();
assertThat(oldClassLoader.findResources(name).hasMoreElements()).isFalse();
}

ClassLoader createClassLoader(Bundle bundle) {
List<URL> urls = new ArrayList<>();
Collection<String> resources = bundle.adapt(BundleWiring.class).listResources("/", "*.jar", BundleWiring.LISTRESOURCES_LOCAL);
Expand Down