Merge pull request from kruland2607/master

Added some logging and an additional mechanism to try to find the jar file URL.
This commit is contained in:
kruland2607 2013-09-26 09:25:52 -07:00
commit f300c3a6be

@ -1,15 +1,22 @@
package net.sf.openrocket.util; package net.sf.openrocket.util;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.security.CodeSource; import java.security.CodeSource;
import java.util.Enumeration;
import net.sf.openrocket.database.Database; import net.sf.openrocket.database.Database;
public class JarUtil { import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JarUtil {
private final static Logger logger = LoggerFactory.getLogger(JarUtil.class);
/** /**
* Return the a File object pointing to the JAR file that this class belongs to, * Return the a File object pointing to the JAR file that this class belongs to,
* or <code>null</code> if it cannot be found. * or <code>null</code> if it cannot be found.
@ -20,16 +27,40 @@ public class JarUtil {
// Find the jar file this class is contained in // Find the jar file this class is contained in
URL jarUrl = null; URL jarUrl = null;
CodeSource codeSource; CodeSource codeSource;
try { try {
codeSource = new URL("rsrc:.").openConnection().getClass().getProtectionDomain().getCodeSource(); codeSource = new URL("rsrc:.").openConnection().getClass().getProtectionDomain().getCodeSource();
logger.debug("Found jar file using rsrc URL");
} catch (Throwable e) { } catch (Throwable e) {
codeSource = Database.class.getProtectionDomain().getCodeSource(); codeSource = Database.class.getProtectionDomain().getCodeSource();
} }
if (codeSource != null) if (codeSource != null) {
logger.debug("Found jar file using codeSource");
jarUrl = codeSource.getLocation(); jarUrl = codeSource.getLocation();
}
if (jarUrl == null) {
try {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Enumeration<URL> urls = cl.getResources("README.TXT");
if (!urls.hasMoreElements()) {
return null;
}
URL readmeURL = urls.nextElement();
jarUrl = readmeURL;
String urlString = readmeURL.toString();
// cut off the trailing path component:
urlString = urlString.substring(0, urlString.length() - "!/README.TXT".length());
// cut off the prefix jar:
urlString = urlString.substring("jar:".length());
logger.debug("jar file location using README.TXT is {}", urlString);
jarUrl = new URL(urlString);
logger.debug("Found jar file using README.TXT location");
} catch (IOException e1) {
}
}
if (jarUrl == null) { if (jarUrl == null) {
return null; return null;
} }
@ -40,15 +71,13 @@ public class JarUtil {
return null; return null;
} }
public static File urlToFile(URL url) { public static File urlToFile(URL url) {
URI uri; URI uri;
try { try {
uri = url.toURI(); uri = url.toURI();
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
try { try {
uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(),
url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url.getPort(), url.getPath(), url.getQuery(), url.getRef());
} catch (URISyntaxException e1) { } catch (URISyntaxException e1) {
throw new IllegalArgumentException("Broken URL: " + url); throw new IllegalArgumentException("Broken URL: " + url);