Skip to content

Commit 7bf8dff

Browse files
authored
Clean up xsd files in temp dirs after exit (#99)
Files in a directory must be deleted individually. Files.deleteOnExit() does not work for directories that are not empty.
1 parent 78d22b0 commit 7bf8dff

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

third_party/wff/specification/validator/src/main/java/com/samsung/watchface/ResourceManager.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,55 @@
2424
import java.io.OutputStream;
2525
import java.net.URL;
2626
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.util.Comparator;
2729
import java.util.Objects;
30+
import java.util.stream.Stream;
2831

2932
class ResourceManager {
3033
private static final String RESOURCE_ZIP_XSD_DOCS = "/docs.zip";
31-
private static final int BUFFER_SIZE = 4096;
3234

3335
private final File xsdTempDirectory;
3436

3537
ResourceManager() {
36-
// load resources
37-
String zipFilePath = getResourceAsFile(RESOURCE_ZIP_XSD_DOCS).toString();
3838
xsdTempDirectory = createTempDirectory();
39-
UnzipUtility.unzip(zipFilePath, xsdTempDirectory.toString());
39+
UnzipUtility.unzip(
40+
this.getClass().getResourceAsStream(RESOURCE_ZIP_XSD_DOCS),
41+
xsdTempDirectory.toString()
42+
);
4043
}
4144

4245
File getXsdFile(String version) {
4346
return new File(xsdTempDirectory + File.separator +
4447
version + File.separator + "watchface.xsd");
4548
}
4649

47-
private File getResourceAsFile(String resource) {
48-
File file;
49-
URL res = getClass().getResource(resource);
50-
if (res == null) {
51-
throw new RuntimeException("No resource File : " + resource);
52-
}
53-
if (res.getProtocol().equals("jar")) {
54-
try {
55-
file = File.createTempFile("dwf_temp", "tmp");
56-
} catch (IOException e) {
57-
throw new RuntimeException(e);
58-
}
59-
file.deleteOnExit();
60-
61-
try (InputStream input = getClass().getResourceAsStream(resource);
62-
OutputStream output = Files.newOutputStream(file.toPath())) {
63-
byte[] bytes = new byte[BUFFER_SIZE];
64-
int read;
65-
while ((read = Objects.requireNonNull(input).read(bytes)) != -1) {
66-
output.write(bytes, 0, read);
67-
}
68-
} catch (Exception e) {
69-
throw new RuntimeException(e.getMessage());
70-
}
71-
} else { // for IDE
72-
file = new File(res.getFile());
73-
}
74-
75-
if (!file.exists()) {
76-
throw new RuntimeException(
77-
"Error: Cannot read Resource File " + file + "(" + resource + ")!");
78-
}
79-
return file;
80-
}
81-
8250
private File createTempDirectory() {
8351
try {
84-
File file = new File(Files.createTempDirectory("validator").toString());
85-
file.deleteOnExit();
86-
return file;
52+
Path dirPath = Files.createTempDirectory("validator");
53+
deleteFileAndContentOnExit(dirPath);
54+
return dirPath.toFile();
8755
} catch (IOException e) {
8856
throw new RuntimeException(e.getMessage());
8957
}
9058
}
59+
60+
private static void deleteFileAndContentOnExit(Path dirPath) {
61+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
62+
if (Files.exists(dirPath)) {
63+
try (Stream<Path> walk = Files.walk(dirPath)) {
64+
walk.sorted(Comparator.reverseOrder()) // Reverse order to delete contents first
65+
.forEach(path -> {
66+
try {
67+
Files.delete(path);
68+
} catch (IOException e) {
69+
// Do nothing
70+
}
71+
});
72+
} catch (IOException e) {
73+
System.err.println("Failed to delete temp dir " + dirPath);
74+
}
75+
}
76+
}));
77+
}
9178
}

third_party/wff/specification/validator/src/main/java/com/samsung/watchface/utils/UnzipUtility.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
import java.io.File;
2121
import java.io.FileInputStream;
2222
import java.io.FileOutputStream;
23+
import java.io.InputStream;
2324
import java.util.zip.ZipEntry;
2425
import java.util.zip.ZipInputStream;
2526

2627
public class UnzipUtility {
2728
private static final int BUFFER_SIZE = 4096;
2829

29-
public static void unzip(String zipFilePath, String destDirectory) {
30+
public static void unzip(InputStream zipInputStream, String destDirectory) {
3031
tryCreateDirectory(destDirectory);
3132

32-
try (FileInputStream fileIn = new FileInputStream(zipFilePath);
33-
ZipInputStream zipIn = new ZipInputStream(fileIn)) {
33+
try (ZipInputStream zipIn = new ZipInputStream(zipInputStream)) {
3434
ZipEntry entry = zipIn.getNextEntry();
3535
while (entry != null) {
3636
final String filePath = destDirectory + File.separator + entry.getName();

0 commit comments

Comments
 (0)