Core spec compliance: fix Bundle.adapt(ServiceReferenceDTO[].class)

The new behaviour is spec compliant, but will cause a known OSGi R6 CT failure:
  testArrayServiceReferenceDTO
    ServiceReferenceDTO[] for stopped bundle is null

This is known deviation and is documented here:
http://felix.apache.org/documentation/development/using-the-osgi-compliance-tests.html


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1672365 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/DTOFactory.java b/framework/src/main/java/org/apache/felix/framework/DTOFactory.java
index 3fba318..2107a5a 100644
--- a/framework/src/main/java/org/apache/felix/framework/DTOFactory.java
+++ b/framework/src/main/java/org/apache/felix/framework/DTOFactory.java
@@ -27,6 +27,7 @@
 
 import org.osgi.dto.DTO;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
@@ -313,6 +314,10 @@
 
     private static ServiceReferenceDTO[] createServiceReferenceDTOArray(Bundle bundle)
     {
+        BundleContext ctx = bundle.getBundleContext();
+        if (ctx == null)
+            return null;
+
         ServiceReference<?>[] svcs = bundle.getRegisteredServices();
         if (svcs == null)
             return new ServiceReferenceDTO[0];
diff --git a/framework/src/test/java/org/apache/felix/framework/DTOFactoryTest.java b/framework/src/test/java/org/apache/felix/framework/DTOFactoryTest.java
index fc897bf..4365835 100644
--- a/framework/src/test/java/org/apache/felix/framework/DTOFactoryTest.java
+++ b/framework/src/test/java/org/apache/felix/framework/DTOFactoryTest.java
@@ -19,6 +19,7 @@
 package org.apache.felix.framework;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
@@ -31,7 +32,6 @@
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
 
-import org.apache.felix.framework.Felix;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -130,6 +130,24 @@
     }
 
     @Test
+    public void testServiceReferenceDTOArrayStoppedBundle() throws Exception
+    {
+        String mf = "Bundle-SymbolicName: tb2\n"
+                + "Bundle-Version: 1.2.3\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Import-Package: org.osgi.framework;version=\"[1.1,2)\"";
+        File bf = createBundle(mf);
+        Bundle bundle = framework.getBundleContext().installBundle(bf.toURI().toURL().toExternalForm());
+
+        assertNull("Precondition", bundle.getBundleContext());
+        ServiceReferenceDTO[] dtos = bundle.adapt(ServiceReferenceDTO[].class);
+
+        // Note this is incorrectly tested by the Core Framework R6 CT, which expects an
+        // empty array. However this is not correct and recorded as a deviation.
+        assertNull("As the bundle is not started, the dtos should be null", dtos);
+    }
+
+    @Test
     public void testBundleRevisionDTO() throws Exception
     {
         String mf = "Bundle-SymbolicName: tb2\n"