From 939969690738bdc77d2f9f19ee72d12426642bd0 Mon Sep 17 00:00:00 2001 From: marcin Date: Wed, 25 Jan 2023 11:18:07 +0100 Subject: [PATCH] fix: updates after removal of deprecated Flow code (#92) --- integration-tests/development/pom.xml | 10 ---- .../vaadin/quarkus/QuarkusInstantiator.java | 43 ++++----------- .../quarkus/QuarkusInstantiatorFactory.java | 54 +++++++++++++++++++ .../quarkus/QuarkusVaadinServletService.java | 22 ++++---- .../annotation/VaadinServiceEnabled.java | 2 +- 5 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiatorFactory.java diff --git a/integration-tests/development/pom.xml b/integration-tests/development/pom.xml index 89290a1..8044129 100644 --- a/integration-tests/development/pom.xml +++ b/integration-tests/development/pom.xml @@ -128,19 +128,9 @@ - - - --host - - maven-failsafe-plugin - - - -Dvaadin.devmode.vite.options=--host - - diff --git a/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiator.java b/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiator.java index 0ad961e..c45f3c6 100644 --- a/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiator.java +++ b/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiator.java @@ -15,39 +15,27 @@ */ package com.vaadin.quarkus; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.spi.BeanManager; -import jakarta.inject.Inject; import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Stream; -import io.quarkus.arc.Unremovable; +import jakarta.enterprise.inject.spi.BeanManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.vaadin.flow.component.Component; import com.vaadin.flow.di.DefaultInstantiator; import com.vaadin.flow.di.Instantiator; +import com.vaadin.flow.di.InstantiatorFactory; import com.vaadin.flow.i18n.I18NProvider; -import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinServiceInitListener; -import com.vaadin.quarkus.annotation.VaadinServiceEnabled; /** - * Instantiator implementation based on Quarkus DI feature. - * - * Quarkus DI solution (also called ArC) is based on the Contexts and Dependency - * Injection for Java 2.0 specification, but it is not a full CDI - * implementation. Only a subset of the CDI features is implemented. + * Instantiator implementation for Quarkus. * - * See Quarkus CDI - * Reference for further details. + * New instances are created by default by QuarkusInstantiatorFactory. * - * @see Instantiator + * @see InstantiatorFactory */ -@VaadinServiceEnabled -@Unremovable -@ApplicationScoped public class QuarkusInstantiator implements Instantiator { private static final String CANNOT_USE_CDI_BEANS_FOR_I18N = "Cannot use CDI beans for I18N, falling back to the default behavior."; @@ -56,16 +44,12 @@ public class QuarkusInstantiator implements Instantiator { private AtomicBoolean i18NLoggingEnabled = new AtomicBoolean(true); private DefaultInstantiator delegate; - @Inject - BeanManager beanManager; + private BeanManager beanManager; - /** - * Gets the service class that this instantiator is supposed to work with. - * - * @return the service class this instantiator is supposed to work with. - */ - public Class getServiceClass() { - return QuarkusVaadinServletService.class; + public QuarkusInstantiator(DefaultInstantiator delegate, + BeanManager beanManager) { + this.delegate = delegate; + this.beanManager = beanManager; } /** @@ -77,13 +61,6 @@ public BeanManager getBeanManager() { return this.beanManager; } - @Override - public boolean init(final VaadinService service) { - delegate = new DefaultInstantiator(service); - return delegate.init(service) - && getServiceClass().isAssignableFrom(service.getClass()); - } - @Override public T getOrCreate(Class type) { return new BeanLookup<>(getBeanManager(), type) diff --git a/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiatorFactory.java b/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiatorFactory.java new file mode 100644 index 0000000..15b70ac --- /dev/null +++ b/runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiatorFactory.java @@ -0,0 +1,54 @@ +package com.vaadin.quarkus; + +import io.quarkus.arc.Unremovable; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.spi.BeanManager; +import jakarta.inject.Inject; + +import com.vaadin.flow.di.DefaultInstantiator; +import com.vaadin.flow.di.Instantiator; +import com.vaadin.flow.di.InstantiatorFactory; +import com.vaadin.flow.server.VaadinService; +import com.vaadin.quarkus.annotation.VaadinServiceEnabled; + +/** + * Instantiator factory implementation based on Quarkus DI feature. + * + * Quarkus DI solution (also called ArC) is based on the Contexts and Dependency + * Injection for Java 2.0 specification, but it is not a full CDI + * implementation. Only a subset of the CDI features is implemented. + * + * See Quarkus CDI + * Reference for further details. + * + * @see InstantiatorFactory + */ +@VaadinServiceEnabled +@Unremovable +@ApplicationScoped +public class QuarkusInstantiatorFactory implements InstantiatorFactory { + + @Inject + BeanManager beanManager; + + @Override + public Instantiator createInstantitor(VaadinService vaadinService) { + if (!getServiceClass().isAssignableFrom(vaadinService.getClass())) { + return null; + } + return new QuarkusInstantiator(new DefaultInstantiator(vaadinService), + beanManager); + } + + /** + * Gets the service class that this instantiator factory is supposed to work + * with. + * + * @return the service class this instantiator factory is supposed to work + * with. + */ + public Class getServiceClass() { + return QuarkusVaadinServletService.class; + } + +} diff --git a/runtime/src/main/java/com/vaadin/quarkus/QuarkusVaadinServletService.java b/runtime/src/main/java/com/vaadin/quarkus/QuarkusVaadinServletService.java index feacc68..2d39619 100644 --- a/runtime/src/main/java/com/vaadin/quarkus/QuarkusVaadinServletService.java +++ b/runtime/src/main/java/com/vaadin/quarkus/QuarkusVaadinServletService.java @@ -33,6 +33,7 @@ import com.vaadin.flow.component.PollEvent; import com.vaadin.flow.component.UI; import com.vaadin.flow.di.Instantiator; +import com.vaadin.flow.di.InstantiatorFactory; import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.internal.UsageStatistics; import com.vaadin.flow.router.AfterNavigationEvent; @@ -97,20 +98,20 @@ public void fireUIInitListeners(UI ui) { @Override public Optional loadInstantiators() throws ServiceException { - final Set> beans = beanManager.getBeans(Instantiator.class, + final Set> beans = beanManager.getBeans(InstantiatorFactory.class, BeanLookup.SERVICE); if (beans == null || beans.isEmpty()) { throw new ServiceException("Cannot init VaadinService " - + "because no CDI instantiator bean found."); + + "because no CDI instantiator factory bean found."); } - final Bean bean; + final Bean bean; try { // noinspection unchecked - bean = (Bean) beanManager.resolve(beans); + bean = (Bean) beanManager.resolve(beans); } catch (final AmbiguousResolutionException e) { throw new ServiceException( "There are multiple eligible CDI " - + Instantiator.class.getSimpleName() + " beans.", + + InstantiatorFactory.class.getSimpleName() + " beans.", e); } @@ -118,15 +119,16 @@ public Optional loadInstantiators() throws ServiceException { // stored inside VaadinService. Not relying on the proxy allows // accessing VaadinService::getInstantiator even when // VaadinServiceScopedContext is not active - final CreationalContext creationalContext = beanManager + final CreationalContext creationalContext = beanManager .createCreationalContext(bean); final Context context = beanManager.getContext(ApplicationScoped.class); // VaadinServiceScoped - final Instantiator instantiator = context.get(bean, creationalContext); + final InstantiatorFactory instantiatorFactory = context.get(bean, + creationalContext); - if (!instantiator.init(this)) { + Instantiator instantiator = instantiatorFactory.createInstantitor(this); + if (instantiator == null) { throw new ServiceException("Cannot init VaadinService because " - + instantiator.getClass().getName() + " CDI bean init()" - + " returned false."); + + Instantiator.class.getSimpleName() + " is null"); } return Optional.of(instantiator); } diff --git a/runtime/src/main/java/com/vaadin/quarkus/annotation/VaadinServiceEnabled.java b/runtime/src/main/java/com/vaadin/quarkus/annotation/VaadinServiceEnabled.java index f258b37..990b4da 100644 --- a/runtime/src/main/java/com/vaadin/quarkus/annotation/VaadinServiceEnabled.java +++ b/runtime/src/main/java/com/vaadin/quarkus/annotation/VaadinServiceEnabled.java @@ -31,7 +31,7 @@ * Qualifier to mark Vaadin service implementations. * * Qualified CDI beans implementing {@link com.vaadin.flow.i18n.I18NProvider}, - * and {@link com.vaadin.flow.di.Instantiator} interfaces are loaded. + * and {@link com.vaadin.flow.di.InstantiatorFactory} interfaces are loaded. */ @Qualifier @Retention(RUNTIME)