1 package de.todesbaum.jsite.main;
3 import static java.util.Optional.empty;
6 import java.io.UnsupportedEncodingException;
8 import java.net.URLDecoder;
9 import java.util.Optional;
12 * Locates the JAR file used to load jSite in the filesystem.
14 public interface JarFileLocator {
16 Optional<File> locateJarFile();
18 class DefaultJarFileLocator implements JarFileLocator {
20 private final ClassLoader classLoader;
22 public DefaultJarFileLocator(ClassLoader classLoader) {
23 this.classLoader = classLoader;
27 public Optional<File> locateJarFile() {
28 URL resourceUrl = classLoader.getResource(Main.class.getName().replace(".", "/") + ".class");
29 if (resourceUrl == null) {
32 String resource = resourceUrl.toString();
33 if (resource.startsWith("jar:")) {
35 String jarFileLocation = URLDecoder.decode(resource.substring(9, resource.indexOf(".jar!") + 4), "UTF-8");
36 return Optional.of(new File(jarFileLocation));
37 } catch (UnsupportedEncodingException e) {
38 /* location is not available, ignore. */