FELIX-1790: fileinstall tmp dir needs to be different for each watched directory

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@828204 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 6e0f27c..fa31f3a 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
@@ -28,6 +28,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Dictionary;
@@ -87,6 +88,10 @@
     public final static String START_NEW_BUNDLES = "felix.fileinstall.bundles.new.start";
     public final static String NO_INITIAL_DELAY = "felix.fileinstall.noInitialDelay";
 
+    static final SecureRandom random = new SecureRandom();
+
+    static final File javaIoTmpdir = new File(System.getProperty("java.io.tmpdir"));
+
     Dictionary properties;
     File watchedDirectory;
     File tmpDir;
@@ -120,7 +125,8 @@
         originatingFileName = (String) properties.get(FILENAME);
         watchedDirectory = getFile(properties, DIR, new File("./load"));
         prepareDir(watchedDirectory);
-        tmpDir = getFile(properties, TMPDIR, new File(System.getProperty("java.io.tmpdir"), "fileinstall"));
+        tmpDir = getFile(properties, TMPDIR, null);
+        prepareTempDir();
         startBundles = getBoolean(properties, START_NEW_BUNDLES, true);  // by default, we start bundles.
         filter = (String) properties.get(FILTER);
         noInitialDelay = getBoolean(properties, NO_INITIAL_DELAY, false);
@@ -283,7 +289,7 @@
                 // Jar up the directory if needed
                 if (file.isDirectory())
                 {
-                    prepareDir(tmpDir);
+                    prepareTempDir();
                     try
                     {
                         jar = new File(tmpDir, file.getName() + ".jar");
@@ -410,7 +416,7 @@
     {
         if (artifact.getListener() instanceof ArtifactTransformer)
         {
-            prepareDir(tmpDir);
+            prepareTempDir();
             try
             {
                 File transformed = ((ArtifactTransformer) artifact.getListener()).transform(artifact.getJaredDirectory(), tmpDir);
@@ -467,6 +473,28 @@
         }
     }
 
+
+    private void prepareTempDir()
+    {
+        if (tmpDir == null)
+        {
+            for (;;)
+            {
+                File f = new File(javaIoTmpdir, "fileinstall-" + Long.toString(random.nextLong()));
+                if (!f.exists() && f.mkdirs())
+                {
+                    tmpDir = f;
+                    System.err.println("Created temp dir at " + tmpDir);
+                    break;
+                }
+            }
+        }
+        else
+        {
+            prepareDir(tmpDir);
+        }
+    }
+
     /**
      * Create the watched directory, if not existing.
      * Throws a runtime exception if the directory cannot be created,
diff --git a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/DirectoryWatcherTest.java b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/DirectoryWatcherTest.java
index 68c4953..a5f632e 100644
--- a/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/DirectoryWatcherTest.java
+++ b/fileinstall/src/test/java/org/apache/felix/fileinstall/internal/DirectoryWatcherTest.java
@@ -183,8 +183,8 @@
             "src" + File.separatorChar + "test" + File.separatorChar + "resources" ) );
         assertEquals( "Default POLL parameter correctly read", 2000l, dw.poll );
         assertEquals( "Default DEBUG parameter correctly read", -1l, dw.debug );
-        assertTrue( "Default TMPDIR parameter correctly read", dw.tmpDir.getAbsolutePath().equals(
-                new File(System.getProperty("java.io.tmpdir"), "fileinstall").getAbsolutePath()) );
+        assertTrue( "Default TMPDIR parameter correctly read", dw.tmpDir.getAbsolutePath().startsWith(
+                new File(System.getProperty("java.io.tmpdir")).getAbsolutePath()) );
         assertEquals( "Default START_NEW_BUNDLES parameter correctly read", true, dw.startBundles );
         assertEquals( "Default FILTER parameter correctly read", null, dw.filter );
     }