|
24 | 24 | import java.io.OutputStream; |
25 | 25 | import java.net.URL; |
26 | 26 | import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.Comparator; |
27 | 29 | import java.util.Objects; |
| 30 | +import java.util.stream.Stream; |
28 | 31 |
|
29 | 32 | class ResourceManager { |
30 | 33 | private static final String RESOURCE_ZIP_XSD_DOCS = "/docs.zip"; |
31 | | - private static final int BUFFER_SIZE = 4096; |
32 | 34 |
|
33 | 35 | private final File xsdTempDirectory; |
34 | 36 |
|
35 | 37 | ResourceManager() { |
36 | | - // load resources |
37 | | - String zipFilePath = getResourceAsFile(RESOURCE_ZIP_XSD_DOCS).toString(); |
38 | 38 | xsdTempDirectory = createTempDirectory(); |
39 | | - UnzipUtility.unzip(zipFilePath, xsdTempDirectory.toString()); |
| 39 | + UnzipUtility.unzip( |
| 40 | + this.getClass().getResourceAsStream(RESOURCE_ZIP_XSD_DOCS), |
| 41 | + xsdTempDirectory.toString() |
| 42 | + ); |
40 | 43 | } |
41 | 44 |
|
42 | 45 | File getXsdFile(String version) { |
43 | 46 | return new File(xsdTempDirectory + File.separator + |
44 | 47 | version + File.separator + "watchface.xsd"); |
45 | 48 | } |
46 | 49 |
|
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 | | - |
82 | 50 | private File createTempDirectory() { |
83 | 51 | 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(); |
87 | 55 | } catch (IOException e) { |
88 | 56 | throw new RuntimeException(e.getMessage()); |
89 | 57 | } |
90 | 58 | } |
| 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 | + } |
91 | 78 | } |
0 commit comments