You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by pa...@apache.org on 2021/09/23 15:31:05 UTC

[felix-dev] 01/01: FELIX-6429: ServiceObjects should not throw IAE if the service is unregistered.

This is an automated email from the ASF dual-hosted git repository.

pauls pushed a commit to branch issues/FELIX-6429
in repository https://gitbox.apache.org/repos/asf/felix-dev.git

commit 9c73890558cecf8bbaf3fe1b44e9a0abd5752b66
Author: Karl Pauls <ka...@gmail.com>
AuthorDate: Thu Sep 23 17:30:49 2021 +0200

    FELIX-6429: ServiceObjects should not throw IAE if the service is unregistered.
---
 .../apache/felix/framework/BundleContextImpl.java  |  2 +-
 .../apache/felix/framework/ServiceObjectsTest.java | 72 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java b/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
index 3b79f1a..5386a3f 100644
--- a/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
+++ b/framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
@@ -559,7 +559,7 @@ class BundleContextImpl implements BundleContext
             checkValidity();
 
             // Unget the specified service.
-            if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) )
+            if ( !m_felix.ungetService(m_bundle, m_ref, srvObj) && m_ref.getBundle() != null )
             {
             	throw new IllegalArgumentException();
             }
diff --git a/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java b/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java
new file mode 100644
index 0000000..4189515
--- /dev/null
+++ b/framework/src/test/java/org/apache/felix/framework/ServiceObjectsTest.java
@@ -0,0 +1,72 @@
+package org.apache.felix.framework;
+
+import junit.framework.TestCase;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceObjects;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.launch.Framework;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+public class ServiceObjectsTest extends TestCase
+{
+    public void testServiceObjects() throws Exception
+    {
+        Map params = new HashMap();
+        File cacheDir = File.createTempFile("felix-cache", ".dir");
+        cacheDir.delete();
+        cacheDir.mkdirs();
+        String cache = cacheDir.getPath();
+        params.put("felix.cache.profiledir", cache);
+        params.put("felix.cache.dir", cache);
+        params.put(Constants.FRAMEWORK_STORAGE, cache);
+        Framework f = new Felix(params);
+        f.init();
+        f.start();
+
+        try
+        {
+            BundleContext context = f.getBundleContext();
+            ServiceRegistration<Object> registration =
+                    context.registerService(Object.class, new Object(), null);
+
+            ServiceReference<Object> reference = registration.getReference();
+
+            ServiceObjects<Object> serviceObjects = context.getServiceObjects(reference);
+
+            Object service = serviceObjects.getService();
+
+            serviceObjects.ungetService(service);
+
+            assertEquals(service, serviceObjects.getService());
+            service = serviceObjects.getService();
+
+            registration.unregister();
+
+            serviceObjects.ungetService(service);
+        }
+        finally
+        {
+            f.stop();
+            Thread.sleep(1000);
+            deleteDir(cacheDir);
+        }
+    }
+
+    private static void deleteDir(File root) throws IOException
+    {
+        if (root.isDirectory())
+        {
+            for (File file : root.listFiles())
+            {
+                deleteDir(file);
+            }
+        }
+        assertTrue(root.delete());
+    }
+}