-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DeleteOnExitPathHook to register java.nio.Path for deletion (#1037)
* Add DeleteOnExitPathHook to register java.nio.Path for deletion, this closely mirrors the equivalent java built in `DeleteOnExitHook` but uses paths instead of files.
- Loading branch information
1 parent
8b8d961
commit 5c8de55
Showing
3 changed files
with
68 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/htsjdk/samtools/util/nio/DeleteOnExitPathHook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package htsjdk.samtools.util.nio; | ||
|
||
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. | ||
* | ||
* <p>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 LinkedHashSet<Path> 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<Path> thePaths; | ||
|
||
synchronized (DeleteOnExitPathHook.class) { | ||
thePaths = paths; | ||
paths = null; | ||
} | ||
|
||
ArrayList<Path> 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 (IOException | SecurityException e) { | ||
// do nothing if cannot be deleted, because it is a shutdown hook | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters