private final class RecursivelyDeleteDirectory extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
- file.toFile().delete();
- return FileVisitResult.CONTINUE;
+ return deletePath("file", file);
}
@Override
public FileVisitResult postVisitDirectory(Path file, IOException ex) throws IOException {
if (ex == null) {
- file.toFile().delete();
- return FileVisitResult.CONTINUE;
+ return deletePath("directory", file);
} else {
throw ex;
}
}
+
+ private FileVisitResult deletePath(String type, Path file) {
+ try {
+ Files.delete(file);
+ } catch (IOException e) {
+ logger.warn("failed to delete {} {}", type, file, e);
+ }
+ return FileVisitResult.CONTINUE;
+ }
}
/* ============================================================ */
* This method always succeeds, unless the lock is already unavailable.
*/
@Override
- public synchronized boolean free() {
- if (isUnavailable()) {
- return false;
- }
+ public boolean free() {
+ synchronized (this) {
+ if (isUnavailable()) {
+ return false;
+ }
- logger.info("releasing lock: {}", this);
- setState(LockState.UNAVAILABLE);
+ logger.info("releasing lock: {}", this);
+ setState(LockState.UNAVAILABLE);
+ }
return true;
}
* @return {@code true} if the lock can be freed, {@code false} if the lock is
* unavailable
*/
- protected boolean freeAllowed() {
+ protected synchronized boolean freeAllowed() {
// do a quick check of the state
if (isUnavailable()) {
return false;
* @return {@code true} if the lock can be extended, {@code false} if the lock is
* unavailable
*/
- protected boolean extendAllowed(int holdSec, LockCallback callback) {
+ protected synchronized boolean extendAllowed(int holdSec, LockCallback callback) {
if (holdSec < 0) {
throw new IllegalArgumentException("holdSec is negative");
}