Bug 578949 - Class loading at shutdown fails with NullPointerException
Change-Id: I55d9570b9ae8ee06087eec50184b997d551835e8
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/equinox/rt.equinox.framework/+/191204
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
index 978a43c..803c09f 100755
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/SystemBundleTests.java
@@ -3265,4 +3265,29 @@
equinox.start();
stop(equinox);
}
+
+ @Test
+ public void testGetBundleAfterShutdown() throws BundleException {
+ File config = OSGiTestsActivator.getContext().getDataFile(getName());
+ Equinox equinox = new Equinox(Collections.singletonMap(Constants.FRAMEWORK_STORAGE, config.getAbsolutePath()));
+
+ equinox.start();
+ BundleContext systemContext = equinox.getBundleContext();
+ Bundle b = systemContext.installBundle(installer.getBundleLocation("substitutes.a")); //$NON-NLS-1$
+ b.start();
+ BundleContext testContext = b.getBundleContext();
+ long id = b.getBundleId();
+ assertEquals("Unexpected bundle", b, systemContext.getBundle(id));
+
+ stop(equinox);
+
+ // system bundle always sees bundles
+ assertEquals("Unexpected bundle", b, systemContext.getBundle(id));
+ assertEquals("Unexpected bundle count", 2, systemContext.getBundles().length);
+ // other bundles may get filtered by hooks,
+ // but since we cannot call hooks the framework just returns null
+ assertNull("Expected null bundle after stop.", testContext.getBundle(id));
+ assertEquals("Unexpected bundle count", 0, testContext.getBundles().length);
+ }
+
}
diff --git a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java
index 0ba9db7..544c1cb 100644
--- a/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java
+++ b/bundles/org.eclipse.osgi/container/src/org/eclipse/osgi/internal/framework/BundleContextImpl.java
@@ -257,8 +257,13 @@
if (debug.DEBUG_HOOKS) {
Debug.println("notifyBundleFindHooks(" + allBundles + ")"); //$NON-NLS-1$ //$NON-NLS-2$
}
- container.getServiceRegistry().notifyHooksPrivileged(FindHook.class, "find", //$NON-NLS-1$
- (hook, hookRegistration) -> hook.find(context, allBundles));
+ ServiceRegistry sr = container.getServiceRegistry();
+ if (sr == null) {
+ allBundles.clear();
+ } else {
+ sr.notifyHooksPrivileged(FindHook.class, "find", //$NON-NLS-1$
+ (hook, hookRegistration) -> hook.find(context, allBundles));
+ }
}
@Override