FELIX-4867 Remove orphaned bundle revisions on uninstall of previous consumers.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1676309 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index 08e4ed7..ab12322 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -2674,6 +2674,28 @@
 
     void uninstallBundle(BundleImpl bundle) throws BundleException
     {
+        // Populate a set of refresh candidates. This also includes any bundles that this bundle
+        // is importing packages from but have previously been uninstalled.
+        List<Bundle> refreshCandidates = new ArrayList<Bundle>();
+        refreshCandidates.add(bundle); // Add this bundle first, so that it gets refreshed first later on
+        BundleRevisions bundleRevisions = bundle.adapt(BundleRevisions.class);
+        if (bundleRevisions != null)
+        {
+            for (BundleRevision br : bundleRevisions.getRevisions())
+            {
+                BundleWiring bw = br.getWiring();
+                if (bw != null)
+                {
+                    for (BundleWire wire : bw.getRequiredWires(null))
+                    {
+                        Bundle b = wire.getProvider().getBundle();
+                        if (Bundle.UNINSTALLED == b.getState() && !refreshCandidates.contains(b))
+                            refreshCandidates.add(b);
+                    }
+                }
+            }
+        }
+
         // Acquire bundle lock.
         try
         {
@@ -2779,20 +2801,23 @@
         {
             try
             {
-                // If the bundle is not used by anyone, then garbage
-                // collect it now.
-                if (!m_dependencies.hasDependents(bundle))
+                for (Bundle b : refreshCandidates)
                 {
-                    try
+                    // If the bundle is not used by anyone, then garbage
+                    // collect it now.
+                    if (!m_dependencies.hasDependents(b))
                     {
-                        List<Bundle> list = Collections.singletonList((Bundle) bundle);
-                        refreshPackages(list, null);
-                    }
-                    catch (Exception ex)
-                    {
-                        m_logger.log(bundle,
-                            Logger.LOG_ERROR,
-                            "Unable to immediately garbage collect the bundle.", ex);
+                        try
+                        {
+                            List<Bundle> list = Collections.singletonList(b);
+                            refreshPackages(list, null);
+                        }
+                        catch (Exception ex)
+                        {
+                            m_logger.log(b,
+                                Logger.LOG_ERROR,
+                                "Unable to immediately garbage collect the bundle.", ex);
+                        }
                     }
                 }
             }
diff --git a/framework/src/test/java/org/apache/felix/framework/UninstallBundleTest.java b/framework/src/test/java/org/apache/felix/framework/UninstallBundleTest.java
new file mode 100644
index 0000000..241758a
--- /dev/null
+++ b/framework/src/test/java/org/apache/felix/framework/UninstallBundleTest.java
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.framework;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
+
+import junit.framework.TestCase;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+import org.osgi.framework.FrameworkListener;
+import org.osgi.service.packageadmin.ExportedPackage;
+
+public class UninstallBundleTest extends TestCase
+{
+    private static final int DELAY = 1000;
+
+    public void testUninstallBundleCleansUpRevision() throws Exception {
+        Map<String, String> params = new HashMap<String, String>();
+        params.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
+            "org.osgi.framework; version=1.4.0,"
+            + "org.osgi.service.packageadmin; version=1.2.0,"
+            + "org.osgi.service.startlevel; version=1.1.0,"
+            + "org.osgi.util.tracker; version=1.3.3,"
+            + "org.osgi.service.url; version=1.0.0");
+        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);
+
+        String mfA = "Bundle-SymbolicName: A\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Export-Package: org.foo.bar\n";
+        File bundleAFile = createBundle(mfA, cacheDir);
+
+        String mfB = "Bundle-SymbolicName: B\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Export-Package: org.foo.bbr\n";
+        File bundleBFile = createBundle(mfB, cacheDir);
+
+        String mfC = "Bundle-SymbolicName: C\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Import-Package: org.foo.bar, org.foo.bbr\n";
+        File bundleCFile = createBundle(mfC, cacheDir);
+
+        final List<Bundle> shouldNotBeRefreshed = new ArrayList<Bundle>();
+        Felix felix = new Felix(params) {
+            @Override
+            void refreshPackages(Collection<Bundle> targets, FrameworkListener[] listeners)
+            {
+                if (targets != null)
+                {
+                    for (Bundle b : targets)
+                    {
+                        if (shouldNotBeRefreshed.contains(b))
+                            fail("Bundle " + b + " should not be refreshed");
+                    }
+                }
+                super.refreshPackages(targets, listeners);
+            }
+        };
+
+        felix.init();
+        felix.start();
+
+        try
+        {
+            Bundle bundleA = felix.getBundleContext().installBundle(bundleAFile.toURI().toString());
+            bundleA.start();
+
+            Bundle bundleB = felix.getBundleContext().installBundle(bundleBFile.toURI().toString());
+            bundleB.start();
+            // This bundle is not going to be uninstalled, so it should not be refreshed
+            shouldNotBeRefreshed.add(bundleB);
+
+            Bundle bundleC = felix.getBundleContext().installBundle(bundleCFile.toURI().toString());
+            bundleC.start();
+
+            bundleA.uninstall();
+
+            boolean foundBar = false;
+            for (ExportedPackage ep : felix.getExportedPackages((Bundle) null))
+            {
+                if ("org.foo.bar".equals(ep.getName()))
+                    foundBar = true;
+            }
+            assertTrue("The system should still export org.foo.bar as C is importing it.", foundBar);
+
+            bundleC.uninstall();
+
+            for (ExportedPackage ep : felix.getExportedPackages((Bundle) null))
+            {
+                if ("org.foo.bar".equals(ep.getName()))
+                    fail("Should not export org.foo.bar any more!");
+            }
+
+            boolean foundBbr = false;
+            for (ExportedPackage ep : felix.getExportedPackages((Bundle) null))
+            {
+                if ("org.foo.bbr".equals(ep.getName()))
+                    foundBbr = true;
+            }
+            assertTrue("The system should still export org.foo.bbr as it was not uninstalled.", foundBbr);
+        }
+        finally
+        {
+            felix.stop();
+            Thread.sleep(DELAY);
+            deleteDir(cacheDir);
+        }
+    }
+
+    private static File createBundle(String manifest, File tempDir) throws IOException
+    {
+        File f = File.createTempFile("felix-bundle", ".jar", tempDir);
+
+        Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("utf-8")));
+        mf.getMainAttributes().putValue("Manifest-Version", "1.0");
+        JarOutputStream os = new JarOutputStream(new FileOutputStream(f), mf);
+
+        os.close();
+        return f;
+    }
+
+    private static void deleteDir(File root) throws IOException
+    {
+        if (root.isDirectory())
+        {
+            for (File file : root.listFiles())
+            {
+                deleteDir(file);
+            }
+        }
+        assertTrue(root.delete());
+    }
+
+}