joerghoh commented on code in PR #20:
URL:
https://github.com/apache/sling-org-apache-sling-discovery-oak/pull/20#discussion_r3720259946
##########
src/main/java/org/apache/sling/discovery/oak/OakDiscoveryService.java:
##########
@@ -581,6 +604,80 @@ public void updateProperties() {
}
}
+ /**
+ * Returns {@code true} when this component or the framework is shutting
+ * down. Latches {@link #deactivating} on first observation so subsequent
+ * calls are a single volatile read. Package-private for unit testing.
+ */
+ boolean isShuttingDown() {
+ if (deactivating) {
+ return true;
+ }
+ final Bundle sb = systemBundle.get();
+ if (sb == null) {
+ return false;
+ }
+ try {
+ final int state = sb.getState();
+ if (state == Bundle.STOPPING
+ || state == Bundle.RESOLVED
+ || state == Bundle.INSTALLED
+ || state == Bundle.UNINSTALLED) {
+ deactivating = true;
+ logger.info("isShuttingDown: system bundle state {} observed -
"
+ + "OakDiscoveryService will skip further property
writes", state);
+ return true;
+ }
+ } catch (IllegalStateException e) {
+ deactivating = true;
+ logger.info("isShuttingDown: system bundle reference invalidated -
"
+ + "OakDiscoveryService will skip further property writes");
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Test-only hook to install a system-bundle reference without going
+ * through {@link #cacheSystemBundle()}.
+ */
+ void setSystemBundleForTesting(final Bundle bundle) {
+ this.systemBundle.set(bundle);
+ }
+
+ private void cacheSystemBundle() {
+ cacheSystemBundle(FrameworkUtil.getBundle(OakDiscoveryService.class));
+ }
+
+ /**
+ * Package-private overload taking the caller-bundle explicitly, so unit
+ * tests can exercise the {@link BundleContext} failure branches without
+ * an OSGi framework.
+ */
+ void cacheSystemBundle(final Bundle ourBundle) {
+ if (ourBundle == null) {
+ logger.debug("cacheSystemBundle: no OSGi framework detected via
FrameworkUtil"
+ + " - shutdown detection degrades to deactivate() only");
+ return;
+ }
+ try {
+ final BundleContext ctx = ourBundle.getBundleContext();
Review Comment:
More a nitpick than an actual issue: There is a version of the
``activate()`` method which passes in the ComponentContext; from that you can
derive the BundleContext of this bundle directly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]