Merge pull request #147 from kruland2607/master
Added some logging and an additional mechanism to try to find the jar file URL.
This commit is contained in:
		
						commit
						f300c3a6be
					
				@ -1,15 +1,22 @@
 | 
			
		||||
package net.sf.openrocket.util;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URI;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
import java.security.CodeSource;
 | 
			
		||||
import java.util.Enumeration;
 | 
			
		||||
 | 
			
		||||
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,
 | 
			
		||||
	 * 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
 | 
			
		||||
		
 | 
			
		||||
		URL jarUrl = null;
 | 
			
		||||
		
 | 
			
		||||
		CodeSource codeSource;
 | 
			
		||||
		try {
 | 
			
		||||
			codeSource = new URL("rsrc:.").openConnection().getClass().getProtectionDomain().getCodeSource();
 | 
			
		||||
			logger.debug("Found jar file using rsrc URL");
 | 
			
		||||
		} catch (Throwable e) {
 | 
			
		||||
			codeSource = Database.class.getProtectionDomain().getCodeSource();
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if (codeSource != null)
 | 
			
		||||
		if (codeSource != null) {
 | 
			
		||||
			logger.debug("Found jar file using codeSource");
 | 
			
		||||
			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) {
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
@ -40,15 +71,13 @@ public class JarUtil {
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public static File urlToFile(URL url) {
 | 
			
		||||
		URI uri;
 | 
			
		||||
		try {
 | 
			
		||||
			uri = url.toURI();
 | 
			
		||||
		} catch (URISyntaxException e) {
 | 
			
		||||
			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());
 | 
			
		||||
			} catch (URISyntaxException e1) {
 | 
			
		||||
				throw new IllegalArgumentException("Broken URL: " + url);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user