Added some logging to help diagnose NPE bug reports from

ExampleDesignFileAction.
This commit is contained in:
kruland2607 2013-08-17 19:31:24 -05:00
parent 0775f780dd
commit 9e16bc8530

View File

@ -3,25 +3,24 @@ 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ExampleDesignFile implements Comparable<ExampleDesignFile> { 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;
@ -48,6 +47,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
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) {
@ -74,16 +74,20 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
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];
@ -108,6 +112,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
// 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;
@ -117,7 +122,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
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);
} }
@ -140,6 +145,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
} }
} 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,7 +153,6 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
try { try {
jarFile.close(); jarFile.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace();
} }
} }
} }