Java Code Examples for java.util.logging.FileHandler#close()

The following examples show how to use java.util.logging.FileHandler#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileHandlerMaxLocksTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String maxLocksSet = System.getProperty(MX_LCK_SYS_PROPERTY);
    File loggerDir = createLoggerDir();
    List<FileHandler> fileHandlers = new ArrayList<>();
    try {
        // 200 raises the default limit of 100, we try 102 times
        for (int i = 0; i < 102; i++) {
            fileHandlers.add(new FileHandler(loggerDir.getPath()
                    + File.separator + "test_%u.log"));
        }
    } catch (IOException ie) {
        if (maxLocksSet.equals("200ab")
                && ie.getMessage().contains("get lock for")) {
            // Ignore: Expected exception while passing bad value- 200ab
        } else {
            throw new RuntimeException("Test Failed: " + ie.getMessage());
        }
    } finally {
        for (FileHandler fh : fileHandlers) {
            fh.close();
        }
        FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
    }
}
 
Example 2
Source File: CheckZombieLockTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 3
Source File: FileHandlerMaxLocksTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String maxLocksSet = System.getProperty(MX_LCK_SYS_PROPERTY);
    File loggerDir = createLoggerDir();
    List<FileHandler> fileHandlers = new ArrayList<>();
    try {
        // 200 raises the default limit of 100, we try 102 times
        for (int i = 0; i < 102; i++) {
            fileHandlers.add(new FileHandler(loggerDir.getPath()
                    + File.separator + "test_%u.log"));
        }
    } catch (IOException ie) {
        if (maxLocksSet.equals("200ab")
                && ie.getMessage().contains("get lock for")) {
            // Ignore: Expected exception while passing bad value- 200ab
        } else {
            throw new RuntimeException("Test Failed: " + ie.getMessage());
        }
    } finally {
        for (FileHandler fh : fileHandlers) {
            fh.close();
        }
        FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
    }
}
 
Example 4
Source File: CheckZombieLockTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 5
Source File: CheckZombieLockTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 6
Source File: CheckZombieLockTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 7
Source File: CheckZombieLockTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 8
Source File: CheckZombieLockTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 9
Source File: FileHandlerMaxLocksTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    String maxLocksSet = System.getProperty(MX_LCK_SYS_PROPERTY);
    File loggerDir = createLoggerDir();
    List<FileHandler> fileHandlers = new ArrayList<>();
    try {
        // 200 raises the default limit of 100, we try 102 times
        for (int i = 0; i < 102; i++) {
            fileHandlers.add(new FileHandler(loggerDir.getPath()
                    + File.separator + "test_%u.log"));
        }
    } catch (IOException ie) {
        if (maxLocksSet.equals("200ab")
                && ie.getMessage().contains("get lock for")) {
            // Ignore: Expected exception while passing bad value- 200ab
        } else {
            throw new RuntimeException("Test Failed: " + ie.getMessage());
        }
    } finally {
        for (FileHandler fh : fileHandlers) {
            fh.close();
        }
        FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
    }
}
 
Example 10
Source File: CheckZombieLockTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testFileHandlerClose(File writableDir) throws IOException {
    File fakeLock = new File(writableDir, "log.log.lck");
    if (!createFile(fakeLock, false)) {
        throw new IOException("Can't create fake lock file: " + fakeLock);
    }
    try {
        List<File> before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        FileHandler handler = createFileHandler(writableDir);
        System.out.println("handler created: " + handler);
        List<File> after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        handler.close();
        System.out.println("handler closed: " + handler);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        afterClose.removeAll(before);
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }
    } finally {
        if (fakeLock.canRead()) delete(fakeLock);
    }
    List<File> finalLocks = listLocks(writableDir, false);
    System.out.println("After cleanup: " + finalLocks.size() + " locks found");
}
 
Example 11
Source File: OldFileHandlerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testPublish() throws Exception {
    LogRecord[] r = new LogRecord[] { new LogRecord(Level.CONFIG, "msg__"),
            new LogRecord(Level.WARNING, "message"),
            new LogRecord(Level.INFO, "message for"),
            new LogRecord(Level.FINE, "message for test") };
    for (int i = 0; i < r.length; i++) {
        handler = new FileHandler("%t/log/stringPublish");
        handler.publish(r[i]);
        handler.close();
        assertFileContent(TEMPPATH + SEP + "log", "stringPublish",
                new LogRecord[] { r[i] }, handler.getFormatter());
    }
}
 
Example 12
Source File: FileHandlerPath.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void test(String name, Properties props) throws Exception {
    System.out.println("Testing: " + name);
    String file = props.getProperty("test.file.name");
    // create the lock files first - in order to take the path that
    // used to trigger the NPE
    Files.createFile(Paths.get(file + ".lck"));
    Files.createFile(Paths.get(file + ".1.lck"));
    final FileHandler f1 = new FileHandler();
    final FileHandler f2 = new FileHandler();
    f1.close();
    f2.close();
    System.out.println("Success for " + name);
}
 
Example 13
Source File: FileHandlerPath.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void test(String name, Properties props) throws Exception {
    System.out.println("Testing: " + name);
    String file = props.getProperty("test.file.name");
    // create the lock files first - in order to take the path that
    // used to trigger the NPE
    Files.createFile(Paths.get(file + ".lck"));
    Files.createFile(Paths.get(file + ".1.lck"));
    final FileHandler f1 = new FileHandler();
    final FileHandler f2 = new FileHandler();
    f1.close();
    f2.close();
    System.out.println("Success for " + name);
}
 
Example 14
Source File: FileHandlerPath.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void test(String name, Properties props) throws Exception {
    System.out.println("Testing: " + name);
    String file = props.getProperty("test.file.name");
    // create the lock files first - in order to take the path that
    // used to trigger the NPE
    Files.createFile(Paths.get(file + ".lck"));
    Files.createFile(Paths.get(file + ".1.lck"));
    final FileHandler f1 = new FileHandler();
    final FileHandler f2 = new FileHandler();
    f1.close();
    f2.close();
    System.out.println("Success for " + name);
}
 
Example 15
Source File: EngineLoggerTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * the user uses the user defined log. All log should be outputted to the
 * user defined logger.
 * 
 * @throws Exception
 */
public void testUserLogger( ) throws Exception
{
	// init the root logger
	FileHandler fileHandle = new FileHandler( "./utest/logger.txt" );
	try
	{
		Logger logger = Logger.getAnonymousLogger( );
		logger.addHandler( fileHandle );
		logger.setLevel( Level.ALL );
		logger.setUseParentHandlers( false );
		try
		{

			// start a default logger
			LoggerSetting setting = EngineLogger.createSetting(logger, null, null, Level.FINE, 0, 0);

			// all the log should be output to the root logger
			log( );
			EngineLogger.setLogger( setting, null );
			log( );
			EngineLogger.removeSetting(setting);
		}
		finally
		{
			logger.removeHandler( fileHandle );
		}
	}
	finally
	{
		fileHandle.close( );
	}
	// test the log file content
	checkLogging( "./utest/logger.txt", 0, 1, 1 );
}
 
Example 16
Source File: NoVelocityLoggingTest.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
private void doTestDontLog(File log) throws IOException {
  // Set things up so that if Velocity is successfully logging then we will see its log output
  // in the temporary file we have created. This depends on Velocity falling back on JDK logging,
  // so this test won't do anything useful if its classpath contains Log4J or Commons Logging or
  // any of the other exotic logging systems that Velocity will pounce on if it sees them.
  FileHandler fileHandler = new FileHandler(log.getPath());
  fileHandler.setFormatter(new SimpleFormatter());
  Logger logger = Logger.getLogger(JdkLogChute.DEFAULT_LOG_NAME);
  logger.addHandler(fileHandler);
  logger.setLevel(Level.ALL);
  LogManager logManager = LogManager.getLogManager();
  logManager.addLogger(logger);

  // Now do a random compilation that implies using RetroFacebookProcessor.
  JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
      "foo.bar.Baz",
      "package foo.bar;",
      "",
      "import retrofacebook.RetroFacebook;",
      "",
      "@RetroFacebook",
      "public abstract class Baz {",
      "  public abstract int buh();",
      "",
      "  public static Baz create(int buh) {",
      "    return new RetroFacebook_Baz(buh);",
      "  }",
      "}");
  assert_().about(javaSource())
      .that(javaFileObject)
      .processedWith(new RetroFacebookProcessor())
      .compilesWithoutError();

  // The log file should be empty.
  fileHandler.close();
  assertEquals("", Files.toString(log, StandardCharsets.UTF_8));
}
 
Example 17
Source File: EngineLoggerTest.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public void run( )
{
	try
	{
		FileHandler handler = new FileHandler( logFile );
		try
		{
			Logger logger = Logger.getAnonymousLogger( );
			logger.addHandler( handler );
			logger.setLevel( logLevel );
			logger.setUseParentHandlers( false );
			EngineLogger.setThreadLogger( logger );
			try
			{
				log( );
			}
			finally
			{
				EngineLogger.setThreadLogger( null );
			}
		}
		finally
		{
			handler.close( );
			count.decrementAndGet( );
			synchronized ( count )
			{
				count.notify( );
			}
		}
	}
	catch ( IOException ex )
	{
		ex.printStackTrace( );
	}
}
 
Example 18
Source File: CheckZombieLockTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static void testFileHandlerReuse(File writableDir) throws IOException {
    List<File> before = listLocks(writableDir, true);
    System.out.println("before: " + before.size() + " locks found");
    try {
        if (!before.isEmpty()) {
            throw new RuntimeException("Expected no lock file! Found: " + before);
        }
    } finally {
        before.stream().forEach(CheckZombieLockTest::delete);
    }

    FileHandler handler1 = createFileHandler(writableDir);
    System.out.println("handler created: " + handler1);
    List<File> after = listLocks(writableDir, true);
    System.out.println("after creating handler: " + after.size() + " locks found");
    if (after.size() != 1) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    final File lock = after.get(0);
    after.clear();
    handler1.close();
    after = listLocks(writableDir, true);
    System.out.println("after closing handler: " + after.size() + " locks found");
    if (!after.isEmpty()) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    if (!createFile(lock, false)) {
        throw new IOException("Can't create fake lock file: " + lock);
    }
    try {
        before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        if (before.size() != 1) {
            throw new RuntimeException("Unexpected number of lock files found: "
                    + before + " expected [" + lock + "].");
        }
        FileHandler handler2 = createFileHandler(writableDir);
        System.out.println("handler created: " + handler2);
        after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        after.removeAll(before);
        if (!after.isEmpty()) {
            throw new RuntimeException("Unexpected lock file found: " + after
                    + "\n\t" + lock + " should have been reused");
        }
        handler2.close();
        System.out.println("handler closed: " + handler2);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }

        if (supportsLocking) {
            FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()),
                StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
                StandardOpenOption.WRITE);
            try {
                if (fc.tryLock() != null) {
                    System.out.println("locked: " + lock);
                    handler2 = createFileHandler(writableDir);
                    System.out.println("handler created: " + handler2);
                    after = listLocks(writableDir, true);
                    System.out.println("after creating handler: " + after.size()
                            + " locks found");
                    after.removeAll(before);
                    if (after.size() != 1) {
                        throw new RuntimeException("Unexpected lock files found: " + after
                            + "\n\t" + lock + " should not have been reused");
                    }
                } else {
                    throw new RuntimeException("Failed to lock: " + lock);
                }
            } finally {
                delete(lock);
            }
        }
    } finally {
        List<File> finalLocks = listLocks(writableDir, false);
        System.out.println("end: " + finalLocks.size() + " locks found");
        delete(writableDir);
    }
}
 
Example 19
Source File: CheckZombieLockTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static void testFileHandlerReuse(File writableDir) throws IOException {
    List<File> before = listLocks(writableDir, true);
    System.out.println("before: " + before.size() + " locks found");
    try {
        if (!before.isEmpty()) {
            throw new RuntimeException("Expected no lock file! Found: " + before);
        }
    } finally {
        before.stream().forEach(CheckZombieLockTest::delete);
    }

    FileHandler handler1 = createFileHandler(writableDir);
    System.out.println("handler created: " + handler1);
    List<File> after = listLocks(writableDir, true);
    System.out.println("after creating handler: " + after.size() + " locks found");
    if (after.size() != 1) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    final File lock = after.get(0);
    after.clear();
    handler1.close();
    after = listLocks(writableDir, true);
    System.out.println("after closing handler: " + after.size() + " locks found");
    if (!after.isEmpty()) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    if (!createFile(lock, false)) {
        throw new IOException("Can't create fake lock file: " + lock);
    }
    try {
        before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        if (before.size() != 1) {
            throw new RuntimeException("Unexpected number of lock files found: "
                    + before + " expected [" + lock + "].");
        }
        FileHandler handler2 = createFileHandler(writableDir);
        System.out.println("handler created: " + handler2);
        after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        after.removeAll(before);
        if (!after.isEmpty()) {
            throw new RuntimeException("Unexpected lock file found: " + after
                    + "\n\t" + lock + " should have been reused");
        }
        handler2.close();
        System.out.println("handler closed: " + handler2);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }

        if (supportsLocking) {
            FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()),
                StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
                StandardOpenOption.WRITE);
            try {
                if (fc.tryLock() != null) {
                    System.out.println("locked: " + lock);
                    handler2 = createFileHandler(writableDir);
                    System.out.println("handler created: " + handler2);
                    after = listLocks(writableDir, true);
                    System.out.println("after creating handler: " + after.size()
                            + " locks found");
                    after.removeAll(before);
                    if (after.size() != 1) {
                        throw new RuntimeException("Unexpected lock files found: " + after
                            + "\n\t" + lock + " should not have been reused");
                    }
                } else {
                    throw new RuntimeException("Failed to lock: " + lock);
                }
            } finally {
                delete(lock);
            }
        }
    } finally {
        List<File> finalLocks = listLocks(writableDir, false);
        System.out.println("end: " + finalLocks.size() + " locks found");
        delete(writableDir);
    }
}
 
Example 20
Source File: CheckZombieLockTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static void testFileHandlerReuse(File writableDir) throws IOException {
    List<File> before = listLocks(writableDir, true);
    System.out.println("before: " + before.size() + " locks found");
    try {
        if (!before.isEmpty()) {
            throw new RuntimeException("Expected no lock file! Found: " + before);
        }
    } finally {
        before.stream().forEach(CheckZombieLockTest::delete);
    }

    FileHandler handler1 = createFileHandler(writableDir);
    System.out.println("handler created: " + handler1);
    List<File> after = listLocks(writableDir, true);
    System.out.println("after creating handler: " + after.size() + " locks found");
    if (after.size() != 1) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    final File lock = after.get(0);
    after.clear();
    handler1.close();
    after = listLocks(writableDir, true);
    System.out.println("after closing handler: " + after.size() + " locks found");
    if (!after.isEmpty()) {
        throw new RuntimeException("Unexpected number of lock files found for "
                + handler1 + ": " + after);
    }
    if (!createFile(lock, false)) {
        throw new IOException("Can't create fake lock file: " + lock);
    }
    try {
        before = listLocks(writableDir, true);
        System.out.println("before: " + before.size() + " locks found");
        if (before.size() != 1) {
            throw new RuntimeException("Unexpected number of lock files found: "
                    + before + " expected [" + lock + "].");
        }
        FileHandler handler2 = createFileHandler(writableDir);
        System.out.println("handler created: " + handler2);
        after = listLocks(writableDir, true);
        System.out.println("after creating handler: " + after.size() + " locks found");
        after.removeAll(before);
        if (!after.isEmpty()) {
            throw new RuntimeException("Unexpected lock file found: " + after
                    + "\n\t" + lock + " should have been reused");
        }
        handler2.close();
        System.out.println("handler closed: " + handler2);
        List<File> afterClose = listLocks(writableDir, true);
        System.out.println("after closing handler: " + afterClose.size() + " locks found");
        if (!afterClose.isEmpty()) {
            throw new RuntimeException("Zombie lock file detected: " + afterClose);
        }

        if (supportsLocking) {
            FileChannel fc = FileChannel.open(Paths.get(lock.getAbsolutePath()),
                StandardOpenOption.CREATE_NEW, StandardOpenOption.APPEND,
                StandardOpenOption.WRITE);
            try {
                if (fc.tryLock() != null) {
                    System.out.println("locked: " + lock);
                    handler2 = createFileHandler(writableDir);
                    System.out.println("handler created: " + handler2);
                    after = listLocks(writableDir, true);
                    System.out.println("after creating handler: " + after.size()
                            + " locks found");
                    after.removeAll(before);
                    if (after.size() != 1) {
                        throw new RuntimeException("Unexpected lock files found: " + after
                            + "\n\t" + lock + " should not have been reused");
                    }
                } else {
                    throw new RuntimeException("Failed to lock: " + lock);
                }
            } finally {
                delete(lock);
            }
        }
    } finally {
        List<File> finalLocks = listLocks(writableDir, false);
        System.out.println("end: " + finalLocks.size() + " locks found");
        delete(writableDir);
    }
}