FELIX-1540:  [FileInstall] When removing/re-adding a bundle, all the dependent bundles don't start anymore

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@810854 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
index b15d91e..9891b5f 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/DirectoryWatcher.java
@@ -99,9 +99,6 @@
     // Represents artifacts that could not be installed
     Map/* <File, Artifact> */ installationFailures = new HashMap/* <File, Artifact> */();
 
-    // Represents artifacts that could not be installed
-    Set/* <Bundle> */ startupFailures = new HashSet/* <Bundle> */();
-    
     public DirectoryWatcher(Dictionary properties, BundleContext context)
     {
         super(properties.toString());
@@ -298,12 +295,9 @@
 
                 if (startBundles)
                 {
-                    // Try to start all the bundles that we could not start last time.
-                    // Make a copy, because start() changes the underlying collection
-                    start(new HashSet(startupFailures));
-		            // Start updated bundles.
-		            start(updatedBundles);
-                    // Start newly installed bundles
+                    // Try to start all the bundles that are not persistently stopped
+                    startAllBundles();
+                    // Try to start newly installed bundles
                     start(installedBundles);
                 }
 
@@ -729,7 +723,6 @@
                 }
                 bundle.uninstall();
             }
-            startupFailures.remove(bundle);
             log("Uninstalled " + path, null);
         }
         catch (Exception e)
@@ -773,7 +766,6 @@
                     in.close();
                 }
             }
-            startupFailures.remove(bundle);
             artifact.setLastModified(Util.getLastModified(path));
             log("Updated " + path, null);
         }
@@ -784,6 +776,28 @@
         return bundle;
     }
 
+    private void startAllBundles()
+    {
+        List bundles = new ArrayList();
+        for (Iterator it = currentManagedArtifacts.values().iterator(); it.hasNext();)
+        {
+            Artifact artifact = (Artifact) it.next();
+            if (artifact.getBundleId() > 0)
+            {
+                Bundle bundle = context.getBundle(artifact.getBundleId());
+                if (bundle != null)
+                {
+                    if (bundle.getState() != Bundle.STARTING && bundle.getState() != Bundle.ACTIVE
+                            && FileInstall.getStartLevel().isBundlePersistentlyStarted(bundle))
+                    {
+                        bundles.add(bundle);
+                    }
+                }
+            }
+        }
+        start(bundles);
+    }
+
     private void start(Collection/* <Bundle> */ bundles)
     {
         for (Iterator b = bundles.iterator(); b.hasNext(); )
@@ -803,13 +817,11 @@
             try
             {
                 bundle.start();
-                startupFailures.remove(bundle);
                 log("Started bundle: " + bundle.getLocation(), null);
             }
             catch (BundleException e)
             {
                 log("Error while starting bundle: " + bundle.getLocation(), e);
-                startupFailures.add(bundle);
             }
         }
     }
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
index e17216d..0a8ee82 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/FileInstall.java
@@ -39,6 +39,7 @@
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedServiceFactory;
 import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.service.startlevel.StartLevel;
 import org.osgi.util.tracker.ServiceTracker;
 
 /**
@@ -50,6 +51,7 @@
 public class FileInstall implements BundleActivator, ManagedServiceFactory
 {
     static ServiceTracker padmin;
+    static ServiceTracker startLevel;
     static ServiceTracker cmTracker;
     static List /* <ArtifactListener> */ listeners = new ArrayList /* <ArtifactListener> */();
     BundleContext context;
@@ -68,6 +70,8 @@
 
         padmin = new ServiceTracker(context, PackageAdmin.class.getName(), null);
         padmin.open();
+        startLevel = new ServiceTracker(context, StartLevel.class.getName(), null);
+        startLevel.open();
         cmTracker = new ServiceTracker(context, ConfigurationAdmin.class.getName(), null)
         {
             public Object addingService(ServiceReference serviceReference)
@@ -216,6 +220,24 @@
         }
     }
 
+    static StartLevel getStartLevel()
+    {
+        return getStartLevel(10000);
+    }
+
+    static StartLevel getStartLevel(long timeout)
+    {
+        try
+        {
+            return (StartLevel) startLevel.waitForService(timeout);
+        }
+        catch (InterruptedException e)
+        {
+            Thread.currentThread().interrupt();
+            return null;
+        }
+    }
+
     static ConfigurationAdmin getConfigurationAdmin()
     {
         return getConfigurationAdmin(10000);