Fix errors when starting server offline

- Updater would throw NPE and UnknownHostException when it could not connect to projectkorra.com

- Also cleaned up output of DB connections
This commit is contained in:
kingbirdy 2015-08-19 00:36:06 -04:00
parent 18ab899f27
commit 7cacdf82c2
2 changed files with 28 additions and 19 deletions

View file

@ -16,7 +16,7 @@ public class DBConnection {
public static void init() { public static void init() {
if (ProjectKorra.plugin.getConfig().getString("Storage.engine").equalsIgnoreCase("mysql")) { if (ProjectKorra.plugin.getConfig().getString("Storage.engine").equalsIgnoreCase("mysql")) {
sql = new MySQL(ProjectKorra.log, "[ProjectKorra] Establishing MySQL Connection...", host, port, user, pass, db); sql = new MySQL(ProjectKorra.log, "Establishing MySQL Connection...", host, port, user, pass, db);
if (((MySQL) sql).open() == null) { if (((MySQL) sql).open() == null) {
ProjectKorra.log.severe("Disabling due to database error"); ProjectKorra.log.severe("Disabling due to database error");
GeneralMethods.stopPlugin(); GeneralMethods.stopPlugin();
@ -24,7 +24,7 @@ public class DBConnection {
} }
isOpen = true; isOpen = true;
ProjectKorra.log.info("[ProjectKorra] Database connection established."); ProjectKorra.log.info("Database connection established.");
if (!sql.tableExists("pk_players")) { if (!sql.tableExists("pk_players")) {
ProjectKorra.log.info("Creating pk_players table"); ProjectKorra.log.info("Creating pk_players table");
@ -38,7 +38,7 @@ public class DBConnection {
sql.modifyQuery(query); sql.modifyQuery(query);
} }
} else { } else {
sql = new SQLite(ProjectKorra.log, "[ProjectKorra] Establishing SQLite Connection.", "projectkorra.db", ProjectKorra.plugin.getDataFolder().getAbsolutePath()); sql = new SQLite(ProjectKorra.log, "", "projectkorra.db", ProjectKorra.plugin.getDataFolder().getAbsolutePath());
if (((SQLite) sql).open() == null) { if (((SQLite) sql).open() == null) {
ProjectKorra.log.severe("Disabling due to database error"); ProjectKorra.log.severe("Disabling due to database error");
GeneralMethods.stopPlugin(); GeneralMethods.stopPlugin();

View file

@ -1,18 +1,19 @@
package com.projectkorra.projectkorra.util; package com.projectkorra.projectkorra.util;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/** /**
* Updater class that takes an rss feed and checks for updates there * Updater class that takes an rss feed and checks for updates there
* <br> * <br>
@ -57,6 +58,8 @@ public class Updater {
urlc = url.openConnection(); urlc = url.openConnection();
urlc.setRequestProperty("User-Agent", ""); // Must be used or face 403 urlc.setRequestProperty("User-Agent", ""); // Must be used or face 403
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(urlc.getInputStream()); document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(urlc.getInputStream());
} catch (UnknownHostException e) {
plugin.getLogger().info("Could not connect to ProjectKorra.com to check for updates");
} catch (IOException | SAXException | ParserConfigurationException e) { } catch (IOException | SAXException | ParserConfigurationException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -70,7 +73,9 @@ public class Updater {
* *
*/ */
public void checkUpdate() { public void checkUpdate() {
if (updateAvailable()) { if (getUpdateVersion()==null)
return;
else if (updateAvailable()) {
plugin.getLogger().info("===================[Update Available]==================="); plugin.getLogger().info("===================[Update Available]===================");
plugin.getLogger().info("You are running version " + getCurrentVersion()); plugin.getLogger().info("You are running version " + getCurrentVersion());
plugin.getLogger().info("The latest version avaliable is " + getUpdateVersion()); plugin.getLogger().info("The latest version avaliable is " + getUpdateVersion());
@ -82,15 +87,18 @@ public class Updater {
/** /**
* Gets latest plugin version. * Gets latest plugin version.
* *
* @return Latest plugin version * @return Latest plugin version, or null if it cannot connect
*/ */
public String getUpdateVersion() { public String getUpdateVersion() {
if (document!=null) {
Node latestFile = document.getElementsByTagName("item").item(0); Node latestFile = document.getElementsByTagName("item").item(0);
NodeList children = latestFile.getChildNodes(); NodeList children = latestFile.getChildNodes();
String version = children.item(1).getTextContent(); String version = children.item(1).getTextContent();
return version; return version;
} }
return null;
}
/** /**
* Checks to see if an update is available. * Checks to see if an update is available.
@ -98,7 +106,8 @@ public class Updater {
* @return true If there is an update * @return true If there is an update
*/ */
public boolean updateAvailable() { public boolean updateAvailable() {
if (currentVersion.equalsIgnoreCase(getUpdateVersion())) { String updateVersion = getUpdateVersion();
if (updateVersion==null || currentVersion.equalsIgnoreCase(getUpdateVersion())) {
return false; return false;
} }
return true; return true;