From 726be6ba42313a9c2093f9af199034f34eaeb3cf Mon Sep 17 00:00:00 2001 From: Matthew Horridge Date: Wed, 18 May 2016 12:49:59 -0700 Subject: [PATCH] Closes #179 --- .../core/ui/action/CheckPluginsAction.java | 19 ++++++- .../ui/action/start/CheckPluginsAction.java | 11 +++- .../editor/core/update/PluginManager.java | 53 ++++++++++++------- .../core/update/PluginRegistryImpl.java | 5 +- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/CheckPluginsAction.java b/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/CheckPluginsAction.java index 5376c6a7b..2d5d84752 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/CheckPluginsAction.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/CheckPluginsAction.java @@ -2,7 +2,10 @@ import org.protege.editor.core.update.PluginManager; +import javax.swing.*; import java.awt.event.ActionEvent; +import java.io.IOException; +import java.net.UnknownHostException; /* * Copyright (C) 2007, University of Manchester * @@ -22,7 +25,21 @@ public class CheckPluginsAction extends ProtegeAction { public void actionPerformed(ActionEvent event) { - PluginManager.getInstance().runCheckForPlugins(); + try { + PluginManager.getInstance().runCheckForPlugins(); + } catch (UnknownHostException e) { + JOptionPane.showMessageDialog(getWorkspace(), + "" + + "Protege could not connect to the plugin registry.

" + + "Please check your internet connection and try again." + + "", "Unable to check for plugins", JOptionPane.ERROR_MESSAGE); + } catch (IOException e) { + JOptionPane.showMessageDialog(getWorkspace(), + "" + + "Protege could not connect to the plugin registry.

" + + "Reason: " + e.getMessage() + + "", "Unable to check for plugins", JOptionPane.ERROR_MESSAGE); + } } diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/start/CheckPluginsAction.java b/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/start/CheckPluginsAction.java index 1136a27ff..6012f17e0 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/start/CheckPluginsAction.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/ui/action/start/CheckPluginsAction.java @@ -1,8 +1,11 @@ package org.protege.editor.core.ui.action.start; import org.protege.editor.core.update.PluginManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.awt.event.ActionEvent; +import java.io.IOException; /** * Author: drummond
@@ -13,11 +16,15 @@ * Date: Nov 6, 2008

*/ public class CheckPluginsAction extends AltStartupAction { - private static final long serialVersionUID = 1531046176436352912L; + private static final Logger logger = LoggerFactory.getLogger(CheckPluginsAction.class); public void actionPerformed(ActionEvent event) { - PluginManager.getInstance().runCheckForPlugins(); + try { + PluginManager.getInstance().runCheckForPlugins(); + } catch (IOException e) { + logger.warn("A problem occurred whilst checking for plugin updates: {}", e.getMessage()); + } } diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginManager.java b/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginManager.java index abb5a21ab..32a7c8c88 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginManager.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginManager.java @@ -4,8 +4,12 @@ import org.protege.editor.core.prefs.Preferences; import org.protege.editor.core.prefs.PreferencesManager; import org.protege.editor.core.ui.progress.BackgroundTask; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.swing.*; +import java.io.IOException; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Date; @@ -17,7 +21,7 @@ * The University Of Manchester
* Medical Informatics Group
* Date: 25-Aug-2006

- + *

* matthew.horridge@cs.man.ac.uk
* www.cs.man.ac.uk/~horridgm

*/ @@ -33,6 +37,8 @@ public class PluginManager { public static final String DEFAULT_REGISTRY = "https://raw.githubusercontent.com/protegeproject/autoupdate/master/update-info/5.0.0/plugins.repository"; + private final Logger logger = LoggerFactory.getLogger(PluginManager.class); + private static enum SearchType { UPDATES_ONLY, UPDATES_AND_INSTALLS @@ -65,26 +71,26 @@ public boolean isAutoUpdateEnabled() { public URL getPluginRegistryLocation() { - String pluginRegistryLoc = getPrefs().getString(PLUGIN_REGISTRY_KEY, DEFAULT_REGISTRY); + String pluginRegistryLoc = getPrefs().getString(PLUGIN_REGISTRY_KEY, DEFAULT_REGISTRY); try { return new URL(pluginRegistryLoc); - } - catch (MalformedURLException e) { + } catch (MalformedURLException e) { throw new RuntimeException(e); } } public void setPluginRegistryLocation(URL url) { - String oldPluginRegistryLoc = getPrefs().getString(PLUGIN_REGISTRY_KEY, DEFAULT_REGISTRY); - String newPluginRegistryLoc = url.toString(); - if (!newPluginRegistryLoc.equals(oldPluginRegistryLoc)) { - getPrefs().putString(PLUGIN_REGISTRY_KEY, newPluginRegistryLoc); - } + String oldPluginRegistryLoc = getPrefs().getString(PLUGIN_REGISTRY_KEY, DEFAULT_REGISTRY); + String newPluginRegistryLoc = url.toString(); + if (!newPluginRegistryLoc.equals(oldPluginRegistryLoc)) { + getPrefs().putString(PLUGIN_REGISTRY_KEY, newPluginRegistryLoc); + } } /** * Gets the date that auto-update was last run. + * * @return The date which auto-update was last run. Not {@code null}. */ public Date getLastAutoUpdateDate() { @@ -96,10 +102,18 @@ public void runAutoUpdate() { runSearch(SearchType.UPDATES_ONLY); } - public void runCheckForPlugins() { + public void runCheckForPlugins() throws IOException { + ensureConnectionToPluginRegistry(); runSearch(SearchType.UPDATES_AND_INSTALLS); } + private void ensureConnectionToPluginRegistry() throws IOException { + URL registryLocation = getPluginRegistryLocation(); + HttpURLConnection connection = (HttpURLConnection) registryLocation.openConnection(); + connection.setRequestMethod("HEAD"); + connection.getResponseCode(); + } + private void runSearch(SearchType searchType) { final BackgroundTask autoUpdateTask = ProtegeApplication.getBackgroundTaskManager().startTask("autoupdate"); Runnable runnable = () -> { @@ -107,15 +121,14 @@ private void runSearch(SearchType searchType) { try { registry.reload(); getPrefs().putLong(LAST_RUN_PREFS_KEY, System.currentTimeMillis()); - } - finally { + } finally { ProtegeApplication.getBackgroundTaskManager().endTask(autoUpdateTask); List availablePlugins = registry.getAvailablePlugins(); if (searchType == SearchType.UPDATES_AND_INSTALLS) { showPluginsDialog(availablePlugins); } else { - if(!availablePlugins.isEmpty()) { + if (!availablePlugins.isEmpty()) { showPluginsDialog(availablePlugins); } } @@ -127,13 +140,13 @@ private void runSearch(SearchType searchType) { } private void showPluginsDialog(List pluginInfoList) { - SwingUtilities.invokeLater(() -> { - List selUpdates = PluginPanel.showDialog(pluginInfoList, null); - if (!selUpdates.isEmpty()){ - PluginInstaller installer = new PluginInstaller(selUpdates); - installer.run(); - } - }); + SwingUtilities.invokeLater(() -> { + List selUpdates = PluginPanel.showDialog(pluginInfoList, null); + if (!selUpdates.isEmpty()) { + PluginInstaller installer = new PluginInstaller(selUpdates); + installer.run(); + } + }); } } diff --git a/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginRegistryImpl.java b/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginRegistryImpl.java index 45d162d24..36bb74b5e 100644 --- a/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginRegistryImpl.java +++ b/protege-editor-core/src/main/java/org/protege/editor/core/update/PluginRegistryImpl.java @@ -16,6 +16,7 @@ import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; +import java.net.UnknownHostException; import java.util.*; /* * Copyright (C) 2007, University of Manchester @@ -175,8 +176,10 @@ private void readRegistry(URL node, int depth) { } } reader.close(); + } catch (UnknownHostException ex) { + logger.info(AUTO_UPDATE, "{} Cannot open remote plugin registry at {} (Unknown Host)", pad(depth), ex.getMessage()); } catch (IOException ex) { - logger.warn(AUTO_UPDATE, "{} Cannot open remote plugin registry at {}. Reason: {}", pad(depth), ex.getMessage(), ex); + logger.info(AUTO_UPDATE, "{} Cannot read plugin registry at {}. Reason: {}", pad(depth), node, ex.getMessage()); } } }