Skip to content

Commit

Permalink
fix: updates after removal of deprecated Flow code (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinVaadin committed Jan 25, 2023
1 parent a1c3209 commit 9399696
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 54 deletions.
10 changes: 0 additions & 10 deletions integration-tests/development/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,9 @@
</goals>
</execution>
</executions>
<configuration>
<systemProperties>
<vaadin.devmode.vite.options>--host</vaadin.devmode.vite.options>
</systemProperties>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<quarkus.test.arg-line>-Dvaadin.devmode.vite.options=--host</quarkus.test.arg-line>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
43 changes: 10 additions & 33 deletions runtime/src/main/java/com/vaadin/quarkus/QuarkusInstantiator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://quarkus.io/guides/cdi-reference">Quarkus CDI
* Reference</a> 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.";
Expand All @@ -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<? extends VaadinService> getServiceClass() {
return QuarkusVaadinServletService.class;
public QuarkusInstantiator(DefaultInstantiator delegate,
BeanManager beanManager) {
this.delegate = delegate;
this.beanManager = beanManager;
}

/**
Expand All @@ -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> T getOrCreate(Class<T> type) {
return new BeanLookup<>(getBeanManager(), type)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="https://quarkus.io/guides/cdi-reference">Quarkus CDI
* Reference</a> 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<? extends VaadinService> getServiceClass() {
return QuarkusVaadinServletService.class;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,36 +98,37 @@ public void fireUIInitListeners(UI ui) {

@Override
public Optional<Instantiator> loadInstantiators() throws ServiceException {
final Set<Bean<?>> beans = beanManager.getBeans(Instantiator.class,
final Set<Bean<?>> 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<Instantiator> bean;
final Bean<InstantiatorFactory> bean;
try {
// noinspection unchecked
bean = (Bean<Instantiator>) beanManager.resolve(beans);
bean = (Bean<InstantiatorFactory>) beanManager.resolve(beans);
} catch (final AmbiguousResolutionException e) {
throw new ServiceException(
"There are multiple eligible CDI "
+ Instantiator.class.getSimpleName() + " beans.",
+ InstantiatorFactory.class.getSimpleName() + " beans.",
e);
}

// Return the contextual instance (rather than CDI proxy) as it will be
// stored inside VaadinService. Not relying on the proxy allows
// accessing VaadinService::getInstantiator even when
// VaadinServiceScopedContext is not active
final CreationalContext<Instantiator> creationalContext = beanManager
final CreationalContext<InstantiatorFactory> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9399696

Please sign in to comment.