jdk/test/java/util/logging/CheckZombieLockTest.java
author dfuchs
Mon, 07 Jul 2014 15:31:07 +0200
changeset 25389 6f994d9e1b57
child 25998 3efc2a6b082a
permissions -rw-r--r--
8048020: Regression on java.util.logging.FileHandler Summary: In some circumstances j.u.l.FileHandler can leave zombie lock files on the file system. The fix lets FileHandler reuse such zombie lock files when it sees them - as it used to do in version 1.7 of the JDK. Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25389
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     1
/*
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     4
 *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     8
 *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    14
 *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    18
 *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    21
 * questions.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    22
 */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    23
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    24
/*
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    25
 * @test
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    26
 * @bug     8048020
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    27
 * @author  Daniel Fuchs
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    28
 * @summary Regression on java.util.logging.FileHandler.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    29
 *     The fix is to avoid filling up the file system with zombie lock files.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    30
 *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    31
 * @run  main/othervm CheckZombieLockTest WRITABLE CLOSE CLEANUP
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    32
 * @run  main/othervm CheckZombieLockTest CLEANUP
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    33
 * @run  main/othervm CheckZombieLockTest WRITABLE
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    34
 * @run  main/othervm CheckZombieLockTest CREATE_FIRST
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    35
 * @run  main/othervm CheckZombieLockTest CREATE_NEXT
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    36
 * @run  main/othervm CheckZombieLockTest CREATE_NEXT
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    37
 * @run  main/othervm CheckZombieLockTest CLEANUP
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    38
 * @run  main/othervm CheckZombieLockTest REUSE
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    39
 * @run  main/othervm CheckZombieLockTest CLEANUP
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    40
 */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    41
import java.io.File;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    42
import java.io.IOException;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    43
import java.nio.channels.FileChannel;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    44
import java.nio.file.Paths;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    45
import java.nio.file.StandardOpenOption;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    46
import java.util.ArrayList;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    47
import java.util.List;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    48
import java.util.UUID;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    49
import java.util.logging.FileHandler;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    50
import java.util.logging.Level;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    51
import java.util.logging.LogRecord;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    52
public class CheckZombieLockTest {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    53
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    54
    private static final String WRITABLE_DIR = "writable-dir";
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    55
    private static volatile boolean supportsLocking = true;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    56
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    57
    static enum TestCase {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    58
        WRITABLE,  // just verifies that we can create a file in our 'writable-dir'
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    59
        CLOSE, // checks that closing a FileHandler removes its lock file
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    60
        CREATE_FIRST, // verifies that 'writable-dir' contains no lock, then creates a first FileHandler.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    61
        CREATE_NEXT, // verifies that 'writable-dir' contains a single lock, then creates the next FileHandler
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    62
        REUSE, // verifies that zombie lock files can be reused
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    63
        CLEANUP // removes "writable-dir"
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    64
    };
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    65
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    66
    public static void main(String... args) throws IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    67
        // we'll base all file creation attempts on the system temp directory,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    68
        // %t
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    69
        File writableDir = setup();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    70
        System.out.println("Writable dir is: "+writableDir.getAbsolutePath());
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    71
        // we now have one writable directory to work with:
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    72
        //    writableDir
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    73
        if (args == null || args.length == 0) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    74
            args = new String[] { "WRITABLE", "CLOSE", "CLEANUP" };
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    75
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    76
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    77
            runTests(writableDir, args);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    78
        } catch (RuntimeException | IOException | Error x) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    79
            // some error occured: cleanup
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    80
            delete(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    81
            throw x;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    82
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    83
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    84
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    85
    /**
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    86
     * @param writableDir in which log and lock file are created
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    87
     * @throws SecurityException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    88
     * @throws RuntimeException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    89
     * @throws IOException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    90
     */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    91
    private static void runTests(File writableDir, String... args) throws SecurityException,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    92
            RuntimeException, IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    93
        for (String arg : args) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    94
            switch(TestCase.valueOf(arg)) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    95
                // Test 1: makes sure we can create FileHandler in writable directory
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    96
                case WRITABLE: checkWritable(writableDir); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    97
                // Test 2: verifies that FileHandler.close() cleans up its lock file
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    98
                case CLOSE: testFileHandlerClose(writableDir); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
    99
                // Test 3: creates the first file handler
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   100
                case CREATE_FIRST: testFileHandlerCreate(writableDir, true); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   101
                // Test 4, 5, ... creates the next file handler
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   102
                case CREATE_NEXT: testFileHandlerCreate(writableDir, false); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   103
                // Checks that zombie lock files are reused appropriatly
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   104
                case REUSE: testFileHandlerReuse(writableDir); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   105
                // Removes the writableDir
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   106
                case CLEANUP: delete(writableDir); break;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   107
                default: throw new RuntimeException("No such test case: "+arg);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   108
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   109
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   110
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   111
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   112
    /**
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   113
     * @param writableDir in which log and lock file are created
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   114
     * @throws SecurityException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   115
     * @throws RuntimeException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   116
     * @throws IOException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   117
     */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   118
    private static void checkWritable(File writableDir) throws SecurityException,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   119
            RuntimeException, IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   120
        // Test 1: make sure we can create/delete files in the writable dir.
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   121
        final File file = new File(writableDir, "test.txt");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   122
        if (!createFile(file, false)) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   123
            throw new IOException("Can't create "+file+"\n\tUnable to run test");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   124
        } else {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   125
            delete(file);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   126
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   127
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   128
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   129
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   130
    private static FileHandler createFileHandler(File writableDir) throws SecurityException,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   131
            RuntimeException, IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   132
        // Test 1: make sure we can create FileHandler in writable directory
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   133
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   134
            FileHandler handler = new FileHandler("%t/" + WRITABLE_DIR + "/log.log");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   135
            handler.publish(new LogRecord(Level.INFO, handler.toString()));
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   136
            handler.flush();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   137
            return handler;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   138
        } catch (IOException ex) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   139
            throw new RuntimeException("Test failed: should have been able"
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   140
                    + " to create FileHandler for " + "%t/" + WRITABLE_DIR
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   141
                    + "/log.log in writable directory.", ex);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   142
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   143
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   144
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   145
    private static List<File> listLocks(File writableDir, boolean print)
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   146
            throws IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   147
        List<File> locks = new ArrayList<>();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   148
        for (File f : writableDir.listFiles()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   149
            if (print) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   150
                System.out.println("Found file: " + f.getName());
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   151
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   152
            if (f.getName().endsWith(".lck")) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   153
                locks.add(f);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   154
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   155
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   156
        return locks;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   157
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   158
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   159
    private static void testFileHandlerClose(File writableDir) throws IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   160
        File fakeLock = new File(writableDir, "log.log.lck");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   161
        if (!createFile(fakeLock, false)) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   162
            throw new IOException("Can't create fake lock file: "+fakeLock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   163
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   164
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   165
            List<File> before = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   166
            System.out.println("before: " +before.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   167
            FileHandler handler = createFileHandler(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   168
            System.out.println("handler created: "+handler);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   169
            List<File> after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   170
            System.out.println("after creating handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   171
            handler.close();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   172
            System.out.println("handler closed: "+handler);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   173
            List<File> afterClose = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   174
            System.out.println("after closing handler: " + afterClose.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   175
            afterClose.removeAll(before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   176
            if (!afterClose.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   177
                throw new RuntimeException("Zombie lock file detected: "+ afterClose);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   178
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   179
        } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   180
            if (fakeLock.canRead()) delete(fakeLock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   181
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   182
        List<File> finalLocks = listLocks(writableDir, false);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   183
        System.out.println("After cleanup: " + finalLocks.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   184
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   185
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   186
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   187
    private static void testFileHandlerReuse(File writableDir) throws IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   188
        List<File> before = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   189
        System.out.println("before: " +before.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   190
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   191
            if (!before.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   192
                throw new RuntimeException("Expected no lock file! Found: "+before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   193
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   194
        } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   195
            before.stream().forEach(CheckZombieLockTest::delete);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   196
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   197
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   198
        FileHandler handler1 = createFileHandler(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   199
        System.out.println("handler created: "+handler1);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   200
        List<File> after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   201
        System.out.println("after creating handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   202
        if (after.size() != 1) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   203
            throw new RuntimeException("Unexpected number of lock files found for "+handler1+": "+after);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   204
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   205
        final File lock = after.get(0);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   206
        after.clear();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   207
        handler1.close();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   208
        after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   209
        System.out.println("after closing handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   210
        if (!after.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   211
            throw new RuntimeException("Unexpected number of lock files found for "+handler1+": "+after);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   212
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   213
        if (!createFile(lock, false)) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   214
            throw new IOException("Can't create fake lock file: "+lock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   215
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   216
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   217
            before = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   218
            System.out.println("before: " +before.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   219
            if (before.size() != 1) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   220
                throw new RuntimeException("Unexpected number of lock files found: "+before+" expected ["
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   221
                        +lock+"].");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   222
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   223
            FileHandler handler2 = createFileHandler(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   224
            System.out.println("handler created: "+handler2);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   225
            after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   226
            System.out.println("after creating handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   227
            after.removeAll(before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   228
            if (!after.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   229
                throw new RuntimeException("Unexpected lock file found: "+after
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   230
                        + "\n\t" + lock + " should have been reused");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   231
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   232
            handler2.close();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   233
            System.out.println("handler closed: "+handler2);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   234
            List<File> afterClose = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   235
            System.out.println("after closing handler: " + afterClose.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   236
            if (!afterClose.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   237
                throw new RuntimeException("Zombie lock file detected: "+ afterClose);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   238
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   239
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   240
            if (supportsLocking) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   241
                FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()),
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   242
                    StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   243
                    StandardOpenOption.WRITE);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   244
                try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   245
                    if (fc.tryLock() != null) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   246
                        System.out.println("locked: "+lock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   247
                        handler2 = createFileHandler(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   248
                        System.out.println("handler created: "+handler2);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   249
                        after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   250
                        System.out.println("after creating handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   251
                        after.removeAll(before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   252
                        if (after.size() != 1) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   253
                            throw new RuntimeException("Unexpected lock files found: "+after
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   254
                                + "\n\t" + lock + " should not have been reused");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   255
                        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   256
                    } else {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   257
                        throw new RuntimeException("Failed to lock: "+lock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   258
                    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   259
                } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   260
                    delete(lock);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   261
                }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   262
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   263
        } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   264
            List<File> finalLocks = listLocks(writableDir, false);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   265
            System.out.println("end: " + finalLocks.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   266
            delete(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   267
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   268
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   269
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   270
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   271
    private static void testFileHandlerCreate(File writableDir, boolean first)
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   272
            throws IOException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   273
        List<File> before = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   274
        System.out.println("before: " +before.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   275
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   276
            if (first && !before.isEmpty()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   277
                throw new RuntimeException("Expected no lock file! Found: "+before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   278
            } else if (!first && before.size() != 1) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   279
                throw new RuntimeException("Expected a single lock file! Found: "+before);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   280
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   281
        } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   282
            before.stream().forEach(CheckZombieLockTest::delete);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   283
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   284
        FileHandler handler = createFileHandler(writableDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   285
        System.out.println("handler created: "+handler);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   286
        List<File> after = listLocks(writableDir, true);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   287
        System.out.println("after creating handler: " + after.size() + " locks found");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   288
        if (after.size() != 1) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   289
            throw new RuntimeException("Unexpected number of lock files found for "+handler+": "+after);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   290
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   291
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   292
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   293
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   294
    /**
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   295
     * Setup all the files and directories needed for the tests
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   296
     *
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   297
     * @return writable directory created that needs to be deleted when done
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   298
     * @throws RuntimeException
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   299
     */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   300
    private static File setup() throws RuntimeException {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   301
        // First do some setup in the temporary directory (using same logic as
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   302
        // FileHandler for %t pattern)
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   303
        String tmpDir = System.getProperty("java.io.tmpdir"); // i.e. %t
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   304
        if (tmpDir == null) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   305
            tmpDir = System.getProperty("user.home");
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   306
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   307
        File tmpOrHomeDir = new File(tmpDir);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   308
        // Create a writable directory here (%t/writable-dir)
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   309
        File writableDir = new File(tmpOrHomeDir, WRITABLE_DIR);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   310
        if (!createFile(writableDir, true)) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   311
            throw new RuntimeException("Test setup failed: unable to create"
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   312
                    + " writable working directory "
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   313
                    + writableDir.getAbsolutePath() );
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   314
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   315
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   316
        // try to determine whether file locking is supported
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   317
        try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   318
            FileChannel fc = FileChannel.open(Paths.get(writableDir.getAbsolutePath(),
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   319
                    UUID.randomUUID().toString()+".lck"),
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   320
                    StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   321
                    StandardOpenOption.DELETE_ON_CLOSE);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   322
            try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   323
                fc.tryLock();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   324
            } catch(IOException x) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   325
                supportsLocking = false;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   326
            } finally {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   327
                fc.close();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   328
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   329
        } catch(Throwable t) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   330
            // should not happen
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   331
            t.printStackTrace();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   332
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   333
        return writableDir;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   334
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   335
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   336
    /**
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   337
     * @param newFile
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   338
     * @return true if file already exists or creation succeeded
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   339
     */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   340
    private static boolean createFile(File newFile, boolean makeDirectory) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   341
        if (newFile.exists()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   342
            return true;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   343
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   344
        if (makeDirectory) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   345
            return newFile.mkdir();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   346
        } else {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   347
            try {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   348
                return newFile.createNewFile();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   349
            } catch (IOException ioex) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   350
                ioex.printStackTrace();
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   351
                return false;
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   352
            }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   353
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   354
    }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   355
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   356
    /*
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   357
     * Recursively delete all files starting at specified file
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   358
     */
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   359
    private static void delete(File f) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   360
        if (f != null && f.isDirectory()) {
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   361
            for (File c : f.listFiles())
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   362
                delete(c);
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   363
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   364
        if (!f.delete())
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   365
            System.err.println(
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   366
                    "WARNING: unable to delete/cleanup writable test directory: "
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   367
                    + f );
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   368
        }
6f994d9e1b57 8048020: Regression on java.util.logging.FileHandler
dfuchs
parents:
diff changeset
   369
}