Bug 8261: prevent TestBundleDiag from throwing NPE
[odlparent.git] / bundles4-test / src / main / java / org / opendaylight / odlparent / bundles4test / TestBundleDiag.java
index a6ec619e23daed571335c83267745b6d32b2285a..e86e52ad94c768680b16b8dac9cdd2968549fedf 100644 (file)
@@ -14,6 +14,7 @@ import java.util.concurrent.TimeUnit;
 import org.apache.karaf.bundle.core.BundleService;
 import org.awaitility.Awaitility;
 import org.awaitility.core.ConditionTimeoutException;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
@@ -127,8 +128,14 @@ public class TestBundleDiag {
                 + "diag failure BundleService reported bundle(s) which are not active");
         try {
             for (ServiceReference<?> serviceRef : bundleContext.getAllServiceReferences(null, null)) {
-                LOG.info("{} defines OSGi Service {} used by {}", serviceRef.getBundle().getSymbolicName(),
-                        util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+                Bundle bundle = serviceRef.getBundle();
+                // serviceRef.getBundle() can return null if the bundle was destroyed
+                if (bundle != null) {
+                    LOG.info("{} defines OSGi Service {} used by {}", bundle.getSymbolicName(),
+                            util.getProperties(serviceRef), util.getUsingBundleSymbolicNames(serviceRef));
+                } else {
+                    LOG.trace("skipping reporting service reference as the underlying bundle is null");
+                }
             }
         } catch (InvalidSyntaxException e) {
             LOG.error("logOSGiServices() failed due to InvalidSyntaxException", e);