From 29257562062e716cad56ede4627ffc67e499cfe0 Mon Sep 17 00:00:00 2001 From: Javier Godoy <11554739+javier-godoy@users.noreply.github.com> Date: Wed, 4 Feb 2026 14:24:10 -0300 Subject: [PATCH] refactor: log dynamic-theme initialization --- .../addons/demo/DynamicThemeInitializer.java | 49 ++++++++++++++++--- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/flowingcode/vaadin/addons/demo/DynamicThemeInitializer.java b/src/main/java/com/flowingcode/vaadin/addons/demo/DynamicThemeInitializer.java index c302be3..2a4c7fa 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/demo/DynamicThemeInitializer.java +++ b/src/main/java/com/flowingcode/vaadin/addons/demo/DynamicThemeInitializer.java @@ -5,7 +5,12 @@ import com.vaadin.flow.server.communication.IndexHtmlRequestListener; import java.io.IOException; import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; +import java.util.Optional; import java.util.Properties; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Service initialization listener that automatically applies a dynamic theme. @@ -19,18 +24,22 @@ @SuppressWarnings("serial") public class DynamicThemeInitializer implements VaadinServiceInitListener { + private static final Logger logger = LoggerFactory.getLogger(DynamicThemeInitializer.class); + + private static final String PROPERTIES_PATH = "META-INF/dynamic-theme.properties"; + @Override public void serviceInit(ServiceInitEvent event) { if (DynamicTheme.isFeatureSupported()) { - try (InputStream in = getClass().getResourceAsStream("/META-INF/dynamic-theme.properties")) { - if (in != null) { - Properties props = new Properties(); - props.load(in); - String themeName = props.getProperty("theme"); - if (themeName != null) { - DynamicTheme theme = DynamicTheme.valueOf(themeName.trim()); + try { + Enumeration resources = getClass().getClassLoader().getResources(PROPERTIES_PATH); + while (resources.hasMoreElements()) { + URL url = resources.nextElement(); + String source = getSourceName(url); + readTheme(url).ifPresent(theme -> { + logger.info("Applying dynamic theme '{}' from {}", theme, source); event.addIndexHtmlRequestListener(theme::initialize); - } + }); } } catch (IOException e) { throw new RuntimeException("Error reading dynamic-theme.properties", e); @@ -38,4 +47,28 @@ public void serviceInit(ServiceInitEvent event) { } } + private Optional readTheme(URL url) throws IOException { + try (InputStream in = url.openStream()) { + Properties props = new Properties(); + props.load(in); + String themeName = props.getProperty("theme"); + return Optional.ofNullable(themeName).map(String::trim).map(DynamicTheme::valueOf); + } + } + + // Extracts a short, readable source name from the URL. + private String getSourceName(URL url) { + String path = url.getPath(); + // JAR URLs look like: file:/path/to/file.jar!/META-INF/... + int jarSeparator = path.indexOf("!/"); + if (jarSeparator > 0) { + path = path.substring(0, jarSeparator); + int lastSlash = path.lastIndexOf('/'); + if (lastSlash >= 0) { + return path.substring(lastSlash + 1); + } + } + return url.toString(); + } + }