Skip to content

Commit d268e3d

Browse files
committed
Restored two helper classes from legacy module
1 parent 30a756a commit d268e3d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.lightbody.bmp.exception;
2+
3+
/**
4+
* A wrapper for exceptions coming from Jetty methods that throw Exception,
5+
* rather than a useful Exception subclass.
6+
*/
7+
public class JettyException extends RuntimeException {
8+
private static final long serialVersionUID = 8125833642102189196L;
9+
10+
public JettyException() {
11+
}
12+
13+
public JettyException(String message) {
14+
super(message);
15+
}
16+
17+
public JettyException(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
public JettyException(String message, Throwable cause) {
22+
super(message, cause);
23+
}
24+
25+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package net.lightbody.bmp.util;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.io.IOException;
7+
import java.nio.file.FileVisitResult;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.SimpleFileVisitor;
11+
import java.nio.file.attribute.BasicFileAttributes;
12+
13+
/**
14+
* A Runnable that deletes the specified directory. Useful as a shutdown hook.
15+
*/
16+
public class DeleteDirectoryTask implements Runnable {
17+
// the static final logger is in this static inner class to allow the logger initialization code to use DeleteDirectoryTask
18+
// without prematurely initializing the logger when loading this class. since the 'log' field is in a static inner class, it will
19+
// only be initialized when it is actually used, instead of when the classloader loads the DeleteDirectoryTask class.
20+
private static class LogHolder {
21+
private static final Logger log = LoggerFactory.getLogger(DeleteDirectoryTask.class);
22+
}
23+
24+
private final Path directory;
25+
26+
public DeleteDirectoryTask(Path directory) {
27+
this.directory = directory;
28+
}
29+
30+
@Override
31+
public void run() {
32+
try {
33+
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
34+
@Override
35+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
36+
try {
37+
Files.delete(file);
38+
} catch (IOException e) {
39+
LogHolder.log.warn("Unable to delete file or directory", e);
40+
}
41+
42+
return FileVisitResult.CONTINUE;
43+
}
44+
45+
@Override
46+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
47+
try {
48+
Files.delete(dir);
49+
} catch (IOException e) {
50+
LogHolder.log.warn("Unable to delete file or directory", e);
51+
}
52+
53+
return FileVisitResult.CONTINUE;
54+
}
55+
});
56+
} catch (IOException e) {
57+
LogHolder.log.warn("Unable to delete file or directory", e);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)