|
| 1 | +package de.fraunhofer.iem.swan; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.net.URL; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Enumeration; |
| 11 | +import java.util.List; |
| 12 | +import java.util.jar.JarEntry; |
| 13 | +import java.util.jar.JarFile; |
| 14 | + |
| 15 | +import org.apache.commons.io.FileUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class can extract resource entries from the JAR file that this |
| 19 | + * application is located in. Also it keeps track of extracted files to delete |
| 20 | + * them when the application is exiting. |
| 21 | + * |
| 22 | + * @author Ingo Budde (Fraunhofer IEM) |
| 23 | + * |
| 24 | + */ |
| 25 | +public class FileUtility { |
| 26 | + |
| 27 | + /** |
| 28 | + * List of temporary files that have been created and must be deleted before |
| 29 | + * exiting the application. |
| 30 | + */ |
| 31 | + private List<File> tempFiles = new ArrayList<File>(); |
| 32 | + |
| 33 | + /** |
| 34 | + * List of temporary directories that have been created and must be deleted |
| 35 | + * before exiting the application. |
| 36 | + */ |
| 37 | + private List<File> tempDirs = new ArrayList<File>(); |
| 38 | + |
| 39 | + /** |
| 40 | + * Finds and provides the bundled directory with the given resource location |
| 41 | + * inside this application's jar file. If it is not directly accessable from the |
| 42 | + * filesystem, it is copied to a temporary location. |
| 43 | + * |
| 44 | + * @param resourceLocation The location of the directory within the jar file. |
| 45 | + * @return The File describing the location of the provided directory. |
| 46 | + * @throws IOException When an error occurs while copying the directory. |
| 47 | + */ |
| 48 | + public File getResourceDirectory(String resourceLocation) throws IOException { |
| 49 | + |
| 50 | + File localFile = getLocalResource(resourceLocation); |
| 51 | + if (localFile != null) { |
| 52 | + return localFile; |
| 53 | + } |
| 54 | + |
| 55 | + // Create Temporary Directory |
| 56 | + Path path = Files.createTempDirectory("swan"); |
| 57 | + File tempDir = path.toFile(); |
| 58 | + this.tempDirs.add(tempDir); |
| 59 | + |
| 60 | + // Find JAR Entries |
| 61 | + URL url = FileUtility.class.getResource(resourceLocation); |
| 62 | + if (resourceLocation.startsWith("/")) { |
| 63 | + resourceLocation = resourceLocation.substring(1); |
| 64 | + } |
| 65 | + if ("jar".equals(url.getProtocol())) { |
| 66 | + String[] splitted = url.getPath().split("\\!"); |
| 67 | + splitted = splitted[0].split("file:/"); |
| 68 | + String jarPath = splitted[splitted.length - 1]; |
| 69 | + File jarFile = new File(jarPath); |
| 70 | + if (jarFile.exists()) { |
| 71 | + JarFile jar = null; |
| 72 | + try { |
| 73 | + jar = new JarFile(jarFile.getAbsolutePath()); |
| 74 | + Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries in jar |
| 75 | + while (entries.hasMoreElements()) { |
| 76 | + String name = entries.nextElement().getName(); |
| 77 | + if (name.startsWith(resourceLocation) && !name.endsWith("/")) { |
| 78 | + String destinationName = name.substring(resourceLocation.length()); |
| 79 | + File destination = new File(tempDir, destinationName); |
| 80 | + InputStream stream = FileUtility.class.getResourceAsStream("/" + name); |
| 81 | + FileUtils.copyInputStreamToFile(stream, destination); |
| 82 | + } |
| 83 | + } |
| 84 | + } finally { |
| 85 | + if (jar != null) { |
| 86 | + jar.close(); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return tempDir; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Finds and provides the bundled file with the given resource location inside |
| 96 | + * this application's jar file. If it is not directly accessable from the |
| 97 | + * filesystem, it is copied to a temporary location. |
| 98 | + * |
| 99 | + * @param resourceLocation The location of the file within the jar file. |
| 100 | + * @return The File describing the location of the provided file. |
| 101 | + * @throws IOException When an error occurs while copying the file. |
| 102 | + */ |
| 103 | + public File getResourceFile(String resourceLocation) throws IOException { |
| 104 | + return getResourceFile(resourceLocation, null); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Finds and provides the bundled file with the given resource location inside |
| 109 | + * this application's jar file. If it is not directly accessable from the |
| 110 | + * filesystem, it is copied to a temporary location. |
| 111 | + * |
| 112 | + * @param resourceLocation The location of the file within the jar file. |
| 113 | + * @param destinationDirectory The directory to place the file in, in case it |
| 114 | + * needs to be copied. If <code>null</code>, a new |
| 115 | + * temporary file is created and used. |
| 116 | + * @return The File describing the location of the provided file. |
| 117 | + * @throws IOException When an error occurs while copying the file. |
| 118 | + */ |
| 119 | + public File getResourceFile(String resourceLocation, File destinationFile) throws IOException { |
| 120 | + File localFile = getLocalResource(resourceLocation); |
| 121 | + if (localFile != null) { |
| 122 | + return localFile; |
| 123 | + } |
| 124 | + String[] segments = resourceLocation.split("\\/|\\\\"); |
| 125 | + String filename = segments[segments.length - 1]; |
| 126 | + if (destinationFile == null) { |
| 127 | + Path path = Files.createTempFile("swan", filename); |
| 128 | + File tempFile = path.toFile(); |
| 129 | + destinationFile = tempFile; |
| 130 | + this.tempFiles.add(tempFile); |
| 131 | + } |
| 132 | + |
| 133 | + InputStream stream = FileUtility.class.getResourceAsStream(resourceLocation); |
| 134 | + FileUtils.copyToFile(stream, destinationFile); |
| 135 | + |
| 136 | + return destinationFile; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns the jar resource from the local filesystem, if it exists. |
| 141 | + * |
| 142 | + * @param resourceLocation The location of the file within the jar file. |
| 143 | + * @return The File from the local filesystem, or <code>null</code>, if no such |
| 144 | + * file exists. |
| 145 | + */ |
| 146 | + private File getLocalResource(String resourceLocation) { |
| 147 | + URL url = FileUtility.class.getResource(resourceLocation); |
| 148 | + if ("file".equals(url.getProtocol())) { |
| 149 | + return new File(url.getFile()); |
| 150 | + } |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Cleans up this object and deletes all temporary files and directies. This |
| 156 | + * should be called before the application exists, or after an analysis is done. |
| 157 | + */ |
| 158 | + public void dispose() { |
| 159 | + for (File directory : tempDirs) { |
| 160 | + try { |
| 161 | + FileUtils.deleteDirectory(directory); |
| 162 | + } catch (Exception e) { |
| 163 | + e.printStackTrace(); |
| 164 | + } |
| 165 | + } |
| 166 | + for (File file : tempFiles) { |
| 167 | + try { |
| 168 | + file.delete(); |
| 169 | + } catch (Exception e) { |
| 170 | + e.printStackTrace(); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments