From 9e16bc85307d444634bcea8a154e6f3d3c5a40b8 Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Sat, 17 Aug 2013 19:31:24 -0500 Subject: [PATCH] Added some logging to help diagnose NPE bug reports from ExampleDesignFileAction. --- .../gui/main/ExampleDesignFile.java | 89 ++++++++++--------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java b/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java index bb8cd8545..d697f042b 100644 --- a/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java +++ b/core/src/net/sf/openrocket/gui/main/ExampleDesignFile.java @@ -3,62 +3,62 @@ package net.sf.openrocket.gui.main; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; -import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Enumeration; -import java.util.List; import java.util.jar.JarEntry; 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.JarUtil; -import net.sf.openrocket.util.Pair; -public class ExampleDesignFile implements Comparable { +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +public class ExampleDesignFile implements Comparable { + + private final static Logger logger = LoggerFactory.getLogger(ExampleDesignFile.class); + private final URL url; private final String name; - + private ExampleDesignFile(URL url, String name) { this.url = url; this.name = name; } - + @Override public String toString() { return name; } - + public URL getURL() { return url; } - + @Override public int compareTo(ExampleDesignFile o) { return this.name.compareTo(o.name); } - + public static ExampleDesignFile[] getExampleDesigns() { - + ExampleDesignFile[] designs = getJarFileNames(); if (designs == null || designs.length == 0) { + logger.debug("Cannot find jar file, trying to load from directory"); designs = getDirFileNames(); } if (designs == null || designs.length == 0) { return null; } - + Arrays.sort(designs); - + return designs; } - + private static final String DIRECTORY = "datafiles/examples/"; private static final String PATTERN = ".*\\.[oO][rR][kK]$"; private static final FilenameFilter FILTER = new FilenameFilter() { @@ -67,65 +67,70 @@ public class ExampleDesignFile implements Comparable { return name.matches(PATTERN); } }; - + private static ExampleDesignFile[] getDirFileNames() { - + // Try to find directory as a system resource File dir; URL url = ClassLoader.getSystemResource(DIRECTORY); - + + logger.debug("Loading example from {} ", url); try { dir = JarUtil.urlToFile(url); } catch (Exception e1) { dir = new File(DIRECTORY); } - + + logger.debug("Directory to search is: {}", dir); // Get the list of files File[] files = dir.listFiles(FILTER); - if (files == null) + if (files == null) { + logger.debug("No files found in directory"); return null; - + } + ExampleDesignFile[] designs = new ExampleDesignFile[files.length]; - - for (int i=0; i list = new ArrayList(); int dirLength = DIRECTORY.length(); - + // Find and open the jar file this class is contained in File file = JarUtil.getCurrentJarFile(); + logger.debug("Current jar file is: {}", file); if (file == null) return null; - - + + // Generate URL pointing to JAR file URL fileUrl; try { fileUrl = file.toURI().toURL(); } catch (MalformedURLException e1) { - e1.printStackTrace(); + logger.error("Unable to transform file name {} to URL", file, e1); throw new BugException(e1); } - + // Iterate over JAR entries searching for designs JarFile jarFile = null; try { jarFile = new JarFile(file); - + // Loop through JAR entries searching for files to load Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { @@ -134,12 +139,13 @@ public class ExampleDesignFile implements Comparable { if (name.startsWith(DIRECTORY) && FILTER.accept(null, name)) { String urlName = "jar:" + fileUrl + "!/" + name; URL url = new URL(urlName); - list.add(new ExampleDesignFile(url, - name.substring(dirLength, name.length()-4))); + list.add(new ExampleDesignFile(url, + name.substring(dirLength, name.length() - 4))); } } - + } catch (IOException e) { + logger.error("IOException when processing jarFile", e); // Could be normal condition if not package in JAR return null; } finally { @@ -147,12 +153,11 @@ public class ExampleDesignFile implements Comparable { try { jarFile.close(); } catch (IOException e) { - e.printStackTrace(); } } } - + return list.toArray(new ExampleDesignFile[0]); } - + } \ No newline at end of file