Skip to content

Commit

Permalink
Fixes #143, Fixes #134 (#146)
Browse files Browse the repository at this point in the history
* Fixes #143, Fixes #134

* Improve error handling for version check
  • Loading branch information
farion authored Jun 6, 2023
1 parent 3dd40aa commit 199abc8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 17 deletions.
16 changes: 12 additions & 4 deletions src/main/java/org/correomqtt/CorreoMqtt.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.correomqtt.business.dispatcher.ShutdownObserver;
import org.correomqtt.business.dispatcher.StartupDispatcher;
import org.correomqtt.business.dispatcher.StartupObserver;
import org.correomqtt.business.exception.CorreoMqttUnableToCheckVersionException;
import org.correomqtt.business.model.GlobalUISettings;
import org.correomqtt.business.model.SettingsDTO;
import org.correomqtt.business.provider.SettingsProvider;
Expand Down Expand Up @@ -73,13 +74,18 @@ public void init() throws IOException {
initUpdatesOnFirstStart(settings);
}

boolean doPluginUpdates = false;
if (settings.isSearchUpdates()) {
PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderSearchingUpdates"));
PluginCheckUtils.checkMigration();
new PluginLauncher().start();
checkForUpdates();
try {
doPluginUpdates = checkForUpdates();
} catch (CorreoMqttUnableToCheckVersionException ignored) {
}
}

PluginCheckUtils.checkMigration();
new PluginLauncher().start(doPluginUpdates);

PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderKeyring"));

KeyringHandler.getInstance().init();
Expand Down Expand Up @@ -137,11 +143,13 @@ private void setLocale(SettingsDTO settings) {
resources = ResourceBundle.getBundle("org.correomqtt.i18n", SettingsProvider.getInstance().getSettings().getCurrentLocale());
}

private void checkForUpdates() {
private boolean checkForUpdates() throws CorreoMqttUnableToCheckVersionException {
try {
CheckNewVersionUtils.checkNewVersion(false);
return true;
} catch (IOException | ParseException e) {
LOGGER.warn("Version check failed.", e);
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.correomqtt.business.exception;

public class CorreoMqttUnableToCheckVersionException extends Exception {
}
24 changes: 17 additions & 7 deletions src/main/java/org/correomqtt/business/utils/VersionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import org.apache.commons.io.IOUtils;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.correomqtt.business.exception.CorreoMqttUnableToCheckVersionException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -47,33 +50,40 @@ public static String getVersion() {
*
* @return The name of the tag on github if a new version exists, null otherwise.
*/
public static String isNewerVersionAvailable() throws IOException, ParseException {
public static String isNewerVersionAvailable() throws IOException, ParseException, CorreoMqttUnableToCheckVersionException {

URL url = new URL(GITHUB_API_LATEST);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

try {
connection.connect();
InputStream inputStream = connection.getInputStream();
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonParser.parse(
JSONObject jsonObject = (JSONObject) jsonParser.parse(
new InputStreamReader(inputStream, StandardCharsets.UTF_8));

ComparableVersion latestGithubVersion = new ComparableVersion(jsonObject.get("tag_name").toString().replaceAll("[^0-9\\.]",""));
ComparableVersion latestGithubVersion = new ComparableVersion(jsonObject.get("tag_name").toString().replaceAll("[^0-9\\.]", ""));
ComparableVersion currentLocalVersion = new ComparableVersion(getVersion());

if (latestGithubVersion.compareTo(currentLocalVersion) > 0) {
LOGGER.info("There is a new release available on github!");
LOGGER.info("There is a new release available on github! {}", GITHUB_API_LATEST);
return jsonObject.get("tag_name").toString();
} else {
LOGGER.info("Version is up to date or newer!");
LOGGER.info("Version is up to date or newer! {}", GITHUB_API_LATEST);
return null;
}
} catch (FileNotFoundException fnfe){
LOGGER.warn("Unable to find {} while checking for new version. Plugin updates will also be skipped.", GITHUB_API_LATEST);
} catch (SocketTimeoutException ste){
LOGGER.warn("Timeout checking for new version. Plugin updates will also be skipped.");
} catch (UnknownHostException uhe) {
LOGGER.error("No internet connection for checking latest version");
return null;
LOGGER.warn("No internet connection for checking latest version. Plugin updates will also be skipped.");
}
throw new CorreoMqttUnableToCheckVersionException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.correomqtt.business.dispatcher.ConfigDispatcher;
import org.correomqtt.business.dispatcher.ConfigObserver;
import org.correomqtt.business.dispatcher.ShutdownDispatcher;
import org.correomqtt.business.exception.CorreoMqttUnableToCheckVersionException;
import org.correomqtt.business.provider.SettingsProvider;
import org.correomqtt.business.provider.PersistPublishHistoryProvider;
import org.correomqtt.business.provider.PersistPublishMessageHistoryProvider;
Expand Down Expand Up @@ -144,8 +145,8 @@ private void setMenuEventHandler() {
updateItem.setOnAction(event -> {
try {
CheckNewVersionUtils.checkNewVersion(true);
} catch (IOException | ParseException e) {
LOGGER.warn("Exception checking version", e);
} catch (IOException | ParseException | CorreoMqttUnableToCheckVersionException e) {
LOGGER.warn("Exception checking version", e); //TODO UI?
}
});
websiteItem.setOnAction(event -> HostServicesHolder.getInstance().getHostServices().showDocument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.correomqtt.gui.utils;

import org.correomqtt.business.exception.CorreoMqttUnableToCheckVersionException;
import org.correomqtt.business.provider.SettingsProvider;
import org.correomqtt.business.utils.VersionUtils;
import org.correomqtt.gui.helper.AlertHelper;
Expand All @@ -17,7 +18,7 @@ private CheckNewVersionUtils() {
// nothing to do
}

public static void checkNewVersion(boolean showHintIfUpToDate) throws IOException, ParseException {
public static void checkNewVersion(boolean showHintIfUpToDate) throws IOException, ParseException, CorreoMqttUnableToCheckVersionException {

String newVersion = VersionUtils.isNewerVersionAvailable();
if (newVersion != null) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/correomqtt/plugin/PluginLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ public class PluginLauncher {

private ResourceBundle resources = ResourceBundle.getBundle("org.correomqtt.i18n", SettingsProvider.getInstance().getSettings().getCurrentLocale());

public void start() throws IOException {
public void start(boolean doPluginUpdates) throws IOException {

PluginManager pluginManager = PluginManager.getInstance();

try {
PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderLoadPlugins"));
pluginManager.loadPlugins();
PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderUpdatePlugins"));
updateSystem();
if(doPluginUpdates) {
PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderUpdatePlugins"));
updateSystem();
}
PreloadingDispatcher.getInstance().onProgress(resources.getString("preloaderStartPlugins"));
pluginManager.startPlugins();
} catch (Exception e) {
Expand Down

0 comments on commit 199abc8

Please sign in to comment.