11/*
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.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
5050import java .util .logging .FileHandler ;
5151import java .util .logging .Level ;
5252import java .util .logging .LogRecord ;
53+ import static java .nio .file .StandardOpenOption .*;
54+
5355public class CheckZombieLockTest {
5456
5557 private static final String WRITABLE_DIR = "writable-lockfile-dir" ;
@@ -241,10 +243,9 @@ private static void testFileHandlerReuse(File writableDir) throws IOException {
241243 }
242244
243245 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+
248249 if (fc .tryLock () != null ) {
249250 System .out .println ("locked: " + lock );
250251 handler2 = createFileHandler (writableDir );
@@ -261,6 +262,7 @@ private static void testFileHandlerReuse(File writableDir) throws IOException {
261262 throw new RuntimeException ("Failed to lock: " + lock );
262263 }
263264 } finally {
265+ if (handler2 != null ) handler2 .close ();
264266 delete (lock );
265267 }
266268 }
@@ -320,17 +322,12 @@ private static File setup() throws RuntimeException {
320322
321323 // try to determine whether file locking is supported
322324 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 )) {
328327 try {
329328 fc .tryLock ();
330- } catch (IOException x ) {
329+ } catch (IOException x ) {
331330 supportsLocking = false ;
332- } finally {
333- fc .close ();
334331 }
335332 } catch (IOException t ) {
336333 // should not happen
0 commit comments