diff --git a/flow-server/src/main/java/com/vaadin/flow/hotswap/Hotswapper.java b/flow-server/src/main/java/com/vaadin/flow/hotswap/Hotswapper.java index 650d7e48ced..73776c031f8 100644 --- a/flow-server/src/main/java/com/vaadin/flow/hotswap/Hotswapper.java +++ b/flow-server/src/main/java/com/vaadin/flow/hotswap/Hotswapper.java @@ -46,6 +46,8 @@ import com.vaadin.flow.server.SessionDestroyListener; import com.vaadin.flow.server.SessionInitEvent; import com.vaadin.flow.server.SessionInitListener; +import com.vaadin.flow.server.UIInitEvent; +import com.vaadin.flow.server.UIInitListener; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; @@ -83,7 +85,7 @@ * @since 24.5 */ public class Hotswapper implements ServiceDestroyListener, SessionInitListener, - SessionDestroyListener { + SessionDestroyListener, UIInitListener { private static final Logger LOGGER = LoggerFactory .getLogger(Hotswapper.class); private final Set sessions = ConcurrentHashMap.newKeySet(); @@ -413,6 +415,11 @@ public void serviceDestroy(ServiceDestroyEvent event) { sessions.clear(); } + @Override + public void uiInit(UIInitEvent event) { + sessions.add(event.getUI().getSession()); + } + /** * Register the hotwsapper entry point for the given {@link VaadinService}. *

@@ -428,6 +435,7 @@ public void serviceDestroy(ServiceDestroyEvent event) { public static Optional register(VaadinService vaadinService) { if (!vaadinService.getDeploymentConfiguration().isProductionMode()) { Hotswapper hotswapper = new Hotswapper(vaadinService); + vaadinService.addUIInitListener(hotswapper); vaadinService.addSessionInitListener(hotswapper); vaadinService.addSessionDestroyListener(hotswapper); vaadinService.addServiceDestroyListener(hotswapper); diff --git a/flow-server/src/test/java/com/vaadin/flow/hotswap/HotswapperTest.java b/flow-server/src/test/java/com/vaadin/flow/hotswap/HotswapperTest.java index 75ba7339643..a17fe5dd2b5 100644 --- a/flow-server/src/test/java/com/vaadin/flow/hotswap/HotswapperTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/hotswap/HotswapperTest.java @@ -48,6 +48,7 @@ import com.vaadin.flow.server.SessionDestroyListener; import com.vaadin.flow.server.SessionInitEvent; import com.vaadin.flow.server.SessionInitListener; +import com.vaadin.flow.server.UIInitListener; import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; @@ -504,6 +505,7 @@ public void register_developmentMode_trackingListenerInstalled() { AtomicBoolean sessionInitInstalled = new AtomicBoolean(); AtomicBoolean sessionDestroyInstalled = new AtomicBoolean(); AtomicBoolean serviceDestroyInstalled = new AtomicBoolean(); + AtomicBoolean uiInitInstalled = new AtomicBoolean(); MockDeploymentConfiguration configuration = new MockDeploymentConfiguration(); configuration.setProductionMode(false); VaadinService service = new MockVaadinServletService(configuration) { @@ -527,6 +529,12 @@ public Registration addServiceDestroyListener( serviceDestroyInstalled.set(true); return super.addServiceDestroyListener(listener); } + + @Override + public Registration addUIInitListener(UIInitListener listener) { + uiInitInstalled.set(true); + return super.addUIInitListener(listener); + } }; ApplicationConfiguration appConfig = Mockito .mock(ApplicationConfiguration.class); @@ -546,6 +554,9 @@ public Registration addServiceDestroyListener( Assert.assertTrue( "Expected hotswapper ServiceDestroyListener to be registered in development mode, but was not", serviceDestroyInstalled.get()); + Assert.assertTrue( + "Expected hotswapper UIInitListener to be registered in development mode, but was not", + uiInitInstalled.get()); } @Test @@ -553,6 +564,7 @@ public void register_productionMode_trackingListenerNotInstalled() { AtomicBoolean sessionInitInstalled = new AtomicBoolean(); AtomicBoolean sessionDestroyInstalled = new AtomicBoolean(); AtomicBoolean serviceDestroyInstalled = new AtomicBoolean(); + AtomicBoolean uiInitInstalled = new AtomicBoolean(); MockDeploymentConfiguration configuration = new MockDeploymentConfiguration(); configuration.setProductionMode(true); VaadinService service = new MockVaadinServletService(configuration) { @@ -588,6 +600,9 @@ public Registration addServiceDestroyListener( Assert.assertFalse( "Expected hotswapper ServiceDestroyListener not to be registered in production mode, but it was", serviceDestroyInstalled.get()); + Assert.assertFalse( + "Expected hotswapper UIInitListener not to be registered in production mode, but it was", + uiInitInstalled.get()); } @Tag("my-route")