Fix bug when JAR file is stored in path with non-US-ASCII characters
[jSite.git] / src / main / java / de / todesbaum / jsite / main / JarFileLocator.java
diff --git a/src/main/java/de/todesbaum/jsite/main/JarFileLocator.java b/src/main/java/de/todesbaum/jsite/main/JarFileLocator.java
new file mode 100644 (file)
index 0000000..da2e766
--- /dev/null
@@ -0,0 +1,45 @@
+package de.todesbaum.jsite.main;
+
+import static java.util.Optional.empty;
+
+import java.io.File;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.net.URLDecoder;
+import java.util.Optional;
+
+/**
+ * Locates the JAR file used to load jSite in the filesystem.
+ */
+public interface JarFileLocator {
+
+       Optional<File> locateJarFile();
+
+       class DefaultJarFileLocator implements JarFileLocator {
+
+               private final ClassLoader classLoader;
+
+               public DefaultJarFileLocator(ClassLoader classLoader) {
+                       this.classLoader = classLoader;
+               }
+
+               @Override
+               public Optional<File> locateJarFile() {
+                       URL resourceUrl = classLoader.getResource(Main.class.getName().replace(".", "/") + ".class");
+                       if (resourceUrl == null) {
+                               return empty();
+                       }
+                       String resource = resourceUrl.toString();
+                       if (resource.startsWith("jar:")) {
+                               try {
+                                       String jarFileLocation = URLDecoder.decode(resource.substring(9, resource.indexOf(".jar!") + 4), "UTF-8");
+                                       return Optional.of(new File(jarFileLocation));
+                               } catch (UnsupportedEncodingException e) {
+                                       /* location is not available, ignore. */
+                               }
+                       }
+                       return empty();
+               }
+       }
+
+}