Browse Source

Merge pull request #58 from csmith/master

FileUtil kludges to work in bundles.
pull/59/head
Greg Holmes 7 years ago
parent
commit
13092095e0
1 changed files with 12 additions and 1 deletions
  1. 12
    1
      src/main/java/com/dmdirc/util/io/FileUtils.java

+ 12
- 1
src/main/java/com/dmdirc/util/io/FileUtils.java View File

@@ -75,7 +75,8 @@ public final class FileUtils {
75 75
      * @throws IllegalStateException If the application path cannot be determined for any reason
76 76
      */
77 77
     public static boolean isRunningFromJar(final Class<?> clazz) throws IllegalStateException {
78
-        return getApplicationPath(clazz).getFileName().toString().endsWith(".jar");
78
+        // If we're running from any kind of regular file, assume it's a jar.
79
+        return Files.isRegularFile(getApplicationPath(clazz));
79 80
     }
80 81
 
81 82
     /**
@@ -163,7 +164,9 @@ public final class FileUtils {
163 164
      * @param resource The resource to get a path for
164 165
      * @return The path for the specified resource
165 166
      * @throws URISyntaxException If the resource could not be mapped to a path
167
+     * @deprecated This doesn't and can't work reliably in all cases, and should be avoided.
166 168
      */
169
+    @Deprecated
167 170
     public static Path getPathForResource(final URL resource) throws URISyntaxException {
168 171
         if (JAR_FILE_SYSTEM.isPresent()) {
169 172
             final String path = resource.toURI().toString();
@@ -171,6 +174,14 @@ public final class FileUtils {
171 174
             if (index > -1) {
172 175
                 return JAR_FILE_SYSTEM.get().getPath(path.substring(index + 1));
173 176
             }
177
+
178
+            // If we're running inside an OSGI framework then the classloader returns bundleresource:// URIs.
179
+            // We don't have a filesystem handler for those, so instead map them on to the jar file system.
180
+            // [This is hacky and horrible. It assumes that we only have one bundle, and that this class
181
+            // is located in the same bundle as all of its callers.]
182
+            if (path.startsWith("bundleresource://")) {
183
+                return JAR_FILE_SYSTEM.get().getPath(path.substring(path.indexOf("/", 19)));
184
+            }
174 185
         }
175 186
 
176 187
         return Paths.get(resource.toURI());

Loading…
Cancel
Save