Merge pull request #110 from kruland2607/master

Added some logging to help diagnose NPE bug reports
This commit is contained in:
kruland2607 2013-08-21 21:58:54 -07:00
commit a05c08ac8b

View File

@ -3,62 +3,62 @@ package net.sf.openrocket.gui.main;
import java.io.File; import java.io.File;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import net.sf.openrocket.file.iterator.DirectoryIterator;
import net.sf.openrocket.file.iterator.FileIterator;
import net.sf.openrocket.gui.util.SimpleFileFilter;
import net.sf.openrocket.util.BugException; import net.sf.openrocket.util.BugException;
import net.sf.openrocket.util.JarUtil; import net.sf.openrocket.util.JarUtil;
import net.sf.openrocket.util.Pair;
public class ExampleDesignFile implements Comparable<ExampleDesignFile> { import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
private final static Logger logger = LoggerFactory.getLogger(ExampleDesignFile.class);
private final URL url; private final URL url;
private final String name; private final String name;
private ExampleDesignFile(URL url, String name) { private ExampleDesignFile(URL url, String name) {
this.url = url; this.url = url;
this.name = name; this.name = name;
} }
@Override @Override
public String toString() { public String toString() {
return name; return name;
} }
public URL getURL() { public URL getURL() {
return url; return url;
} }
@Override @Override
public int compareTo(ExampleDesignFile o) { public int compareTo(ExampleDesignFile o) {
return this.name.compareTo(o.name); return this.name.compareTo(o.name);
} }
public static ExampleDesignFile[] getExampleDesigns() { public static ExampleDesignFile[] getExampleDesigns() {
ExampleDesignFile[] designs = getJarFileNames(); ExampleDesignFile[] designs = getJarFileNames();
if (designs == null || designs.length == 0) { if (designs == null || designs.length == 0) {
logger.debug("Cannot find jar file, trying to load from directory");
designs = getDirFileNames(); designs = getDirFileNames();
} }
if (designs == null || designs.length == 0) { if (designs == null || designs.length == 0) {
return null; return null;
} }
Arrays.sort(designs); Arrays.sort(designs);
return designs; return designs;
} }
private static final String DIRECTORY = "datafiles/examples/"; private static final String DIRECTORY = "datafiles/examples/";
private static final String PATTERN = ".*\\.[oO][rR][kK]$"; private static final String PATTERN = ".*\\.[oO][rR][kK]$";
private static final FilenameFilter FILTER = new FilenameFilter() { private static final FilenameFilter FILTER = new FilenameFilter() {
@ -67,65 +67,70 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
return name.matches(PATTERN); return name.matches(PATTERN);
} }
}; };
private static ExampleDesignFile[] getDirFileNames() { private static ExampleDesignFile[] getDirFileNames() {
// Try to find directory as a system resource // Try to find directory as a system resource
File dir; File dir;
URL url = ClassLoader.getSystemResource(DIRECTORY); URL url = ClassLoader.getSystemResource(DIRECTORY);
logger.debug("Loading example from {} ", url);
try { try {
dir = JarUtil.urlToFile(url); dir = JarUtil.urlToFile(url);
} catch (Exception e1) { } catch (Exception e1) {
dir = new File(DIRECTORY); dir = new File(DIRECTORY);
} }
logger.debug("Directory to search is: {}", dir);
// Get the list of files // Get the list of files
File[] files = dir.listFiles(FILTER); File[] files = dir.listFiles(FILTER);
if (files == null) if (files == null) {
logger.debug("No files found in directory");
return null; return null;
}
ExampleDesignFile[] designs = new ExampleDesignFile[files.length]; ExampleDesignFile[] designs = new ExampleDesignFile[files.length];
for (int i=0; i<files.length; i++) { for (int i = 0; i < files.length; i++) {
String name = files[i].getName(); String name = files[i].getName();
try { try {
designs[i] = new ExampleDesignFile(files[i].toURI().toURL(), designs[i] = new ExampleDesignFile(files[i].toURI().toURL(),
name.substring(0, name.length()-4)); name.substring(0, name.length() - 4));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
throw new BugException(e); throw new BugException(e);
} }
} }
return designs; return designs;
} }
private static ExampleDesignFile[] getJarFileNames() { private static ExampleDesignFile[] getJarFileNames() {
ArrayList<ExampleDesignFile> list = new ArrayList<ExampleDesignFile>(); ArrayList<ExampleDesignFile> list = new ArrayList<ExampleDesignFile>();
int dirLength = DIRECTORY.length(); int dirLength = DIRECTORY.length();
// Find and open the jar file this class is contained in // Find and open the jar file this class is contained in
File file = JarUtil.getCurrentJarFile(); File file = JarUtil.getCurrentJarFile();
logger.debug("Current jar file is: {}", file);
if (file == null) if (file == null)
return null; return null;
// Generate URL pointing to JAR file // Generate URL pointing to JAR file
URL fileUrl; URL fileUrl;
try { try {
fileUrl = file.toURI().toURL(); fileUrl = file.toURI().toURL();
} catch (MalformedURLException e1) { } catch (MalformedURLException e1) {
e1.printStackTrace(); logger.error("Unable to transform file name {} to URL", file, e1);
throw new BugException(e1); throw new BugException(e1);
} }
// Iterate over JAR entries searching for designs // Iterate over JAR entries searching for designs
JarFile jarFile = null; JarFile jarFile = null;
try { try {
jarFile = new JarFile(file); jarFile = new JarFile(file);
// Loop through JAR entries searching for files to load // Loop through JAR entries searching for files to load
Enumeration<JarEntry> entries = jarFile.entries(); Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
@ -134,12 +139,13 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) { if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) {
String urlName = "jar:" + fileUrl + "!/" + name; String urlName = "jar:" + fileUrl + "!/" + name;
URL url = new URL(urlName); URL url = new URL(urlName);
list.add(new ExampleDesignFile(url, list.add(new ExampleDesignFile(url,
name.substring(dirLength, name.length()-4))); name.substring(dirLength, name.length() - 4)));
} }
} }
} catch (IOException e) { } catch (IOException e) {
logger.error("IOException when processing jarFile", e);
// Could be normal condition if not package in JAR // Could be normal condition if not package in JAR
return null; return null;
} finally { } finally {
@ -147,12 +153,11 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
try { try {
jarFile.close(); jarFile.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
} }
return list.toArray(new ExampleDesignFile[0]); return list.toArray(new ExampleDesignFile[0]);
} }
} }