Fixed #121 by catching future errors. Untested whether this works or not. Ran mbaxter's formatter through some files.

This commit is contained in:
SuPaH sPii committed 2012-12-20 22:17:48 -06:00
1 parent 1f6e4175aa
commit c8bc5f82f3
1 file changed
+82 -39
@@ -35,17 +35,23 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
/**
* Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed.
* Check dev.bukkit.org to find updates for a given plugin, and download the
* updates if needed.
* <p>
* <strong>VERY, VERY IMPORTANT</strong>: Because there are no standards for adding auto-update toggles in your plugin's config, this system provides NO CHECK WITH YOUR CONFIG to make sure the user has allowed auto-updating.
* <br>
* It is a <strong>BUKKIT POLICY</strong> that you include a boolean value in your config that prevents the auto-updater from running <strong>AT ALL</strong>.
* <br>
* If you fail to include this option in your config, your plugin will be <strong>REJECTED</strong> when you attempt to submit it to dev.bukkit.org.
* <strong>VERY, VERY IMPORTANT</strong>: Because there are no standards for
* adding auto-update toggles in your plugin's config, this system provides NO
* CHECK WITH YOUR CONFIG to make sure the user has allowed auto-updating. <br>
* It is a <strong>BUKKIT POLICY</strong> that you include a boolean value in
* your config that prevents the auto-updater from running <strong>AT
* ALL</strong>. <br>
* If you fail to include this option in your config, your plugin will be
* <strong>REJECTED</strong> when you attempt to submit it to dev.bukkit.org.
* <p>
* An example of a good configuration option would be something similar to 'auto-update: true' - if this value is set to false you may NOT run the auto-updater.
* <br>
* If you are unsure about these rules, please read the plugin submission guidelines: http://goo.gl/8iU5l
* An example of a good configuration option would be something similar to
* 'auto-update: true' - if this value is set to false you may NOT run the
* auto-updater. <br>
* If you are unsure about these rules, please read the plugin submission
* guidelines: http://goo.gl/8iU5l
*
* @author H31IX
*/
@@ -74,12 +80,14 @@ public class Updater
private static final String ITEM = "item";
/**
* Gives the dev the result of the update process. Can be obtained by called getResult().
* Gives the dev the result of the update process. Can be obtained by called
* getResult().
*/
public enum UpdateResult
{
/**
* The updater found an update, and has readied it to be loaded the next time the server restarts/reloads.
* The updater found an update, and has readied it to be loaded the next
* time the server restarts/reloads.
*/
SUCCESS(1),
/**
@@ -91,21 +99,29 @@ public class Updater
*/
FAIL_DOWNLOAD(3),
/**
* For some reason, the updater was unable to contact dev.bukkit.org to download the file.
* For some reason, the updater was unable to contact dev.bukkit.org to
* download the file.
*/
FAIL_DBO(4),
/**
* When running the version check, the file on DBO did not contain the a version in the format 'vVersion' such as 'v1.0'.
* When running the version check, the file on DBO did not contain the a
* version in the format 'vVersion' such as 'v1.0'.
*/
FAIL_NOVERSION(5),
/**
* The slug provided by the plugin running the updater was invalid and doesn't exist on DBO.
* The slug provided by the plugin running the updater was invalid and
* doesn't exist on DBO.
*/
FAIL_BADSLUG(6),
/**
* The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded.
* The updater found an update, but because of the UpdateType being set
* to NO_DOWNLOAD, it wasn't downloaded.
*/
UPDATE_AVAILABLE(7);
UPDATE_AVAILABLE(7),
/**
* The update was unable to update due to a unknown exception thrown.
*/
UNKNOWN_ERROR(8);
private static final Map<Integer, Updater.UpdateResult> valueList = new HashMap<Integer, Updater.UpdateResult>();
private final int value;
@@ -140,15 +156,18 @@ public class Updater
public enum UpdateType
{
/**
* Run a version check, and then if the file is out of date, download the newest version.
* Run a version check, and then if the file is out of date, download
* the newest version.
*/
DEFAULT(1),
/**
* Don't run a version check, just find the latest update and download it.
* Don't run a version check, just find the latest update and download
* it.
*/
NO_VERSION_CHECK(2),
/**
* Get information about the version and the download size, but don't actually download anything.
* Get information about the version and the download size, but don't
* actually download anything.
*/
NO_DOWNLOAD(3);
@@ -185,13 +204,17 @@ public class Updater
* @param plugin
* The plugin that is checking for an update.
* @param slug
* The dev.bukkit.org slug of the project (http://dev.bukkit.org/server-mods/SLUG_IS_HERE)
* The dev.bukkit.org slug of the project
* (http://dev.bukkit.org/server-mods/SLUG_IS_HERE)
* @param file
* The file that the plugin is running from, get this by doing this.getFile() from within your main class.
* The file that the plugin is running from, get this by doing
* this.getFile() from within your main class.
* @param type
* Specify the type of update this will be. See {@link UpdateType}
* Specify the type of update this will be. See
* {@link UpdateType}
* @param announce
* True if the program should announce the progress of new updates in console
* True if the program should announce the progress of new
* updates in console
*/
public Updater(Plugin plugin, String slug, File file, UpdateType type, boolean announce)
{
@@ -202,14 +225,6 @@ public class Updater
{
// Obtain the results of the project's file feed
url = new URL(DBOUrl + slug + "/files.rss");
}
catch (MalformedURLException ex)
{
// The slug doesn't exist
plugin.getLogger().warning("The author of this plugin has misconfigured their Auto Update system");
plugin.getLogger().warning("The project slug added ('" + slug + "') is invalid, and does not exist on dev.bukkit.org");
result = Updater.UpdateResult.FAIL_BADSLUG; // Bad slug! Bad!
}
if (url != null)
{
// Obtain the results of the project's file feed
@@ -235,6 +250,25 @@ public class Updater
}
}
}
catch (MalformedURLException ex)
{
// The slug doesn't exist
plugin.getLogger().warning("The author of this plugin has misconfigured their Auto Update system");
plugin.getLogger().warning("The project slug added ('" + slug + "') is invalid, and does not exist on dev.bukkit.org");
result = Updater.UpdateResult.FAIL_BADSLUG; // Bad slug! Bad!
}
catch (RuntimeException ex)
{
// Something went wrong with the updater and can't check for updates.
if (result == Updater.UpdateResult.SUCCESS)
{
plugin.getLogger().warning("An error had occured and Auto Update was unable to check for updates");
plugin.getLogger().warning("Here is a stacktrace of what happened: ");
ex.printStackTrace();
result = Updater.UpdateResult.UNKNOWN_ERROR;
}
}
}
/**
* Get the result of the update process.
@@ -245,7 +279,8 @@ public class Updater
}
/**
* Get the total bytes of the file (can only be used after running a version check or a normal run).
* Get the total bytes of the file (can only be used after running a version
* check or a normal run).
*/
public long getFileSize()
{
@@ -281,7 +316,8 @@ public class Updater
byte[] data = new byte[BYTE_SIZE];
int count;
if(announce) plugin.getLogger().info("About to download a new update: " + versionTitle);
if (announce)
plugin.getLogger().info("About to download a new update: " + versionTitle);
long downloaded = 0;
while ((count = in.read(data, 0, BYTE_SIZE)) != -1)
{
@@ -308,7 +344,8 @@ public class Updater
// Unzip
unzip(dFile.getCanonicalPath());
}
if(announce) plugin.getLogger().info("Finished updating.");
if (announce)
plugin.getLogger().info("Finished updating.");
}
catch (Exception ex)
{
@@ -428,7 +465,8 @@ public class Updater
}
/**
* Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip.
* Check if the name of a jar is one of the plugins currently installed,
* used for extracting the correct files out of a zip.
*/
public boolean pluginFile(String name)
{
@@ -496,7 +534,8 @@ public class Updater
}
/**
* Check to see if the program should continue by evaluation whether the plugin is already updated, or shouldn't be updated
* Check to see if the program should continue by evaluation whether the
* plugin is already updated, or shouldn't be updated
*/
private boolean versionCheck(String title)
{
@@ -527,7 +566,8 @@ public class Updater
}
/**
* Evaluate whether the version number is marked showing that it should not be updated by this program
* Evaluate whether the version number is marked showing that it should not
* be updated by this program
*/
private boolean hasTag(String version, String remoteVersion)
{
@@ -552,7 +592,7 @@ public class Updater
/**
* Part of RSS Reader by Vogella, modified by H31IX for use with Bukkit
*/
@SuppressWarnings("null")
@SuppressWarnings({ "deprecation", "restriction" })
private void readFeed()
{
try
@@ -614,7 +654,10 @@ public class Updater
}
catch (IOException e)
{
throw new RuntimeException(e);
e.printStackTrace();
plugin.getLogger().warning("The auto-updater tried to contact dev.bukkit.org, but was unsuccessful.");
result = Updater.UpdateResult.FAIL_DBO;
return null;
}
}
}