diff --git a/src/net/gravitydevelopment/updater/Updater.java b/src/net/gravitydevelopment/updater/Updater.java index 1ce2609..dfc3a5f 100644 --- a/src/net/gravitydevelopment/updater/Updater.java +++ b/src/net/gravitydevelopment/updater/Updater.java @@ -143,9 +143,9 @@ public Updater(Plugin plugin, int id, File file, UpdateType type, boolean announ this.id = id; this.updateFolder = plugin.getServer().getUpdateFolder(); - File pluginFile = plugin.getDataFolder().getParentFile(); - File updaterFile = new File(pluginFile, "Updater"); - File updaterConfigFile = new File(updaterFile, "config.yml"); + final File pluginFile = plugin.getDataFolder().getParentFile(); + final File updaterFile = new File(pluginFile, "Updater"); + final File updaterConfigFile = new File(updaterFile, "config.yml"); if (!updaterFile.exists()) { updaterFile.mkdir(); @@ -153,80 +153,80 @@ public Updater(Plugin plugin, int id, File file, UpdateType type, boolean announ if (!updaterConfigFile.exists()) { try { updaterConfigFile.createNewFile(); - } catch (IOException e) { + } catch (final IOException e) { plugin.getLogger().severe("The updater could not create a configuration in " + updaterFile.getAbsolutePath()); e.printStackTrace(); } } - config = YamlConfiguration.loadConfiguration(updaterConfigFile); + this.config = YamlConfiguration.loadConfiguration(updaterConfigFile); - config.addDefault("api-key", "PUT_API_KEY_HERE"); - config.addDefault("disable", false); + this.config.addDefault("api-key", "PUT_API_KEY_HERE"); + this.config.addDefault("disable", false); - if (config.get("api-key", null) == null) { - config.options().copyDefaults(true); + if (this.config.get("api-key", null) == null) { + this.config.options().copyDefaults(true); try { - config.save(updaterConfigFile); - } catch (IOException e) { + this.config.save(updaterConfigFile); + } catch (final IOException e) { plugin.getLogger().severe("The updater could not save the configuration in " + updaterFile.getAbsolutePath()); e.printStackTrace(); } } - if (config.getBoolean("disable")) { - result = UpdateResult.DISABLED; + if (this.config.getBoolean("disable")) { + this.result = UpdateResult.DISABLED; return; } - String key = config.getString("api-key"); + String key = this.config.getString("api-key"); if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) { key = null; } - apiKey = key; + this.apiKey = key; try { - url = new URL(HOST + QUERY + id); - } catch (MalformedURLException e) { + this.url = new URL(Updater.HOST + Updater.QUERY + id); + } catch (final MalformedURLException e) { plugin.getLogger().severe("The project ID provided for updating, " + id + " is invalid."); - result = UpdateResult.FAIL_BADID; + this.result = UpdateResult.FAIL_BADID; e.printStackTrace(); } - thread = new Thread(new UpdateRunnable()); - thread.start(); + this.thread = new Thread(new UpdateRunnable()); + this.thread.start(); } /** * Get the result of the update process. */ public Updater.UpdateResult getResult() { - waitForThread(); - return result; + this.waitForThread(); + return this.result; } /** * Get the latest version's release type (release, beta, or alpha) */ public String getLatestType() { - waitForThread(); - return versionType; + this.waitForThread(); + return this.versionType; } /** * Get the latest version's game version */ public String getLatestGameVersion() { - waitForThread(); - return versionGameVersion; + this.waitForThread(); + return this.versionGameVersion; } /** * Get the latest version's name */ public String getLatestName() { - waitForThread(); - return versionName; + this.waitForThread(); + return this.versionName; } /** @@ -234,10 +234,10 @@ public String getLatestName() { * before allowing anyone to check the result. */ private void waitForThread() { - if (thread != null && thread.isAlive()) { + if ((this.thread != null) && this.thread.isAlive()) { try { - thread.join(); - } catch (InterruptedException e) { + this.thread.join(); + } catch (final InterruptedException e) { e.printStackTrace(); } } @@ -254,39 +254,43 @@ private void saveFile(File folder, String file, String u) { FileOutputStream fout = null; try { // Download the file - URL url = new URL(u); - int fileLength = url.openConnection().getContentLength(); + final URL url = new URL(u); + final int fileLength = url.openConnection().getContentLength(); in = new BufferedInputStream(url.openStream()); fout = new FileOutputStream(folder.getAbsolutePath() + "/" + file); - byte[] data = new byte[BYTE_SIZE]; + final byte[] data = new byte[Updater.BYTE_SIZE]; int count; - if (announce) plugin.getLogger().info("About to download a new update: " + versionName); + if (this.announce) { + this.plugin.getLogger().info("About to download a new update: " + this.versionName); + } long downloaded = 0; - while ((count = in.read(data, 0, BYTE_SIZE)) != -1) { + while ((count = in.read(data, 0, Updater.BYTE_SIZE)) != -1) { downloaded += count; fout.write(data, 0, count); - int percent = (int) (downloaded * 100 / fileLength); - if (announce & (percent % 10 == 0)) { - plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes."); + final int percent = (int) ((downloaded * 100) / fileLength); + if (this.announce & ((percent % 10) == 0)) { + this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes."); } } //Just a quick check to make sure we didn't leave any files from last time... - for (File xFile : new File(plugin.getDataFolder().getParent(), updateFolder).listFiles()) { + for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) { if (xFile.getName().endsWith(".zip")) { xFile.delete(); } } // Check to see if it's a zip file, if it is, unzip it. - File dFile = new File(folder.getAbsolutePath() + "/" + file); + final File dFile = new File(folder.getAbsolutePath() + "/" + file); if (dFile.getName().endsWith(".zip")) { // Unzip - unzip(dFile.getCanonicalPath()); + this.unzip(dFile.getCanonicalPath()); + } + if (this.announce) { + this.plugin.getLogger().info("Finished updating."); } - if (announce) plugin.getLogger().info("Finished updating."); - } catch (Exception ex) { - plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful."); - result = Updater.UpdateResult.FAIL_DOWNLOAD; + } catch (final Exception ex) { + this.plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful."); + this.result = Updater.UpdateResult.FAIL_DOWNLOAD; } finally { try { if (in != null) { @@ -295,7 +299,7 @@ private void saveFile(File folder, String file, String u) { if (fout != null) { fout.close(); } - } catch (Exception ex) { + } catch (final Exception ex) { } } } @@ -305,8 +309,8 @@ private void saveFile(File folder, String file, String u) { */ private void unzip(String file) { try { - File fSourceZip = new File(file); - String zipPath = file.substring(0, file.length() - 4); + final File fSourceZip = new File(file); + final String zipPath = file.substring(0, file.length() - 4); ZipFile zipFile = new ZipFile(fSourceZip); Enumeration e = zipFile.entries(); while (e.hasMoreElements()) { @@ -316,20 +320,20 @@ private void unzip(String file) { if (entry.isDirectory()) { continue; } else { - BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); + final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); int b; - byte buffer[] = new byte[BYTE_SIZE]; - FileOutputStream fos = new FileOutputStream(destinationFilePath); - BufferedOutputStream bos = new BufferedOutputStream(fos, BYTE_SIZE); - while ((b = bis.read(buffer, 0, BYTE_SIZE)) != -1) { + final byte buffer[] = new byte[Updater.BYTE_SIZE]; + final FileOutputStream fos = new FileOutputStream(destinationFilePath); + final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE); + while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) { bos.write(buffer, 0, b); } bos.flush(); bos.close(); bis.close(); - String name = destinationFilePath.getName(); - if (name.endsWith(".jar") && pluginFile(name)) { - destinationFilePath.renameTo(new File(plugin.getDataFolder().getParent(), updateFolder + "/" + name)); + final String name = destinationFilePath.getName(); + if (name.endsWith(".jar") && this.pluginFile(name)) { + destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + "/" + name)); } } entry = null; @@ -340,15 +344,15 @@ private void unzip(String file) { zipFile = null; // Move any plugin data folders that were included to the right place, Bukkit won't do this for us. - for (File dFile : new File(zipPath).listFiles()) { + for (final File dFile : new File(zipPath).listFiles()) { if (dFile.isDirectory()) { - if (pluginFile(dFile.getName())) { - File oFile = new File(plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir - File[] contents = oFile.listFiles(); // List of existing files in the current dir - for (File cFile : dFile.listFiles()) // Loop through all the files in the new dir + if (this.pluginFile(dFile.getName())) { + final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir + final File[] contents = oFile.listFiles(); // List of existing files in the current dir + for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir { boolean found = false; - for (File xFile : contents) // Loop through contents to see if it exists + for (final File xFile : contents) // Loop through contents to see if it exists { if (xFile.getName().equals(cFile.getName())) { found = true; @@ -369,9 +373,9 @@ private void unzip(String file) { } new File(zipPath).delete(); fSourceZip.delete(); - } catch (IOException ex) { - plugin.getLogger().warning("The auto-updater tried to unzip a new update file, but was unsuccessful."); - result = Updater.UpdateResult.FAIL_DOWNLOAD; + } catch (final IOException ex) { + this.plugin.getLogger().warning("The auto-updater tried to unzip a new update file, but was unsuccessful."); + this.result = Updater.UpdateResult.FAIL_DOWNLOAD; ex.printStackTrace(); } new File(file).delete(); @@ -381,7 +385,7 @@ private void unzip(String file) { * Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip. */ private boolean pluginFile(String name) { - for (File file : new File("plugins").listFiles()) { + for (final File file : new File("plugins").listFiles()) { if (file.getName().equals(name)) { return true; } @@ -393,29 +397,29 @@ private boolean pluginFile(String name) { * 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) { - if (type != UpdateType.NO_VERSION_CHECK) { - String version = plugin.getDescription().getVersion(); + if (this.type != UpdateType.NO_VERSION_CHECK) { + final String version = this.plugin.getDescription().getVersion(); if (title.split(" v").length == 2) { - String remoteVersion = title.split(" v")[1].split(" ")[0]; // Get the newest file's version number + final String remoteVersion = title.split(" v")[1].split(" ")[0]; // Get the newest file's version number int remVer = -1, curVer = 0; try { - remVer = calVer(remoteVersion); - curVer = calVer(version); - } catch (NumberFormatException nfe) { + remVer = this.calVer(remoteVersion); + curVer = this.calVer(version); + } catch (final NumberFormatException nfe) { remVer = -1; } - if (hasTag(version) || version.equalsIgnoreCase(remoteVersion) || curVer >= remVer) { + if (this.hasTag(version) || version.equalsIgnoreCase(remoteVersion) || (curVer >= remVer)) { // We already have the latest version, or this build is tagged for no-update - result = Updater.UpdateResult.NO_UPDATE; + this.result = Updater.UpdateResult.NO_UPDATE; return false; } } else { // The file's name did not contain the string 'vVersion' - String authorInfo = plugin.getDescription().getAuthors().size() == 0 ? "" : " (" + plugin.getDescription().getAuthors().get(0) + ")"; - plugin.getLogger().warning("The author of this plugin" + authorInfo + " has misconfigured their Auto Update system"); - plugin.getLogger().warning("Files uploaded to BukkitDev should contain the version number, seperated from the name by a 'v', such as PluginName v1.0"); - plugin.getLogger().warning("Please notify the author of this error."); - result = Updater.UpdateResult.FAIL_NOVERSION; + final String authorInfo = this.plugin.getDescription().getAuthors().size() == 0 ? "" : " (" + this.plugin.getDescription().getAuthors().get(0) + ")"; + this.plugin.getLogger().warning("The author of this plugin" + authorInfo + " has misconfigured their Auto Update system"); + this.plugin.getLogger().warning("Files uploaded to BukkitDev should contain the version number, seperated from the name by a 'v', such as PluginName v1.0"); + this.plugin.getLogger().warning("Please notify the author of this error."); + this.result = Updater.UpdateResult.FAIL_NOVERSION; return false; } } @@ -427,9 +431,9 @@ private boolean versionCheck(String title) { */ private Integer calVer(String s) throws NumberFormatException { if (s.contains(".")) { - StringBuilder sb = new StringBuilder(); + final StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { - Character c = s.charAt(i); + final Character c = s.charAt(i); if (Character.isLetterOrDigit(c)) { sb.append(c); } @@ -443,7 +447,7 @@ private Integer calVer(String s) throws NumberFormatException { * Evaluate whether the version number is marked showing that it should not be updated by this program */ private boolean hasTag(String version) { - for (String string : NO_UPDATE_TAG) { + for (final String string : Updater.NO_UPDATE_TAG) { if (version.contains(string)) { return true; } @@ -453,42 +457,42 @@ private boolean hasTag(String version) { private boolean read() { try { - URLConnection conn = url.openConnection(); + final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); - if (apiKey != null) { - conn.addRequestProperty("X-API-Key", apiKey); + if (this.apiKey != null) { + conn.addRequestProperty("X-API-Key", this.apiKey); } conn.addRequestProperty("User-Agent", "Updater (by Gravity)"); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String response = reader.readLine(); + final String response = reader.readLine(); - JSONArray array = (JSONArray) JSONValue.parse(response); + final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.size() == 0) { - plugin.getLogger().warning("The updater could not find any files for the project id " + id); - result = UpdateResult.FAIL_BADID; + this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id); + this.result = UpdateResult.FAIL_BADID; return false; } - versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(TITLE_VALUE); - versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(LINK_VALUE); - versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(TYPE_VALUE); - versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get(VERSION_VALUE); + this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE); + this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE); + this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE); + this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.VERSION_VALUE); return true; - } catch (IOException e) { + } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { - plugin.getLogger().warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); - plugin.getLogger().warning("Please double-check your configuration to ensure it is correct."); - result = UpdateResult.FAIL_APIKEY; + this.plugin.getLogger().warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); + this.plugin.getLogger().warning("Please double-check your configuration to ensure it is correct."); + this.result = UpdateResult.FAIL_APIKEY; } else { - plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating."); - plugin.getLogger().warning("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); - result = UpdateResult.FAIL_DBO; + this.plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating."); + this.plugin.getLogger().warning("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); + this.result = UpdateResult.FAIL_DBO; } e.printStackTrace(); return false; @@ -497,21 +501,22 @@ private boolean read() { private class UpdateRunnable implements Runnable { + @Override public void run() { - if (url != null) { + if (Updater.this.url != null) { // Obtain the results of the project's file feed - if (read()) { - if (versionCheck(versionName)) { - if (versionLink != null && type != UpdateType.NO_DOWNLOAD) { - String name = file.getName(); + if (Updater.this.read()) { + if (Updater.this.versionCheck(Updater.this.versionName)) { + if ((Updater.this.versionLink != null) && (Updater.this.type != UpdateType.NO_DOWNLOAD)) { + String name = Updater.this.file.getName(); // If it's a zip file, it shouldn't be downloaded as the plugin's name - if (versionLink.endsWith(".zip")) { - String[] split = versionLink.split("/"); + if (Updater.this.versionLink.endsWith(".zip")) { + final String[] split = Updater.this.versionLink.split("/"); name = split[split.length - 1]; } - saveFile(new File(plugin.getDataFolder().getParent(), updateFolder), name, versionLink); + Updater.this.saveFile(new File(Updater.this.plugin.getDataFolder().getParent(), Updater.this.updateFolder), name, Updater.this.versionLink); } else { - result = UpdateResult.UPDATE_AVAILABLE; + Updater.this.result = UpdateResult.UPDATE_AVAILABLE; } } }