]> source.dussan.org Git - pf4j.git/commitdiff
Fix for #394
authorDecebal Suiu <decebal.suiu@gmail.com>
Fri, 8 Jan 2021 20:20:17 +0000 (22:20 +0200)
committerDecebal Suiu <decebal.suiu@gmail.com>
Fri, 8 Jan 2021 20:20:17 +0000 (22:20 +0200)
pf4j/src/main/java/org/pf4j/DependencyResolver.java
pf4j/src/test/java/org/pf4j/PluginDependencyTest.java
pf4j/src/test/java/org/pf4j/plugin/PluginZip.java

index 866eb9359bbdd36ea5834b48cf1fb1be1aec2165..7960a464805653bee93651917a8f3a8c8356c06a 100644 (file)
@@ -88,7 +88,7 @@ public class DependencyResolver {
             String pluginId = plugin.getPluginId();
             String existingVersion = plugin.getVersion();
 
-            List<String> dependents = new ArrayList<>(getDependents(pluginId));
+            List<String> dependents = getDependents(pluginId);
             while (!dependents.isEmpty()) {
                 String dependentId = dependents.remove(0);
                 PluginDescriptor dependent = pluginByIds.get(dependentId);
@@ -107,22 +107,22 @@ public class DependencyResolver {
      * Retrieves the plugins ids that the given plugin id directly depends on.
      *
      * @param pluginId the unique plugin identifier, specified in its metadata
-     * @return
+     * @return an immutable list of dependencies (new list for each call)
      */
     public List<String> getDependencies(String pluginId) {
         checkResolved();
-        return dependenciesGraph.getNeighbors(pluginId);
+        return new ArrayList<>(dependenciesGraph.getNeighbors(pluginId));
     }
 
     /**
      * Retrieves the plugins ids that the given content is a direct dependency of.
      *
      * @param pluginId the unique plugin identifier, specified in its metadata
-     * @return
+     * @return an immutable list of dependents (new list for each call)
      */
     public List<String> getDependents(String pluginId) {
         checkResolved();
-        return dependentsGraph.getNeighbors(pluginId);
+        return new ArrayList<>(dependentsGraph.getNeighbors(pluginId));
     }
 
     /**
index c605d237e44e103b0f79388eef8cefbb6d1934d5..a596cccaf66d2ac7aba70fa01520a246a1d23396 100644 (file)
 package org.pf4j;
 
 
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.pf4j.plugin.PluginZip;
+
+import java.nio.file.Path;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -27,6 +32,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
  */
 public class PluginDependencyTest {
 
+    private DefaultPluginManager pluginManager;
+
+    @TempDir
+    Path pluginsPath;
+
+    @BeforeEach
+    public void setUp() {
+        pluginManager = new DefaultPluginManager(pluginsPath);
+    }
+
     /**
      * Test of getPluginId method, of class PluginDependency.
      */
@@ -77,4 +92,33 @@ public class PluginDependencyTest {
         assertEquals("PluginDependency [pluginId=test, pluginVersionSupport=1.0, optional=true]", instance.toString());
     }
 
+    @Test
+    public void dependentStop() throws Exception {
+        // B depends on A
+        PluginZip pluginA = new PluginZip.Builder(pluginsPath.resolve("A-plugin-1.2.3.zip"), "plugin.a")
+            .pluginVersion("1.2.3").build();
+
+        PluginZip pluginB = new PluginZip.Builder(pluginsPath.resolve("B-plugin-1.2.3.zip"), "plugin.b")
+            .pluginDependencies("plugin.a")
+            .pluginVersion("1.2.3").build();
+
+        pluginManager.loadPlugins();
+        assertEquals(2, pluginManager.getPlugins().size());
+
+        pluginManager.startPlugins();
+        assertEquals(2, pluginManager.getStartedPlugins().size());
+
+        // stop A, both A and B should be stopped
+        pluginManager.stopPlugin("plugin.a");
+        assertEquals(0, pluginManager.getStartedPlugins().size());
+
+        // start all again
+        pluginManager.startPlugins();
+        assertEquals(2, pluginManager.getStartedPlugins().size());
+
+        // dependent info should be kept. #394
+        pluginManager.stopPlugin("plugin.a");
+        assertEquals(0, pluginManager.getStartedPlugins().size());
+    }
+
 }
index c2da257afcbb589b1b9d8d62f3069a8d3fb42a77..0748ffffdb86206ab751c4c4389faae020e38c96 100644 (file)
@@ -39,12 +39,14 @@ public class PluginZip {
     private final String pluginId;
     private final String pluginClass;
     private final String pluginVersion;
+    private final String pluginDependencies;
 
     protected PluginZip(Builder builder) {
         this.path = builder.path;
         this.pluginId = builder.pluginId;
         this.pluginClass = builder.pluginClass;
         this.pluginVersion = builder.pluginVersion;
+        this.pluginDependencies = builder.pluginDependencies;
     }
 
     public Path path() {
@@ -67,6 +69,8 @@ public class PluginZip {
         return pluginVersion;
     }
 
+    public String pluginDependencies() { return pluginDependencies; }
+
     public Path unzippedPath() {
         Path path = path();
         String fileName = path.getFileName().toString();
@@ -88,6 +92,7 @@ public class PluginZip {
 
         private String pluginClass;
         private String pluginVersion;
+        private String pluginDependencies;
         private Map<String, String> properties = new LinkedHashMap<>();
         private Map<Path, byte[]> files = new LinkedHashMap<>();
 
@@ -108,6 +113,12 @@ public class PluginZip {
             return this;
         }
 
+        public Builder pluginDependencies(String pluginDependencies) {
+            this.pluginDependencies = pluginDependencies;
+
+            return this;
+        }
+
         /**
          * Add extra properties to the {@code properties} file.
          * As possible attribute name please see {@link PropertiesPluginDescriptorFinder}.
@@ -162,6 +173,9 @@ public class PluginZip {
             Map<String, String> map = new LinkedHashMap<>();
             map.put(PropertiesPluginDescriptorFinder.PLUGIN_ID, pluginId);
             map.put(PropertiesPluginDescriptorFinder.PLUGIN_VERSION, pluginVersion);
+            if (pluginDependencies != null) {
+                map.put(PropertiesPluginDescriptorFinder.PLUGIN_DEPENDENCIES, pluginDependencies);
+            }
             if (pluginClass != null) {
                 map.put(PropertiesPluginDescriptorFinder.PLUGIN_CLASS, pluginClass);
             }