Search in sources :

Example 6 with Instantiator

use of com.vaadin.flow.di.Instantiator in project flow by vaadin.

the class ComponentUtil method componentFromElement.

/**
 * Creates a new component instance using the given element, maps the
 * component to the element and optionally maps the element to the component
 * (if <code>mapComponent</code> is <code>true</code>).
 * <p>
 * This is a helper method for Element#as and Component#from.
 *
 * @see Component#from(Element, Class)
 * @see Element#as(Class)
 *
 * @param <T>
 *            the component type
 * @param element
 *            the element
 * @param componentType
 *            the component type
 * @param mapComponent
 *            <code>true</code> to also map the element to the component,
 *            <code>false</code> to only map the component to the element
 * @return a new component instance of the given type
 */
public static <T extends Component> T componentFromElement(Element element, Class<T> componentType, boolean mapComponent) {
    if (element == null) {
        throw new IllegalArgumentException("Element to use cannot be null");
    }
    if (componentType == null) {
        throw new IllegalArgumentException("Component type cannot be null");
    }
    MapToExistingElement wrapData = new MapToExistingElement(element, mapComponent);
    try {
        Component.elementToMapTo.set(wrapData);
        UI ui = UI.getCurrent();
        if (ui == null) {
            throw new IllegalStateException("UI instance is not available. " + "It looks like you are trying to execute UI code outside the UI/Servlet dispatching thread");
        }
        Instantiator instantiator = Instantiator.get(ui);
        return instantiator.createComponent(componentType);
    } finally {
        // This should always be cleared, normally it's cleared in Component
        // but in case of exception it might be not cleared
        Component.elementToMapTo.remove();
    }
}
Also used : MapToExistingElement(com.vaadin.flow.component.Component.MapToExistingElement) Instantiator(com.vaadin.flow.di.Instantiator)

Example 7 with Instantiator

use of com.vaadin.flow.di.Instantiator in project flow by vaadin.

the class SpringInstantiatorTest method createRouteTarget_springManagedBean_instanceIsCreated.

@Test
public void createRouteTarget_springManagedBean_instanceIsCreated() throws ServletException {
    Instantiator instantiator = getInstantiator(context);
    RouteTarget2 singleton = context.getBean(RouteTarget2.class);
    Assert.assertEquals(singleton, instantiator.createRouteTarget(RouteTarget2.class, null));
}
Also used : Instantiator(com.vaadin.flow.di.Instantiator) Test(org.junit.Test)

Example 8 with Instantiator

use of com.vaadin.flow.di.Instantiator in project flow by vaadin.

the class SpringVaadinServletServiceTest method getInstantiator_springManagedBean_instantiatorBeanReturned.

@Test
public void getInstantiator_springManagedBean_instantiatorBeanReturned() throws ServletException {
    Properties properties = new Properties();
    properties.setProperty(FOO, Boolean.TRUE.toString());
    VaadinService service = SpringInstantiatorTest.getService(context, properties);
    Instantiator instantiator = service.getInstantiator();
    Assert.assertEquals(TestInstantiator.class, instantiator.getClass());
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) Instantiator(com.vaadin.flow.di.Instantiator) Properties(java.util.Properties) Test(org.junit.Test) SpringInstantiatorTest(com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)

Example 9 with Instantiator

use of com.vaadin.flow.di.Instantiator in project flow by vaadin.

the class SpringVaadinServletService method loadInstantiators.

@Override
protected Optional<Instantiator> loadInstantiators() throws ServiceException {
    Optional<Instantiator> spiInstantiator = super.loadInstantiators();
    List<Instantiator> springInstantiators = context.getBeansOfType(Instantiator.class).values().stream().filter(instantiator -> instantiator.init(this)).collect(Collectors.toList());
    if (spiInstantiator.isPresent() && !springInstantiators.isEmpty()) {
        throw new ServiceException("Cannot init VaadinService because there are multiple eligible " + "instantiator implementations: Java SPI registered instantiator " + spiInstantiator.get() + " and Spring instantiator beans: " + springInstantiators);
    }
    if (!spiInstantiator.isPresent() && springInstantiators.isEmpty()) {
        Instantiator defaultInstantiator = new SpringInstantiator(this, context);
        defaultInstantiator.init(this);
        return Optional.of(defaultInstantiator);
    }
    return spiInstantiator.isPresent() ? spiInstantiator : springInstantiators.stream().findFirst();
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Instantiator(com.vaadin.flow.di.Instantiator) SessionDestroyListener(com.vaadin.flow.server.SessionDestroyListener) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Registration(com.vaadin.flow.shared.Registration) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) List(java.util.List) Optional(java.util.Optional) ServiceException(com.vaadin.flow.server.ServiceException) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ServiceException(com.vaadin.flow.server.ServiceException) Instantiator(com.vaadin.flow.di.Instantiator)

Aggregations

Instantiator (com.vaadin.flow.di.Instantiator)9 Test (org.junit.Test)7 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)3 VaadinService (com.vaadin.flow.server.VaadinService)2 Properties (java.util.Properties)2 MapToExistingElement (com.vaadin.flow.component.Component.MapToExistingElement)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 ServiceException (com.vaadin.flow.server.ServiceException)1 SessionDestroyListener (com.vaadin.flow.server.SessionDestroyListener)1 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 VaadinServlet (com.vaadin.flow.server.VaadinServlet)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 Registration (com.vaadin.flow.shared.Registration)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 ApplicationContext (org.springframework.context.ApplicationContext)1