1
1
/*
2
- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
50
50
import java .util .logging .FileHandler ;
51
51
import java .util .logging .Level ;
52
52
import java .util .logging .LogRecord ;
53
+ import static java .nio .file .StandardOpenOption .*;
54
+
53
55
public class CheckZombieLockTest {
54
56
55
57
private static final String WRITABLE_DIR = "writable-lockfile-dir" ;
@@ -241,10 +243,9 @@ private static void testFileHandlerReuse(File writableDir) throws IOException {
241
243
}
242
244
243
245
if (supportsLocking ) {
244
- FileChannel fc = FileChannel .open (Paths .get (lock .getAbsolutePath ()),
245
- StandardOpenOption .CREATE_NEW , StandardOpenOption .APPEND ,
246
- StandardOpenOption .WRITE );
247
- try {
246
+ handler2 = null ;
247
+ try (FileChannel fc = FileChannel .open (lock .toPath (), CREATE_NEW , APPEND , WRITE )) {
248
+
248
249
if (fc .tryLock () != null ) {
249
250
System .out .println ("locked: " + lock );
250
251
handler2 = createFileHandler (writableDir );
@@ -261,6 +262,7 @@ private static void testFileHandlerReuse(File writableDir) throws IOException {
261
262
throw new RuntimeException ("Failed to lock: " + lock );
262
263
}
263
264
} finally {
265
+ if (handler2 != null ) handler2 .close ();
264
266
delete (lock );
265
267
}
266
268
}
@@ -320,17 +322,12 @@ private static File setup() throws RuntimeException {
320
322
321
323
// try to determine whether file locking is supported
322
324
final String uniqueFileName = UUID .randomUUID ().toString ()+".lck" ;
323
- try {
324
- FileChannel fc = FileChannel .open (Paths .get (writableDir .getAbsolutePath (),
325
- uniqueFileName ),
326
- StandardOpenOption .CREATE_NEW , StandardOpenOption .APPEND ,
327
- StandardOpenOption .DELETE_ON_CLOSE );
325
+ try (FileChannel fc = FileChannel .open (Paths .get (writableDir .getAbsolutePath (),
326
+ uniqueFileName ), CREATE_NEW , APPEND , DELETE_ON_CLOSE )) {
328
327
try {
329
328
fc .tryLock ();
330
- } catch (IOException x ) {
329
+ } catch (IOException x ) {
331
330
supportsLocking = false ;
332
- } finally {
333
- fc .close ();
334
331
}
335
332
} catch (IOException t ) {
336
333
// should not happen
1 commit comments
openjdk-notifier[bot] commentedon Jan 17, 2023
Review
Issues