FELIX-1572: fileinstall runs into an infinite loop while watching multiple directories, one of which is itself

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@825455 13f79535-47bb-0310-9956-ffa450edef68
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 a3d8951..4ba5619 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
@@ -87,6 +87,7 @@
     public final static String START_NEW_BUNDLES = "felix.fileinstall.bundles.new.start";
     public final static String NO_INITIAL_DELAY = "felix.fileinstall.noInitialDelay";
 
+    Dictionary properties;
     File watchedDirectory;
     File tmpDir;
     long poll;
@@ -112,6 +113,7 @@
     public DirectoryWatcher(Dictionary properties, BundleContext context)
     {
         super(properties.toString());
+        this.properties = properties;
         this.context = context;
         poll = getLong(properties, POLL, 2000);
         debug = getLong(properties, DEBUG, -1);
@@ -139,7 +141,13 @@
             flt = null;
         }
         scanner = new Scanner(watchedDirectory, flt);
+    }
 
+    public Dictionary getProperties() {
+        return properties;
+    }
+
+    public void start() {
         if (noInitialDelay)
         {
             log("Starting initial scan", null);
@@ -150,6 +158,7 @@
                 process(files);
             }
         }
+        super.start();
     }
 
     /**
diff --git a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/FileInstall.java b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/FileInstall.java
index e5eb74b..26415b4 100644
--- a/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/FileInstall.java
+++ b/fileinstall/src/main/java/org/apache/felix/fileinstall/internal/FileInstall.java
@@ -129,13 +129,10 @@
     public void stop(BundleContext context) throws Exception
     {
         List /*<DirectoryWatcher>*/ toClose = new ArrayList /*<DirectoryWatcher>*/();
-        if (watchers != null)
+        synchronized (watchers)
         {
-            synchronized (watchers)
-            {
-                toClose.addAll(watchers.values());
-                watchers.clear();
-            }
+            toClose.addAll(watchers.values());
+            watchers.clear();
         }
         for (Iterator w = toClose.iterator(); w.hasNext();)
         {
@@ -178,11 +175,25 @@
 
     public void updated(String pid, Dictionary properties)
     {
-        deleted(pid);
-        Util.performSubstitution(properties);    
-        
-        DirectoryWatcher watcher = new DirectoryWatcher(properties, context);
-        watchers.put(pid, watcher);
+        Util.performSubstitution(properties);
+        DirectoryWatcher watcher = null;
+        synchronized (watchers)
+        {
+            watcher = (DirectoryWatcher) watchers.get(pid);
+            if (watcher != null && watcher.getProperties().equals(properties))
+            {
+                return;
+            }
+        }
+        if (watcher != null)
+        {
+            watcher.close();
+        }
+        watcher = new DirectoryWatcher(properties, context);
+        synchronized (watchers)
+        {
+            watchers.put(pid, watcher);
+        }
         watcher.start();
     }