Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set load-on-startup for automatically registered Vaadin servlets #84

Merged
merged 2 commits into from
Sep 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.servlet.annotation.WebServlet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -68,7 +69,8 @@

class VaadinQuarkusProcessor {

private static final Logger LOG = LoggerFactory.getLogger(VaadinQuarkusProcessor.class);
private static final Logger LOG = LoggerFactory
.getLogger(VaadinQuarkusProcessor.class);

private static final String FEATURE = "vaadin-quarkus";

Expand Down Expand Up @@ -111,7 +113,7 @@ void mapVaadinServletPaths(final BeanArchiveIndexBuildItem beanArchiveIndex,

// Collect all VaadinServlet instances and remove QuarkusVaadinServlet
// and VaadinServlet from the list.
final Collection<ClassInfo> vaadinServlets = indexView
Collection<ClassInfo> vaadinServlets = indexView
.getAllKnownSubclasses(
DotName.createSimple(VaadinServlet.class.getName()))
.stream()
Expand All @@ -121,14 +123,16 @@ void mapVaadinServletPaths(final BeanArchiveIndexBuildItem beanArchiveIndex,
.equals(VaadinServlet.class.getName()))
.collect(Collectors.toList());

// If no VaadinServlet instances found register QuarkusVaadinServlet
// Register VaadinServlet instances annotated with @WebServlet
vaadinServlets = registerUserServlets(servletProducer, vaadinServlets);
// If no annotated VaadinServlet instances is registered, register
// QuarkusVaadinServlet
if (vaadinServlets.isEmpty()) {
servletProducer.produce(ServletBuildItem
.builder(QuarkusVaadinServlet.class.getName(),
QuarkusVaadinServlet.class.getName())
.addMapping("/*").setAsyncSupported(true).build());
} else {
registerUserServlets(servletProducer, vaadinServlets);
.addMapping("/*").setAsyncSupported(true)
.setLoadOnStartup(10).build());
mcollovati marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -224,22 +228,30 @@ void setupPush(ServletDeploymentManagerBuildItem deployment,
deployment.getDeploymentManager()), 120));
}

private void registerUserServlets(
private Collection<ClassInfo> registerUserServlets(
BuildProducer<ServletBuildItem> servletProducer,
Collection<ClassInfo> vaadinServlets) {
Collection<ClassInfo> registeredServlets = new ArrayList<>(
vaadinServlets);
// TODO: check that we don't register 2 of the same mapping
for (ClassInfo info : vaadinServlets) {
final AnnotationInstance webServletInstance = info.classAnnotation(
DotName.createSimple(WebServlet.class.getName()));
if (webServletInstance == null) {
LOG.warn("Found unexpected {} extends VaadinServlet without @WebServlet, skipping", info.name());
LOG.warn(
"Found unexpected {} extends VaadinServlet without @WebServlet, skipping",
info.name());
registeredServlets.remove(info);
continue;
}

String servletName = Optional
.ofNullable(webServletInstance.value("name"))
.map(AnnotationValue::asString)
.orElse(info.name().toString());
int loadOnStartup = Optional
.ofNullable(webServletInstance.value("loadOnStartup"))
.map(AnnotationValue::asInt).orElse(-1);
final ServletBuildItem.Builder servletBuildItem = ServletBuildItem
.builder(servletName, info.name().toString());

Expand All @@ -251,9 +263,18 @@ private void registerUserServlets(

addWebInitParameters(webServletInstance, servletBuildItem);
setAsyncSupportedIfDefined(webServletInstance, servletBuildItem);

servletBuildItem
.setLoadOnStartup(loadOnStartup > 0 ? loadOnStartup : 1);
if (loadOnStartup < 1) {
LOG.warn(
"Vaadin Servlet needs to be eagerly loaded by setting load-on-startup to be greater than 0. "
+ "Current value for '{}' is '{}', so it will be forced to '1'. "
+ "Please set 'loadOnStartup' attribute on @WebServlet annotation to a value greater than 0.",
servletName, loadOnStartup);
}
servletProducer.produce(servletBuildItem.build());
}
return registeredServlets;
}

private void addWebInitParameters(AnnotationInstance webServletInstance,
Expand Down