FELIX-1851: FileInstall watched bundles are restarted twice upon update

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@891210 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/fileinstall/pom.xml b/fileinstall/pom.xml
index fa8d386..0b98c6d 100644
--- a/fileinstall/pom.xml
+++ b/fileinstall/pom.xml
@@ -35,12 +35,12 @@
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
-      <version>4.0.0</version>
+      <version>4.1.0</version>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
       <artifactId>org.osgi.compendium</artifactId>
-      <version>4.0.0</version>
+      <version>4.1.0</version>
     </dependency>
   </dependencies>
   <build>
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
index 8d0a792..52ce8fe 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/DirectoryWatcher.java
@@ -390,8 +390,6 @@
         if (uninstalledBundles.size() > 0 || updatedBundles.size() > 0)
         {
             // Refresh if any bundle got uninstalled or updated.
-            // This can lead to restart of recently updated bundles, but
-            // don't worry about that at this point of time.
             refresh();
         }
 
@@ -893,6 +891,7 @@
                     if (Util.loadChecksum(b, context) != checksum) {
                         log(Logger.LOG_WARNING,
                             "A bundle with the same symbolic name (" + sn + ") and version (" + vStr + ") is already installed.  Updating this bundle instead.", null);
+                        stopTransient(b);
                         Util.storeChecksum(b, checksum, context);
                         b.update(is);
                     }
@@ -980,6 +979,7 @@
                         + ". The bundle has been uninstalled", null);
                     return null;
                 }
+                stopTransient(bundle);
                 Util.storeChecksum(bundle, artifact.getChecksum(), context);
                 InputStream in = (transformed != null) ? transformed.openStream() : new FileInputStream(path);
                 try
@@ -1005,6 +1005,7 @@
                         + ". The bundle has been uninstalled", null);
                     return null;
                 }
+                stopTransient(bundle);
                 Util.storeChecksum(bundle, artifact.getChecksum(), context);
                 InputStream in = new FileInputStream(transformed != null ? transformed : path);
                 try
@@ -1025,6 +1026,15 @@
         return bundle;
     }
 
+    private void stopTransient(Bundle bundle) throws BundleException {
+        // Stop the bundle transiently so that it will be restarted when startAllBundles() is called
+        // but this avoids the need to restart the bundle twice (once for the update and another one
+        // when refreshing packages).
+        if (startBundles) {
+            bundle.stop(Bundle.STOP_TRANSIENT);
+        }
+    }
+
     private void startAllBundles()
     {
         List bundles = new ArrayList();