From a0d42e7ec723daf650e29f1c37a4ea7913c21bb6 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 14 May 2024 18:47:04 +0300 Subject: [PATCH] chore: Delete polling based hotswap listener It currently is enabled for Flow applications and logs a lot for no good reason. It, and Hotswapper, will be replaced by a common solution for Flow+Hilla --- .../hotswap/EndpointHotSwapListener.java | 66 -------- .../hotswap/EndpointHotSwapService.java | 62 -------- .../hotswap/HotSwapConfiguration.java | 55 ------- .../HotSwapConfigurationProperties.java | 29 ---- .../internal/hotswap/HotSwapListener.java | 41 ----- .../hotswap/HotSwapServiceInitializer.java | 89 ----------- .../PollChangedEndpointsHotSwapService.java | 141 ------------------ 7 files changed, 483 deletions(-) delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapListener.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapService.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfiguration.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfigurationProperties.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapListener.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapServiceInitializer.java delete mode 100644 packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/PollChangedEndpointsHotSwapService.java diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapListener.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapListener.java deleted file mode 100644 index 153e7bb43e..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapListener.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import jakarta.annotation.PostConstruct; - -import java.io.IOException; -import java.util.Optional; - -import com.vaadin.hilla.EndpointCodeGenerator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.vaadin.flow.internal.BrowserLiveReload; - -class EndpointHotSwapListener implements HotSwapListener { - - private static final Logger LOGGER = LoggerFactory - .getLogger(EndpointHotSwapListener.class); - - private final EndpointHotSwapService endpointHotSwapService; - - public EndpointHotSwapListener( - EndpointHotSwapService endpointHotSwapService) { - this.endpointHotSwapService = endpointHotSwapService; - } - - @PostConstruct - private void registerForHotSwapChanges() { - endpointHotSwapService.addHotSwapListener(this); - } - - @Override - public void endpointChanged(EndpointChangedEvent event) { - try { - EndpointCodeGenerator.getInstance().update(); - } catch (IOException e) { - throw new RuntimeException(e); - } - - reload(event.browserLiveReload()); - } - - private void reload(BrowserLiveReload browserLiveReload) { - - Optional.ofNullable(browserLiveReload).ifPresent(liveReload -> { - LOGGER.debug("Reloading the browser after endpoint(s) changes..."); - liveReload.reload(); - }); - } - -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapService.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapService.java deleted file mode 100644 index a81fc640b1..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/EndpointHotSwapService.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import com.vaadin.flow.internal.BrowserLiveReload; -import com.vaadin.flow.shared.Registration; - -import java.nio.file.Path; - -/** - * The service interface that defines the API for EndpointHotSwapService - * implementations. - */ -public interface EndpointHotSwapService { - - /** - * The method that starts the process for monitoring the changes in - * endpoints. The strategy for detecting the changes can be Polling, - * Watching the File System changes, etc. No matter what the strategy for - * detecting the changes is, instances of {@code EndpointChangedEvent} - * should be fired upon detecting changes in the endpoints and associated - * classes. - * - * @param buildDir - * the path to project's build directory which is 'target' in - * standard Maven projects and 'build' in standard Gradle - * projects. - * @param browserLiveReload - * the BrowserLiveReload object which is used to reload the - * browser after any endpoint changes are detected. - * - * @see EndpointHotSwapService#addHotSwapListener(HotSwapListener) - */ - void monitorChanges(Path buildDir, BrowserLiveReload browserLiveReload); - - /** - * The method that enables registering {@code HotSwapListener}s to be - * notified whenever an {@code EndpointChangedEvent} is fired. - * - * @param listener - * an instance of HotSwapListener that implements the - * {@code endpointChanged} method that will be called when an - * {@code EndpointChangedEvent} is fired. - * @return the Listener Registration object that enables the cally to - * deregister the listener. - */ - Registration addHotSwapListener(HotSwapListener listener); -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfiguration.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfiguration.java deleted file mode 100644 index e3952c2e92..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfiguration.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import com.vaadin.hilla.EndpointCodeGenerator; -import com.vaadin.hilla.Hotswapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class HotSwapConfiguration { - - @Bean - @ConfigurationProperties(prefix = "hilla.endpoint.hot-reload") - public HotSwapConfigurationProperties hotSwapConfigurationProperties() { - return new HotSwapConfigurationProperties(); - } - - @Bean - EndpointHotSwapService hotSwapWatchService( - @Autowired HotSwapConfigurationProperties configurationProperties) { - return new PollChangedEndpointsHotSwapService( - configurationProperties.getPollInterval()); - } - - @Bean - HotSwapServiceInitializer hotSwapServiceInitializer( - @Autowired EndpointHotSwapService endpointHotSwapService, - @Autowired HotSwapConfigurationProperties configurationProperties) { - return new HotSwapServiceInitializer(endpointHotSwapService, - configurationProperties.isEnabled() && !Hotswapper.isInUse()); - } - - @Bean - EndpointHotSwapListener endpointHotSwapListener( - @Autowired EndpointHotSwapService endpointHotSwapService) { - return new EndpointHotSwapListener(endpointHotSwapService); - } -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfigurationProperties.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfigurationProperties.java deleted file mode 100644 index dbca5ae25e..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapConfigurationProperties.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.vaadin.hilla.internal.hotswap; - -public class HotSwapConfigurationProperties { - /** - * Whether the endpoint changes hot-reload is enabled or not. - */ - private boolean enabled = true; - - /** - * The interval to poll for endpoint changes in seconds. - */ - private int pollInterval = 5; - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public int getPollInterval() { - return pollInterval; - } - - public void setPollInterval(int pollInterval) { - this.pollInterval = pollInterval; - } -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapListener.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapListener.java deleted file mode 100644 index ab8b24ad33..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapListener.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import com.vaadin.flow.internal.BrowserLiveReload; - -import java.nio.file.Path; - -/** - * The interface that defines the contract for HotSwapListeners. - */ -public interface HotSwapListener { - - /** - * The event object that is passed to {@code endpointChanged} method. - * @param buildDir - * @param browserLiveReload - */ - record EndpointChangedEvent(Path buildDir, BrowserLiveReload browserLiveReload) { - } - - /** - * The method that is called when an {@code EndpointChangedEvent} is fired. - */ - void endpointChanged(EndpointChangedEvent event); - -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapServiceInitializer.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapServiceInitializer.java deleted file mode 100644 index a7360965a6..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/HotSwapServiceInitializer.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import com.vaadin.flow.internal.BrowserLiveReload; -import com.vaadin.flow.internal.BrowserLiveReloadAccessor; -import com.vaadin.flow.server.ServiceInitEvent; -import com.vaadin.flow.server.VaadinService; -import com.vaadin.flow.server.VaadinServiceInitListener; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.file.Path; - -class HotSwapServiceInitializer implements VaadinServiceInitListener { - - private final Logger LOGGER; - - boolean endpointHotReloadEnabled; - - private final EndpointHotSwapService endpointHotSwapService; - - public HotSwapServiceInitializer( - EndpointHotSwapService endpointHotSwapService, - boolean endpointHotReloadEnabled) { - this.endpointHotSwapService = endpointHotSwapService; - this.endpointHotReloadEnabled = endpointHotReloadEnabled; - LOGGER = LoggerFactory.getLogger(this.getClass()); - } - - @Override - public void serviceInit(ServiceInitEvent serviceInitEvent) { - VaadinService vaadinService = serviceInitEvent.getSource(); - BrowserLiveReloadAccessor.getLiveReloadFromService(vaadinService) - .ifPresent(browserLiveReload -> { - if (browserLiveReload.getBackend() != null - && BrowserLiveReload.Backend.SPRING_BOOT_DEVTOOLS != browserLiveReload - .getBackend() - && isDevModeLiveReloadEnabled(vaadinService)) { - if (isEndpointHotReloadEnabled()) { - endpointHotSwapService.monitorChanges( - getBuildDir(vaadinService), - browserLiveReload); - info("Hilla Endpoint Hot-Reload service is enabled. " - + "You can disable it by defining the hilla.endpoint.hot-reload.enabled=false property in application.properties file."); - info("The default polling interval for Hilla Endpoint Hot-Reload is 5 seconds. " - + "You can change the interval by defining a positive value for the hilla.endpoint.hot-reload.pollInterval property in application.properties file."); - } else { - info("Hilla Endpoint Hot-Reload service is disabled. " - + "You can enable it by removing the hilla.endpoint.hot-reload.enabled=true property from your application.properties file, " - + "or by setting its value to true."); - } - } - }); - } - - private boolean isDevModeLiveReloadEnabled(VaadinService vaadinService) { - return vaadinService.getDeploymentConfiguration() - .isDevModeLiveReloadEnabled(); - } - - private Path getBuildDir(VaadinService vaadinService) { - var deploymentConfig = vaadinService.getDeploymentConfiguration(); - var projectFolder = deploymentConfig.getProjectFolder().toPath(); - return projectFolder.resolve(deploymentConfig.getBuildFolder()); - } - - private boolean isEndpointHotReloadEnabled() { - return endpointHotReloadEnabled; - } - - private void info(String message) { - LOGGER.info(message); - } -} diff --git a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/PollChangedEndpointsHotSwapService.java b/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/PollChangedEndpointsHotSwapService.java deleted file mode 100644 index fe98da9e1c..0000000000 --- a/packages/java/endpoint/src/main/java/com/vaadin/hilla/internal/hotswap/PollChangedEndpointsHotSwapService.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright 2000-2023 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.hilla.internal.hotswap; - -import com.vaadin.flow.internal.BrowserLiveReload; -import com.vaadin.flow.shared.Registration; -import com.vaadin.hilla.engine.EngineConfiguration; -import com.vaadin.hilla.engine.ParserProcessor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -class PollChangedEndpointsHotSwapService implements EndpointHotSwapService { - - private static class ExecutorStatus { - - EngineConfiguration engineConfiguration; - - String openAPI; - } - - private static final Logger LOGGER = LoggerFactory - .getLogger(PollChangedEndpointsHotSwapService.class); - - private final Collection hotSwapListeners = new ArrayList<>(); - - private final int endpointHotReloadPollInterval; - - private Path buildDir; - - private BrowserLiveReload browserLiveReload; - - /** - * @param endpointHotReloadPollInterval - * The interval for polling the changes in seconds. - */ - public PollChangedEndpointsHotSwapService( - int endpointHotReloadPollInterval) { - this.endpointHotReloadPollInterval = endpointHotReloadPollInterval; - } - - @Override - public void monitorChanges(Path buildDir, - BrowserLiveReload browserLiveReload) { - this.browserLiveReload = browserLiveReload; - this.buildDir = buildDir; - - // start polling the endpoints - pollEndpoints(); - } - - private void pollEndpoints() { - debug("Initializing the polling thread for detecting endpoint(s) changes..."); - var status = initializeExecutorStatus(); - ScheduledExecutorService executor = Executors - .newSingleThreadScheduledExecutor(); - executor.scheduleAtFixedRate(() -> { - try { - ParserProcessor parserProcessor = new ParserProcessor( - status.engineConfiguration, - this.getClass().getClassLoader(), false); - var newOpenAPI = parserProcessor.createOpenAPI(); - if (status.openAPI == null) { - status.openAPI = newOpenAPI; - } else if (!status.openAPI.equals(newOpenAPI)) { - status.openAPI = newOpenAPI; - fireEndpointChangedEvent(); - } - } catch (Throwable e) { - error("Error while polling for endpoint(s) changes: %s", - e.getMessage()); - e.printStackTrace(); - } - - }, 0, endpointHotReloadPollInterval, TimeUnit.SECONDS); - debug("for detecting endpoint(s) changes initialized."); - - // Add a shutdown hook to stop the executor when the application is - // shutting down - Runtime.getRuntime().addShutdownHook(new Thread(executor::shutdown)); - } - - private void fireEndpointChangedEvent() { - debug("Endpoint change(s) detected..."); - var event = new HotSwapListener.EndpointChangedEvent(buildDir, - browserLiveReload); - hotSwapListeners.forEach(listener -> listener.endpointChanged(event)); - } - - private ExecutorStatus initializeExecutorStatus() { - ExecutorStatus status = new ExecutorStatus(); - try { - status.engineConfiguration = EngineConfiguration - .loadDirectory(buildDir); - status.engineConfiguration.getParser().setPackages(List.of()); - return status; - } catch (IOException e) { - error("Could not load the engine configuration from %s", buildDir); - throw new RuntimeException(e); - } - } - - @Override - public Registration addHotSwapListener(HotSwapListener listener) { - return Registration.addAndRemove(this.hotSwapListeners, listener); - } - - private void debug(String message) { - LOGGER.debug("### Hilla Endpoint changes Poll service > " + message); - } - - private void error(String format, Object... params) { - error(String.format(format, params)); - } - - private void error(String message) { - LOGGER.error("### Hilla Endpoint changes Poll service > " + message); - } -}