]> source.dussan.org Git - pf4j.git/commitdiff
Post `PluginState.UNLOADED` event when plugins are unloaded (#567)
authorSebastian Lövdahl <slovdahl@hibox.fi>
Tue, 20 Feb 2024 07:16:09 +0000 (09:16 +0200)
committerGitHub <noreply@github.com>
Tue, 20 Feb 2024 07:16:09 +0000 (09:16 +0200)
pf4j/src/main/java/org/pf4j/AbstractPluginManager.java
pf4j/src/main/java/org/pf4j/PluginState.java
pf4j/src/test/java/org/pf4j/DefaultPluginManagerTest.java

index f3a723127c19e0c3f736e467e80c1d5bcb065b8e..78c261c118ecbfd871fcc94198a8ba583f016ea9 100644 (file)
@@ -311,6 +311,7 @@ public abstract class AbstractPluginManager implements PluginManager {
         }
 
         // remove the plugin
+        pluginWrapper.setPluginState(PluginState.UNLOADED);
         plugins.remove(pluginId);
         getResolvedPlugins().remove(pluginWrapper);
         getUnresolvedPlugins().remove(pluginWrapper);
index 989157fa0557885aa9844ad1c729b58d683ac4fe..6a6c1f9ef03b061c7eb42d8cbee1a843bc02cf3a 100644 (file)
@@ -20,7 +20,7 @@ package org.pf4j;
  * <p>
  * Lifecycle of a plugin:
  * <pre>
- * CREATED -> RESOLVED -> STARTED -> STOPPED
+ * CREATED -> RESOLVED -> STARTED -> STOPPED -> UNLOADED
  * CREATED -> DISABLED
  * CREATED -> FAILED
  *
@@ -57,7 +57,14 @@ public enum PluginState {
     /**
      * Plugin failed to start.
      */
-    FAILED("FAILED");
+    FAILED("FAILED"),
+
+    /**
+     * The plugin has been unloaded. After this event has been completed, the plugin's
+     * {@link ClassLoader} will be closed.
+     */
+    UNLOADED("UNLOADED"),
+    ;
 
     private final String status;
 
index 2e78ad2714129b9c63be5399dcbea9fd816c705d..d95a2d08e1f439857a2f4c414480b082a646b520 100644 (file)
@@ -25,6 +25,9 @@ import org.pf4j.test.PluginZip;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -40,12 +43,17 @@ public class DefaultPluginManagerTest {
     private DefaultPluginDescriptor pluginDescriptor;
     private PluginWrapper pluginWrapper;
 
+    private List<PluginStateEvent> receivedEvents;
+
     @TempDir
     Path pluginsPath;
 
     @BeforeEach
     public void setUp() throws IOException {
+        receivedEvents = new ArrayList<>();
+
         pluginManager = new DefaultPluginManager(pluginsPath);
+        pluginManager.addPluginStateListener(event -> receivedEvents.add(event));
 
         pluginDescriptor = new DefaultPluginDescriptor();
         pluginDescriptor.setPluginId("myPlugin");
@@ -63,6 +71,7 @@ public class DefaultPluginManagerTest {
         pluginManager = null;
         pluginDescriptor = null;
         pluginWrapper = null;
+        receivedEvents = null;
     }
 
     @Test
@@ -140,6 +149,7 @@ public class DefaultPluginManagerTest {
 
         PluginManager pluginManager = new DefaultPluginManager(pluginsPath) {
 
+            @Override
             protected PluginStatusProvider createPluginStatusProvider() {
                 return statusProvider;
             }
@@ -171,6 +181,12 @@ public class DefaultPluginManagerTest {
         assertTrue(deleted);
 
         assertFalse(pluginZip.file().exists());
+
+        Optional<PluginStateEvent> unloadedEvent = receivedEvents.stream()
+            .filter(event -> event.getPluginState() == PluginState.UNLOADED)
+            .findFirst();
+
+        assertTrue(unloadedEvent.isPresent());
     }
 
     @Test
@@ -188,6 +204,12 @@ public class DefaultPluginManagerTest {
         assertTrue(deleted);
 
         assertFalse(pluginJar.file().exists());
+
+        Optional<PluginStateEvent> unloadedEvent = receivedEvents.stream()
+            .filter(event -> event.getPluginState() == PluginState.UNLOADED)
+            .findFirst();
+
+        assertTrue(unloadedEvent.isPresent());
     }
 
     @Test
@@ -310,4 +332,25 @@ public class DefaultPluginManagerTest {
         assertThrows(DependencyResolver.CyclicDependencyException.class, () -> pluginManager.loadPlugins());
     }
 
+    @Test
+    public void deleteZipPluginForPluginThatHasNotBeenStartedPostsUnloadedEvent() throws Exception {
+        PluginZip pluginZip = new PluginZip.Builder(pluginsPath.resolve("my-plugin-1.2.3.zip"), "myPlugin")
+            .pluginVersion("1.2.3")
+            .build();
+
+        pluginManager.loadPlugin(pluginZip.path());
+
+        assertEquals(1, pluginManager.getPlugins().size());
+
+        boolean deleted = pluginManager.deletePlugin(pluginZip.pluginId());
+        assertTrue(deleted);
+
+        assertFalse(pluginZip.file().exists());
+
+        Optional<PluginStateEvent> unloadedEvent = receivedEvents.stream()
+            .filter(event -> event.getPluginState() == PluginState.UNLOADED)
+            .findFirst();
+
+        assertTrue(unloadedEvent.isPresent());
+    }
 }