All Downloads are FREE. Search and download functionalities are using the official Maven repository.

htsjdk.samtools.util.nio.DeleteOnExitPathHook Maven / Gradle / Ivy

There is a newer version: 4.1.3
Show newest version
package htsjdk.samtools.util.nio;

import htsjdk.samtools.util.Log;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;

/**
 * Class to hold a set of {@link Path} to be delete on the JVM exit through a shutdown hook.
 *
 * 

This class is a modification of {@link java.io.DeleteOnExitHook} to handle {@link Path} * instead of {@link java.io.File}. * * @author Daniel Gomez-Sanchez (magicDGS) */ public class DeleteOnExitPathHook { private static final Log LOG = Log.getInstance(DeleteOnExitPathHook.class); private static LinkedHashSet paths = new LinkedHashSet<>(); static { Runtime.getRuntime().addShutdownHook(new Thread(DeleteOnExitPathHook::runHooks)); } private DeleteOnExitPathHook() {} /** * Adds a {@link Path} for deletion on JVM exit. * * @param path path to be deleted. * * @throws IllegalStateException if the shutdown hook is in progress. */ public static synchronized void add(Path path) { if(paths == null) { // DeleteOnExitHook is running. Too late to add a file throw new IllegalStateException("Shutdown in progress"); } paths.add(path); } static void runHooks() { LinkedHashSet thePaths; synchronized (DeleteOnExitPathHook.class) { thePaths = paths; paths = null; } ArrayList toBeDeleted = new ArrayList<>(thePaths); // reverse the list to maintain previous jdk deletion order. // Last in first deleted. Collections.reverse(toBeDeleted); for (Path path : toBeDeleted) { try { Files.delete(path); } catch (Exception e) { LOG.debug(e, "Failed to delete file: ", path, " during shutdown because we encountered an exception."); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy