From 3d2d60469e43cdf49b56d75795b28dd883d30928 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Sun, 15 Jan 2012 16:59:33 +0000
Subject: [PATCH]   * Plugins:     - Only stop a plugin before update if it was
 running     - Don't start a plugin after update if it was disabled     -
 Disable plugin if it fails version checks at startup

---
 .../src/net/i2p/router/web/PluginStarter.java | 33 ++++++++++++++++++-
 .../i2p/router/web/PluginUpdateHandler.java   | 23 ++++++++-----
 2 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/PluginStarter.java b/apps/routerconsole/java/src/net/i2p/router/web/PluginStarter.java
index 6be4b1cd7e..29f083bc92 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/PluginStarter.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/PluginStarter.java
@@ -95,6 +95,7 @@ public class PluginStarter implements Runnable {
         File pluginDir = new File(ctx.getConfigDir(), PluginUpdateHandler.PLUGIN_DIR + '/' + appName);
         if ((!pluginDir.exists()) || (!pluginDir.isDirectory())) {
             log.error("Cannot start nonexistent plugin: " + appName);
+            disablePlugin(appName);
             return false;
         }
 
@@ -104,6 +105,7 @@ public class PluginStarter implements Runnable {
             (new VersionComparator()).compare(CoreVersion.VERSION, minVersion) < 0) {
             String foo = "Plugin " + appName + " requires I2P version " + minVersion + " or higher";
             log.error(foo);
+            disablePlugin(appName);
             throw new Exception(foo);
         }
 
@@ -112,6 +114,7 @@ public class PluginStarter implements Runnable {
             (new VersionComparator()).compare(System.getProperty("java.version"), minVersion) < 0) {
             String foo = "Plugin " + appName + " requires Java version " + minVersion + " or higher";
             log.error(foo);
+            disablePlugin(appName);
             throw new Exception(foo);
         }
 
@@ -121,6 +124,7 @@ public class PluginStarter implements Runnable {
             (new VersionComparator()).compare(minVersion, jVersion) > 0) {
             String foo = "Plugin " + appName + " requires Jetty version " + minVersion + " or higher";
             log.error(foo);
+            disablePlugin(appName);
             throw new Exception(foo);
         }
 
@@ -129,6 +133,7 @@ public class PluginStarter implements Runnable {
             (new VersionComparator()).compare(maxVersion, jVersion) < 0) {
             String foo = "Plugin " + appName + " requires Jetty version " + maxVersion + " or lower";
             log.error(foo);
+            disablePlugin(appName);
             throw new Exception(foo);
         }
 
@@ -334,7 +339,7 @@ public class PluginStarter implements Runnable {
         Properties props = pluginProperties();
         for (Iterator iter = props.keySet().iterator(); iter.hasNext(); ) {
             String name = (String)iter.next();
-            if (name.startsWith(PREFIX + appName))
+            if (name.startsWith(PREFIX + appName + '.'))
                 iter.remove();
         }
         storePluginProperties(props);
@@ -373,6 +378,32 @@ public class PluginStarter implements Runnable {
         return rv;
     }
 
+    /**
+     *  Is the plugin enabled in plugins.config?
+     *  Default true
+     *
+     *  @since 0.8.13
+     */
+    public static boolean isPluginEnabled(String appName) {
+        Properties props = pluginProperties();
+        String prop = PREFIX + appName + ENABLED;
+        return Boolean.valueOf(props.getProperty(prop, "true")).booleanValue();
+    }
+
+    /**
+     *  Disable in plugins.config
+     *
+     *  @since 0.8.13
+     */
+    public static void disablePlugin(String appName) {
+        Properties props = pluginProperties();
+        String prop = PREFIX + appName + ENABLED;
+        if (Boolean.valueOf(props.getProperty(prop, "true")).booleanValue()) {
+            props.setProperty(prop, "false");
+            storePluginProperties(props);
+        }
+    }
+
     /**
      *  all installed plugins whether enabled or not
      */
diff --git a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java
index 511bfb0cd0..e2499d1312 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/PluginUpdateHandler.java
@@ -286,6 +286,7 @@ public class PluginUpdateHandler extends UpdateHandler {
                 return;
             }
 
+            boolean wasRunning = false;
             File destDir = new SecureDirectory(appDir, appName);
             if (destDir.exists()) {
                 if (Boolean.valueOf(props.getProperty("install-only")).booleanValue()) {
@@ -350,14 +351,16 @@ public class PluginUpdateHandler extends UpdateHandler {
                     return;
                 }
 
-                // check if it is running first?
-                try {
-                    if (!PluginStarter.stopPlugin(_context, appName)) {
-                        // failed, ignore
+                if (PluginStarter.isPluginRunning(appName, _context)) {
+                    wasRunning = true;
+                    try {
+                        if (!PluginStarter.stopPlugin(_context, appName)) {
+                            // failed, ignore
+                        }
+                    } catch (Throwable e) {
+                        // no updateStatus() for this one
+                        _log.error("Error stopping plugin " + appName, e);
                     }
-                } catch (Throwable e) {
-                    // no updateStatus() for this one
-                    _log.error("Error stopping plugin " + appName, e);
                 }
 
             } else {
@@ -390,8 +393,8 @@ public class PluginUpdateHandler extends UpdateHandler {
                     pluginProps.setProperty(PluginStarter.PREFIX + appName + PluginStarter.ENABLED, "false");
                     PluginStarter.storePluginProperties(pluginProps);
                 }
-            } else {
-                // start everything
+            } else if (wasRunning || PluginStarter.isPluginEnabled(appName)) {
+                // start everything unless it was disabled and not running before
                 try {
                     if (PluginStarter.startPlugin(_context, appName)) {
                         String linkName = ConfigClientsHelper.stripHTML(props, "consoleLinkName_" + Messages.getLanguage(_context));
@@ -411,6 +414,8 @@ public class PluginUpdateHandler extends UpdateHandler {
                     statusDone("<b>" + _("Plugin {0} installed but failed to start", appName) + ": " + e + "</b>");
                     _log.error("Error starting plugin " + appName, e);
                 }
+            } else {
+                statusDone("<b>" + _("Plugin {0} installed", appName) + "</b>");
             }
         }
 
-- 
GitLab