daniancu commented on code in PR #20:
URL:
https://github.com/apache/sling-org-apache-sling-discovery-oak/pull/20#discussion_r3729180636
##########
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:
I made the changes but broke existing tests. To make it work we need a small
change in `MockFactory` from `sling-org-apache-sling-discovery-base` module.
But that module was bumped to 2.1.0 and `MockFactory` was removed as per
https://github.com/apache/sling-org-apache-sling-discovery-base/commit/729a566d627f3f393b45b88826ee87fe6ef6da7c#diff-cdc19a365ec199f6ae169da2474404cb63b52061a1de7d5b2132ef2a5b46211e
Tests in `sling-org-apache-sling-discovery-base` were converted, those in
this module, not. Sounds like a new PR
--
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]