From 332e8cad5da2915d5369946237d28f0d6a83acfe Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Sat, 18 Feb 2012 17:15:59 -0600 Subject: [Bleeding] Exception cleanup. Addresses BUKKIT-774 --- .../configuration/file/YamlConfiguration.java | 18 ++------- .../bukkit/plugin/InvalidDescriptionException.java | 12 +++--- .../org/bukkit/plugin/InvalidPluginException.java | 25 ++++++++++-- src/main/java/org/bukkit/plugin/PluginLoader.java | 6 +-- .../org/bukkit/plugin/SimplePluginManager.java | 34 ++++++----------- .../bukkit/plugin/UnknownDependencyException.java | 30 +++------------ .../plugin/UnknownSoftDependencyException.java | 44 ---------------------- .../org/bukkit/plugin/java/JavaPluginLoader.java | 29 ++++++++------ 8 files changed, 67 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/org/bukkit/plugin/UnknownSoftDependencyException.java (limited to 'src') diff --git a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java index f0d79106..80842a70 100644 --- a/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java +++ b/src/main/java/org/bukkit/configuration/file/YamlConfiguration.java @@ -181,13 +181,7 @@ public class YamlConfiguration extends FileConfiguration { } catch (IOException ex) { Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex); } catch (InvalidConfigurationException ex) { - if (ex.getCause() instanceof YAMLException) { - Bukkit.getLogger().severe("Config file " + file + " isn't valid! " + ex.getCause()); - } else if ((ex.getCause() == null) || (ex.getCause() instanceof ClassCastException)) { - Bukkit.getLogger().severe("Config file " + file + " isn't valid!"); - } else { - Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file + ": " + ex.getCause().getClass(), ex); - } + Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file , ex); } return config; @@ -213,15 +207,9 @@ public class YamlConfiguration extends FileConfiguration { try { config.load(stream); } catch (IOException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration", ex); + Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", ex); } catch (InvalidConfigurationException ex) { - if (ex.getCause() instanceof YAMLException) { - Bukkit.getLogger().severe("Config file isn't valid! " + ex.getCause()); - } else if ((ex.getCause() == null) || (ex.getCause() instanceof ClassCastException)) { - Bukkit.getLogger().severe("Config file isn't valid!"); - } else { - Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration: " + ex.getCause().getClass(), ex); - } + Bukkit.getLogger().log(Level.SEVERE, "Cannot load configuration from stream", ex); } return config; diff --git a/src/main/java/org/bukkit/plugin/InvalidDescriptionException.java b/src/main/java/org/bukkit/plugin/InvalidDescriptionException.java index 170dbacb..a8c0bf86 100644 --- a/src/main/java/org/bukkit/plugin/InvalidDescriptionException.java +++ b/src/main/java/org/bukkit/plugin/InvalidDescriptionException.java @@ -4,7 +4,7 @@ package org.bukkit.plugin; * Thrown when attempting to load an invalid PluginDescriptionFile */ public class InvalidDescriptionException extends Exception { - private static final long serialVersionUID = 5721389122281775895L; + private static final long serialVersionUID = 5721389122281775896L; /** * Constructs a new InvalidDescriptionException based on the given Exception @@ -13,16 +13,16 @@ public class InvalidDescriptionException extends Exception { * @param cause Exception that triggered this Exception */ public InvalidDescriptionException(final Throwable cause, final String message) { - super(message + (cause != null ? ": " + cause.getMessage() : ""), cause); + super(message, cause); } /** * Constructs a new InvalidDescriptionException based on the given Exception * - * @param throwable Exception that triggered this Exception + * @param cause Exception that triggered this Exception */ public InvalidDescriptionException(final Throwable cause) { - this(cause, "Invalid plugin.yml"); + super("Invalid plugin.yml", cause); } /** @@ -31,13 +31,13 @@ public class InvalidDescriptionException extends Exception { * @param message Brief message explaining the cause of the exception */ public InvalidDescriptionException(final String message) { - this(null, message); + super(message); } /** * Constructs a new InvalidDescriptionException */ public InvalidDescriptionException() { - this(null, "Invalid plugin.yml"); + super("Invalid plugin.yml"); } } diff --git a/src/main/java/org/bukkit/plugin/InvalidPluginException.java b/src/main/java/org/bukkit/plugin/InvalidPluginException.java index 66abea74..ba1fc07e 100644 --- a/src/main/java/org/bukkit/plugin/InvalidPluginException.java +++ b/src/main/java/org/bukkit/plugin/InvalidPluginException.java @@ -4,7 +4,7 @@ package org.bukkit.plugin; * Thrown when attempting to load an invalid Plugin file */ public class InvalidPluginException extends Exception { - private static final long serialVersionUID = -8242141640709409543L; + private static final long serialVersionUID = -8242141640709409544L; /** * Constructs a new InvalidPluginException based on the given Exception @@ -12,13 +12,32 @@ public class InvalidPluginException extends Exception { * @param cause Exception that triggered this Exception */ public InvalidPluginException(final Throwable cause) { - super("Invalid plugin" + (cause != null ? ": " + cause.getMessage() : ""), cause); + super(cause); } /** * Constructs a new InvalidPluginException */ public InvalidPluginException() { - this(null); + + } + + /** + * Constructs a new InvalidPluginException with the specified detail message and cause. + * + * @param message the detail message (which is saved for later retrieval by the getMessage() method). + * @param cause the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.) + */ + public InvalidPluginException(final String message, final Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new InvalidPluginException with the specified detail message + * + * @param the detail message. The detail message is saved for later retrieval by the getMessage() method. + */ + public InvalidPluginException(final String message) { + super(message); } } diff --git a/src/main/java/org/bukkit/plugin/PluginLoader.java b/src/main/java/org/bukkit/plugin/PluginLoader.java index 83c65c89..5247b0ae 100644 --- a/src/main/java/org/bukkit/plugin/PluginLoader.java +++ b/src/main/java/org/bukkit/plugin/PluginLoader.java @@ -21,20 +21,18 @@ public interface PluginLoader { * @return Plugin that was contained in the specified file, or null if * unsuccessful * @throws InvalidPluginException Thrown when the specified file is not a plugin - * @throws InvalidDescriptionException If the plugin description file was invalid * @throws UnknownDependencyException If a required dependency could not be found */ - public Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException; + public Plugin loadPlugin(File file) throws InvalidPluginException, UnknownDependencyException; /** * Loads a PluginDescriptionFile from the specified file * * @param file File to attempt to load from * @return A new PluginDescriptionFile loaded from the plugin.yml in the specified file - * @throws InvalidPluginException If when the specified file does not contain a plugin description file * @throws InvalidDescriptionException If the plugin description file could not be created */ - public PluginDescriptionFile getPluginDescription(File file) throws InvalidPluginException, InvalidDescriptionException; + public PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException; /** * Returns a list of all filename filters expected by this PluginLoader diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java index d09466cb..621a3cd1 100644 --- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java +++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java @@ -131,11 +131,8 @@ public final class SimplePluginManager implements PluginManager { PluginDescriptionFile description = null; try { description = loader.getPluginDescription(file); - } catch (InvalidPluginException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); - continue; } catch (InvalidDescriptionException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); continue; } @@ -179,7 +176,7 @@ public final class SimplePluginManager implements PluginManager { server.getLogger().log( Level.SEVERE, - "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': ", + "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", new UnknownDependencyException(dependency)); break; } @@ -216,11 +213,7 @@ public final class SimplePluginManager implements PluginManager { loadedPlugins.add(plugin); continue; } catch (InvalidPluginException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': ", ex.getCause()); - } catch (InvalidDescriptionException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); - } catch (UnknownDependencyException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); } } } @@ -245,11 +238,7 @@ public final class SimplePluginManager implements PluginManager { loadedPlugins.add(plugin); break; } catch (InvalidPluginException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': ", ex.getCause()); - } catch (InvalidDescriptionException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); - } catch (UnknownDependencyException ex) { - server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "': " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Could not load '" + file.getPath() + "' in folder '" + directory.getPath() + "'", ex); } } } @@ -279,10 +268,9 @@ public final class SimplePluginManager implements PluginManager { * @param file File containing the plugin to load * @return The Plugin loaded, or null if it was invalid * @throws InvalidPluginException Thrown when the specified file is not a valid plugin - * @throws InvalidDescriptionException Thrown when the specified file contains an invalid description * @throws UnknownDependencyException If a required dependency could not be found */ - public synchronized Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException { + public synchronized Plugin loadPlugin(File file) throws InvalidPluginException, UnknownDependencyException { Validate.notNull(file, "File cannot be null"); checkUpdate(file); @@ -375,7 +363,7 @@ public final class SimplePluginManager implements PluginManager { try { plugin.getPluginLoader().enablePlugin(plugin); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while enabling " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while enabling " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } HandlerList.bakeAll(); @@ -393,32 +381,32 @@ public final class SimplePluginManager implements PluginManager { try { plugin.getPluginLoader().disablePlugin(plugin); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while disabling " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while disabling " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } try { server.getScheduler().cancelTasks(plugin); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while cancelling tasks for " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while cancelling tasks for " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } try { server.getServicesManager().unregisterAll(plugin); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering services for " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering services for " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } try { HandlerList.unregisterAll(plugin); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering events for " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering events for " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } try { server.getMessenger().unregisterIncomingPluginChannel(plugin); server.getMessenger().unregisterOutgoingPluginChannel(plugin); } catch(Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering plugin channels for " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred (in the plugin loader) while unregistering plugin channels for " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } } } diff --git a/src/main/java/org/bukkit/plugin/UnknownDependencyException.java b/src/main/java/org/bukkit/plugin/UnknownDependencyException.java index db476d40..2cc759ad 100644 --- a/src/main/java/org/bukkit/plugin/UnknownDependencyException.java +++ b/src/main/java/org/bukkit/plugin/UnknownDependencyException.java @@ -3,19 +3,17 @@ package org.bukkit.plugin; /** * Thrown when attempting to load an invalid Plugin file */ -public class UnknownDependencyException extends Exception { +public class UnknownDependencyException extends RuntimeException { - private static final long serialVersionUID = 5721389371901775894L; - private final Throwable cause; - private final String message; + private static final long serialVersionUID = 5721389371901775895L; /** * Constructs a new UnknownDependencyException based on the given Exception * * @param throwable Exception that triggered this Exception */ - public UnknownDependencyException(Throwable throwable) { - this(throwable, "Unknown dependency"); + public UnknownDependencyException(final Throwable throwable) { + super(throwable); } /** @@ -24,7 +22,7 @@ public class UnknownDependencyException extends Exception { * @param message Brief message explaining the cause of the exception */ public UnknownDependencyException(final String message) { - this(null, message); + super(message); } /** @@ -34,29 +32,13 @@ public class UnknownDependencyException extends Exception { * @param throwable Exception that triggered this Exception */ public UnknownDependencyException(final Throwable throwable, final String message) { - this.cause = null; - this.message = message; + super(message, throwable); } /** * Constructs a new UnknownDependencyException */ public UnknownDependencyException() { - this(null, "Unknown dependency"); - } - - /** - * If applicable, returns the Exception that triggered this Exception - * - * @return Inner exception, or null if one does not exist - */ - @Override - public Throwable getCause() { - return cause; - } - @Override - public String getMessage() { - return message; } } diff --git a/src/main/java/org/bukkit/plugin/UnknownSoftDependencyException.java b/src/main/java/org/bukkit/plugin/UnknownSoftDependencyException.java deleted file mode 100644 index 8a39dd0e..00000000 --- a/src/main/java/org/bukkit/plugin/UnknownSoftDependencyException.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.bukkit.plugin; - -/** - * Thrown when attempting to load an invalid Plugin file - */ -public class UnknownSoftDependencyException extends UnknownDependencyException { - - private static final long serialVersionUID = 5721389371901775899L; - - /** - * Constructs a new UnknownSoftDependencyException based on the given Exception - * - * @param throwable Exception that triggered this Exception - */ - public UnknownSoftDependencyException(Throwable throwable) { - this(throwable, "Unknown soft dependency"); - } - - /** - * Constructs a new UnknownSoftDependencyException with the given message - * - * @param message Brief message explaining the cause of the exception - */ - public UnknownSoftDependencyException(final String message) { - this(null, message); - } - - /** - * Constructs a new UnknownSoftDependencyException based on the given Exception - * - * @param message Brief message explaining the cause of the exception - * @param throwable Exception that triggered this Exception - */ - public UnknownSoftDependencyException(final Throwable throwable, final String message) { - super(throwable, message); - } - - /** - * Constructs a new UnknownSoftDependencyException - */ - public UnknownSoftDependencyException() { - this(null, "Unknown dependency"); - } -} diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index b31457dd..35e8d6b6 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -54,14 +54,19 @@ public class JavaPluginLoader implements PluginLoader { } @SuppressWarnings("unchecked") - public Plugin loadPlugin(File file) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException { + public Plugin loadPlugin(File file) throws InvalidPluginException { Validate.notNull(file, "File cannot be null"); if (!file.exists()) { - throw new InvalidPluginException(new FileNotFoundException(String.format("%s does not exist", file.getPath()))); + throw new InvalidPluginException(new FileNotFoundException(file.getPath() + " does not exist")); } - PluginDescriptionFile description = getPluginDescription(file); + PluginDescriptionFile description; + try { + description = getPluginDescription(file); + } catch (InvalidDescriptionException ex) { + throw new InvalidPluginException(ex); + } File dataFolder = new File(file.getParentFile(), description.getName()); File oldDataFolder = getDataFolder(file); @@ -79,7 +84,7 @@ public class JavaPluginLoader implements PluginLoader { )); } else if (oldDataFolder.isDirectory() && !dataFolder.exists()) { if (!oldDataFolder.renameTo(dataFolder)) { - throw new InvalidPluginException(new Exception("Unable to rename old data folder: '" + oldDataFolder + "' to: '" + dataFolder + "'")); + throw new InvalidPluginException("Unable to rename old data folder: '" + oldDataFolder + "' to: '" + dataFolder + "'"); } server.getLogger().log(Level.INFO, String.format( "While loading %s (%s) renamed data folder: '%s' to '%s'", @@ -91,12 +96,12 @@ public class JavaPluginLoader implements PluginLoader { } if (dataFolder.exists() && !dataFolder.isDirectory()) { - throw new InvalidPluginException(new Exception(String.format( + throw new InvalidPluginException(String.format( "Projected datafolder: '%s' for %s (%s) exists and is not a directory", dataFolder, description.getName(), file - ))); + )); } ArrayList depend; @@ -155,7 +160,7 @@ public class JavaPluginLoader implements PluginLoader { return result; } - public Plugin loadPlugin(File file, boolean ignoreSoftDependencies) throws InvalidPluginException, InvalidDescriptionException, UnknownDependencyException { + public Plugin loadPlugin(File file, boolean ignoreSoftDependencies) throws InvalidPluginException { return loadPlugin(file); } @@ -179,7 +184,7 @@ public class JavaPluginLoader implements PluginLoader { return dataFolder; } - public PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException, InvalidPluginException { + public PluginDescriptionFile getPluginDescription(File file) throws InvalidDescriptionException { Validate.notNull(file, "File cannot be null"); JarFile jar = null; @@ -190,7 +195,7 @@ public class JavaPluginLoader implements PluginLoader { JarEntry entry = jar.getJarEntry("plugin.yml"); if (entry == null) { - throw new InvalidPluginException(new FileNotFoundException("Jar does not contain plugin.yml")); + throw new InvalidDescriptionException(new FileNotFoundException("Jar does not contain plugin.yml")); } stream = jar.getInputStream(entry); @@ -198,7 +203,7 @@ public class JavaPluginLoader implements PluginLoader { return new PluginDescriptionFile(stream); } catch (IOException ex) { - throw new InvalidPluginException(ex); + throw new InvalidDescriptionException(ex); } catch (YAMLException ex) { throw new InvalidDescriptionException(ex); } finally { @@ -337,7 +342,7 @@ public class JavaPluginLoader implements PluginLoader { try { jPlugin.setEnabled(true); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred while enabling " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred while enabling " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } // Perhaps abort here, rather than continue going, but as it stands, @@ -363,7 +368,7 @@ public class JavaPluginLoader implements PluginLoader { try { jPlugin.setEnabled(false); } catch (Throwable ex) { - server.getLogger().log(Level.SEVERE, "Error occurred while disabling " + plugin.getDescription().getFullName() + " (Is it up to date?): " + ex.getMessage(), ex); + server.getLogger().log(Level.SEVERE, "Error occurred while disabling " + plugin.getDescription().getFullName() + " (Is it up to date?)", ex); } loaders.remove(jPlugin.getDescription().getName()); -- cgit v1.2.3