jdk/test/tools/launcher/TestHelper.java
changeset 14763 c16fa130fa23
parent 14518 f4b1adde53b3
child 20201 50cc2d25a60b
--- a/jdk/test/tools/launcher/TestHelper.java	Thu Dec 06 21:55:55 2012 -0800
+++ b/jdk/test/tools/launcher/TestHelper.java	Sun Dec 09 07:43:12 2012 -0800
@@ -358,6 +358,51 @@
         Files.copy(src.toPath(), dst.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
     }
 
+    /**
+     * Attempt to create a file at the given location. If an IOException
+     * occurs then back off for a moment and try again. When a number of
+     * attempts fail, give up and throw an exception.
+     */
+    void createAFile(File aFile, List<String> contents) throws IOException {
+        IOException cause = null;
+        for (int attempts = 0; attempts < 10; attempts++) {
+            try {
+                Files.write(aFile.getAbsoluteFile().toPath(), contents,
+                    Charset.defaultCharset(), CREATE, TRUNCATE_EXISTING, WRITE);
+                if (cause != null) {
+                    /*
+                     * report attempts and errors that were encountered
+                     * for diagnostic purposes
+                     */
+                    System.err.println("Created batch file " +
+                                        aFile + " in " + (attempts + 1) +
+                                        " attempts");
+                    System.err.println("Errors encountered: " + cause);
+                    cause.printStackTrace();
+                }
+                return;
+            } catch (IOException ioe) {
+                if (cause != null) {
+                    // chain the exceptions so they all get reported for diagnostics
+                    cause.addSuppressed(ioe);
+                } else {
+                    cause = ioe;
+                }
+            }
+
+            try {
+                Thread.sleep(500);
+            } catch (InterruptedException ie) {
+                if (cause != null) {
+                    // cause should alway be non-null here
+                    ie.addSuppressed(cause);
+                }
+                throw new RuntimeException("Interrupted while creating batch file", ie);
+            }
+        }
+        throw new RuntimeException("Unable to create batch file", cause);
+    }
+
     static void createFile(File outFile, List<String> content) throws IOException {
         Files.write(outFile.getAbsoluteFile().toPath(), content,
                 Charset.defaultCharset(), CREATE_NEW);